How to convert protocol-buffer to a HashMap in java?

4,081 views
Skip to first unread message

tuk

unread,
Feb 16, 2018, 3:56:57 AM2/16/18
to Protocol Buffers
Cross-posting from stackoverflow

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?

Debraj Manna

unread,
Feb 18, 2018, 10:30:21 PM2/18/18
to Protocol Buffers
Anyone any thoughts?

--
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/topic/protobuf/ElF4l1j1Pfk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to protobuf+unsubscribe@googlegroups.com.
To post to this group, send email to prot...@googlegroups.com.
Visit this group at https://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.

Josh Humphries

unread,
Feb 19, 2018, 8:43:52 AM2/19/18
to Debraj Manna, Protocol Buffers
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

--
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+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages