Search code examples
protocol-buffersadditionchildren

Google protobuf how to add sub-children


I have the following protobuf definition

message test {
repeated string fruit = 1 [(order_is_significant) = true]
}

I want to add apple,pear to protbuf and the output should look like

  "test": [
  {
    "fruit": [
    "apple",
    "pear"
  ]
  "rank": 1
   }

I tried to add using test.addFruit("apple") and test.addFruit("pear") method but the output looks like

test {
  fruit: "apple"
  fruit: "pear"
  rank: 1
}

Any ideas?


Solution

  • Your output is in protocol buffer text format. What you want looks like json. So you seem to need some kind of json serializer for protobuf.

    C++: https://developers.google.com/protocol-buffers/docs/reference/cpp/google.protobuf.util.json_util

    Java: https://developers.google.com/protocol-buffers/docs/reference/java/

    You'll find formatters for other languages in the left pane of the C++ link.