Edw*_*per 5 serialization protocol-buffers
我有一个图形数据结构,我想用协议缓冲区编码.图顶点之间存在循环连接.是否有标准/通用方法在protobuf中编码这样的结构?想到的一种方法是向每个顶点添加"id"字段,并使用这些id而不是指针.例如:
message Vertex {
required int32 id = 1;
required string label = 2;
repeated int32 outgoing_edges = 3; // values should be id's of other nodes
}
message Graph {
repeated Vertex vertices = 1;
}
Run Code Online (Sandbox Code Playgroud)
然后我可以编写包装protobuf生成的类的类,并自动将这些标识符转换为反序列化的实际指针(并返回到序列化的ID).这是最好的方法吗?如果是这样,那么有没有人知道使用/记录这种方法的现有项目?如果没有,那么您会推荐什么方法?
如果您需要跨平台支持,那么按照您在问题中建议的方式使用 DTO,然后在您自己的代码中将其映射到/从基于图形的单独模型可能是您的最佳方法。
附带说明一下,在 protobuf-net (c# / .net) 中,我添加了对此的支持,这默默地添加了一个抽象层。基本上,可以进行以下工作:
[ProtoContract]
class Vertex {
...
[ProtoMember(3, AsReference = true)]
public List<Vertex> OutgoingEdges {get;set;}
}
Run Code Online (Sandbox Code Playgroud)