Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would you encode a Map<String, Object> using Protocol Buffers?

I'm trying to use Protocol Buffers for message serialization.

My message format should contain Map< String, Object > entries ... but how do I write the .proto definition?

As far as I know, Protocol Buffers does not have a build-in Map type. I could model around that using repeating fields. But the big problem I have is, that you need to define all your types. I want my message to be flexible, so I can't specify the types.

Any ideas?

like image 341
arturh Avatar asked Jul 09 '09 09:07

arturh


1 Answers

I'd model a tuple with a key and a value (probably one value field per type that the value could be). Then just have a repeated list of that tuple type. You'd need to build the map itself in code.

When you say you can't specify the types - what sort of types are you talking about? If you have an optional field of each type in the tuple, then that would cope for all the primitives - and you could map messages by serializing them into a byte string.

It sounds like the level of "unstructure" you have may not be a good fit for PB though.

like image 67
Jon Skeet Avatar answered Oct 28 '22 14:10

Jon Skeet