c++ - Protocol Buffer 如何支持标准容器的序列化/反序列化?

标签 c++ serialization protocol-buffers

我们计划用分布式系统设计中使用的 Protocol Buffer 替换 Boost.serialization。 Protocol Buffer 如何支持复杂的数据结构,例如标准容器?

例如,在我们的例子中需要对这样的类进行序列化/反序列化:

class Foo
{
  std::vector< std::pair< unsigned int, std::vector< std::pair< int, int> > > > data;
};

最佳答案

Protocol Buffer 使用了一个解析器,它接受一个 .proto 文件并创建适当的序列化例程。参见 this .

更新:您可以将字符串 vector 表示为:

message MyCollection {   
   repeated string str = 1;
} 

在你的原型(prototype)文件中。

并使用:

std::vector<std::string> my_strings; 
// add strings to vector MyCollection proto; 
vector<string>::iterator e = my_strings.end();
for (vector<string>::iterator i = my_strings.begin(); 
    i != e; 
    ++i) {
   *proto.add_str() = *i; 
}

它应该很容易扩展到另一个集合/集合的集合。

关于c++ - Protocol Buffer 如何支持标准容器的序列化/反序列化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/684390/

相关文章:

c++ - 在 range v3 库中,为什么 ranges::copy 不适用于 ranges::views::chunk 的输出?

c++ - 填充动态创建的数组时出现问题

objective-c - 数据序列化

使用 Jackson 序列化 JSON 的 Java 类实现

linker - 在 Linux 上使用 GNU makefile 链接 protobuf 库

c++ - Fstream 按字符读取 while

c++ - 是否可以在其范围之外访问局部变量的内存?

exception - 为什么 Json.NET 序列化因 [Serializable] 和只读属性中的 lambda 而失败?

maven - protobuf-maven-plugin 编译为 golang

c++ - 动态创建一个新的 protobuf 消息