4

We plan to replace Boost.serialization with protocol-buffers used in distributed system design. How protocol-buffers support complicated data structures such as std containers?

For instance, such a class is required to be serialized/deserialized in our case:

class Foo
{
  std::vector< std::pair< unsigned int, std::vector< std::pair< int, int> > > > data;
};
unwind
  • 391,730
  • 64
  • 469
  • 606

2 Answers2

4

Protocol buffers have employ a parser which takes a .proto file and creates appropriate serialization routines. See this.

Update: You can represent a vector of strings as:

message MyCollection {   
   repeated string str = 1;
} 

in your proto file.

and use:

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; 
}

It should be easy to extend for another collection/collection of collections.

dirkgently
  • 108,024
  • 16
  • 131
  • 187
  • 1
    Google's tutorial has only presented how to serialze a simple structure. My requirement is about the std container. How to write proto files as what I have referred above? –  Mar 26 '09 at 04:07
  • Better way to copy a vector to repeated fields is mentioned in this another answer on StackOverflow: https://stackoverflow.com/questions/15499641/copy-a-stdvector-to-a-repeated-field-from-protobuf-with-memcpy – mradul dubey Sep 27 '21 at 11:18
4

The protobuf representation of your Foo would look something like this:

message IntPair {
    required int32 first  = 1;
    required int32 second = 2;
};

message FooElem {
    required uint32 first = 1;
    repeated IntPair second = 2;
};

message Foo {
    repeated FooElem data = 1;
};

Note that Protocol Buffers doesn't provide a "seamless" (de)serialization to/from your own structures like Boost.Serialization does. You work with generated objects that come from running the protoc compiler on a file like the above.

These generated classes will not contain std::pair and std::vector members however, so you'll need to copy data too/from the generated objects if you want to keep working with your legacy Foo structure.

Bklyn
  • 2,559
  • 2
  • 20
  • 16