Doing reflection on a map is a little bit tricky, but the way to do it is
to treat it as a repeated field since that is how it is actually
represented on the wire. A map is a stored as a repeated message field,
where in each message the key is field 1 and the value is field 2.

On Mon, Jun 5, 2017 at 1:37 AM, Qian Zhang <[email protected]> wrote:

> Hi Adam,
>
> We are using picojson <https://github.com/kazuho/picojson> to parse the
> JSON file.
>
> After more debugging, I think I have found the root cause: In the code
> here
> <https://github.com/apache/mesos/blob/1.2.0/3rdparty/stout/include/stout/protobuf.hpp#L575>,
> we are trying to find a field by the key of an entry in a map, obviously it
> will fail since the key of any map entries is not in the protobuf message
> definition (the .proto file), and to avoid finding field by a map entry's
> key, in this method
> <https://github.com/apache/mesos/blob/1.2.0/3rdparty/stout/include/stout/protobuf.hpp#L387:L407>,
> we need to check if "field->is_map()" is true which is currently missed, if
> the field is a map, then we should use the reflection to construct a map
> message, however I do not find a method in protobuf to do that, I find a
> lot of "Addxxx()" methods (e.g., "AddString()", "AddInt64()", etc.), but
> not a method for adding a map. There is a method
> "InsertOrLookupMapValue()", but that is a private method which I can not
> call in my code.
>
> So can you please suggest how to add a map message? Thanks!
>
>
> Regards,
> Qian Zhang
>
> On Sat, Jun 3, 2017 at 1:02 AM, Adam Cozzette <[email protected]>
> wrote:
>
>> It looks to me like your JSON syntax is right. Could you post the C++
>> code you are using to parse the JSON file?
>>
>> On Fri, Jun 2, 2017 at 1:16 AM, Qian Zhang <[email protected]> wrote:
>>
>>> Hi,
>>>
>>> I have a C++ project in which there is a .proto file, and in that file,
>>> there is protobuf message which has a map field:
>>> "map<string, string> annotations = 5;"
>>>
>>> And the JSON file to be parsed with that .proto file has the following
>>> content:
>>> "annotations": {
>>>   "com.example.key1": "value1",
>>>   "com.example.key2": "value2"
>>> }
>>>
>>> The .proto file is in proto2 syntax (it has "syntax = "proto2";" at the
>>> beginning) and I am using protobuf-3.3.0, the compilation succeed, but I
>>> found the annotation map always has only one entry, and both the entry's
>>> key and value are "" which is obviously not correct.
>>>
>>> Any suggestions?
>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "Protocol Buffers" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to [email protected].
>>> To post to this group, send email to [email protected].
>>> Visit this group at https://groups.google.com/group/protobuf.
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.

Reply via email to