I am new to the Google protocol buffer. The intention is to use proto files to generate Java classes that can be sent over the network. Is there a way to have Java Maps in the generated classes? Any example code for it will be extremely useful. The generated Java class should contain a member variable of type Map of keys and values.
-
No, not in the current version.Louis Wasserman– Louis Wasserman2014-10-27 04:34:56 +00:00Commented Oct 27, 2014 at 4:34
-
Thanks for the version. Idea on when it will be available?user2512997– user25129972014-10-27 07:12:03 +00:00Commented Oct 27, 2014 at 7:12
2 Answers
There is an issue, regarding this in google https://code.google.com/p/protobuf/issues/detail?id=299
The last message from October 9, 2014
Project Member #4 [email protected] A new syntax for map fields will be introduced to protobuf: message TestMessage { map a_map_field = 1; }
We are currently working on its implementation and it's supposed to be included in the next major release.
So either wait for new release, or implement your own tuples, with appropriate key and value structures.
Look at similar question How would you encode a Map<String, Object> using Protocol Buffers?
Comments
To folks visiting this page looking on how to add Map into protocol buffers. In proto version 3, map has been added and you can use it as demonstrated below:
The Koltin class:
data class Person(
val name : String,
val badges : Map<String, String>
)
The .proto file:
syntax = "proto3";
message Person {
string name = 1;
map<string, string> badges = 2;
}