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++ - 为什么传统的 GetProcAddress 到 std::function 不能直接工作

c++ - boost mpfr_float 的序列化

c - 如何从 C 库中检索 probuf

c++ - 使用 C 和 C++ 的 Protocol Buffer

c++ - C和C++标准库的关系

c++ - Boost 编译标志 gcc 错误

c# - Newtonsoft json 序列化原始类型 WebApi

java - 序列化和反序列化未实现 Serializable 的对象

支持部分序列化的C++序列化库?

c++ - 由于条件而不传递参数