Protobuf: what complex Data Structure are Support by protobuf?( C++ C#)

Created on 19 Apr 2016  路  4Comments  路  Source: protocolbuffers/protobuf

I want use protobuf between C++ and C# to transport some complex data structure like Dictionary.

How to make it?

Thanks!

Most helpful comment

That should work. In fact the wire format of map fields is equivalent to your repeated MapStruct field (see here), so you can use your approach and later replace it with a map in a backward-compatible way if you ever upgrade to a newer version of protocol buffers with map support.

All 4 comments

You can convert your dictionary to a List of key value pairs. This can be easily handled using protobuf. Then you can convert it back to a Dictionary later.

We also have support for maps so that may be a good fit for your dictionary.

@Dexteroo7 @acozzette thank you ! if I use version 2.6, there is no maps. I have a idea,I create message like this:
message Msg
{
message MapStruct
{
optional string Key_str = 1;
optional string Value_ba = 2;
}

optional uint32 Msg_ID = 1;
repeated MapStruct Msg_Map = 2;
};

I make a test to prove it works. Is this a good way?Or you guys have a better solution?

That should work. In fact the wire format of map fields is equivalent to your repeated MapStruct field (see here), so you can use your approach and later replace it with a map in a backward-compatible way if you ever upgrade to a newer version of protocol buffers with map support.

Was this page helpful?
0 / 5 - 0 ratings