Anyone any thoughts? On 16-Feb-2018 2:27 PM, "tuk" <[email protected]> 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/ > topic/protobuf/ElF4l1j1Pfk/unsubscribe. > To unsubscribe from this group and all its topics, 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.
