A much more efficient approach would be to use the getAllFields() method of
Message and then translate the message into a map of maps. The keys of the
getAllFields() map are field descriptors, from which you can extract the
field name. If a value is a nested message, you can recursively apply the
same transformation.

However, if you are trying to emulate the map you get from JSON
serialization, it will be trickier. Several types (the "well-known types")
have custom JSON forms. And proto has a standard way to map field names (as
defined in proto) to JSON keys (converts lower--snake-case -> camel-case
with initial lower-case). So if that's what you're after, it's probably is
better to just rely on JSON serialization instead of trying to reproduce
that logic.


----
*Josh Humphries*
jh...@bluegosling.com

On Sun, Feb 18, 2018 at 10:30 PM, Debraj Manna <subharaj.ma...@gmail.com>
wrote:

> Anyone any thoughts?
>
> On 16-Feb-2018 2:27 PM, "tuk" <subharaj.ma...@gmail.com> wrote:
>
>> Cross-posting from stackoverflow
>> <https://stackoverflow.com/questions/48821916/how-to-convert-protocol-buffer-message-to-a-hashmap-in-java>
>>
>> I have a protobuf message of the form
>>
>> enum PolicyValidationType {
>>     Number = 0;}
>>
>>
>> message NumberPolicyValidation {
>>     optional int64 maxValue = 1;
>>     optional int64 minValue = 2;}
>>
>> message PolicyObject {
>>     required string key = 1;
>>     optional string value = 2;
>>     optional string name = 3;
>>     optional PolicyValidationType validationType = 4;
>>     optional NumberPolicyValidation numberPolicyValidation = 5;}
>>
>>
>> For example
>>
>> policyObject {
>>       key: "sessionIdleTimeoutInSecs"
>>       value: "1800"
>>       name: "Session Idle Timeout"
>>       validationType: Number
>>       numberPolicyValidation {
>>         maxValue: 3600
>>         minValue: 5
>>       }}
>>
>>
>> Can someone let me know how can I convert this to a Map like below:-
>>
>> {validationType=Number, name=Session Idle Timeout, 
>> numberPolicyValidation={maxValue=3600.0, minValue=5.0}, value=1800, 
>> key=sessionIdleTimeoutInSecs}
>>
>>
>> One way I can think of is convert this to a json and then convert the
>> json to map?
>>
>> PolicyObject policyObject;...JsonFormat jsonFormat = new JsonFormat();final 
>> String s = jsonFormat.printToString(policyObject);Type objectMapType = new 
>> TypeToken<HashMap<String, Object>>() {}.getType();Gson gson = new 
>> GsonBuilder().registerTypeAdapter(new 
>> TypeToken<HashMap<String,Object>>(){}.getType(), new 
>> PrimitiveDeserializer()).create();Map<String, Object> mappedObject = 
>> gson.fromJson(s, objectMapType);
>>
>>
>> I think there must be some better way. Can someone suggest any better
>> approach?
>>
>> --
>> You received this message because you are subscribed to a topic in the
>> Google Groups "Protocol Buffers" group.
>> To unsubscribe from this topic, visit https://groups.google.com/d/to
>> pic/protobuf/ElF4l1j1Pfk/unsubscribe.
>> To unsubscribe from this group and all its topics, send an email to
>> protobuf+unsubscr...@googlegroups.com.
>> To post to this group, send email to protobuf@googlegroups.com.
>> 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 protobuf+unsubscr...@googlegroups.com.
> To post to this group, send email to protobuf@googlegroups.com.
> 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 protobuf+unsubscr...@googlegroups.com.
To post to this group, send email to protobuf@googlegroups.com.
Visit this group at https://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.

Reply via email to