I have a protobuf message definition like this:
message MyThing {
string foo = 1;
int32 bar = 2;
}
And a Java/Kotlin API that returns a List<MyThing>
I need to format this as a JSON string, as follows:
[
{
"foo": "value1",
"bar": 1,
},
{
"foo": "value2",
"bar": 2,
},
{
"foo": "value3",
"bar": 3,
}
]
I know I can use JsonFormat to convert protobuf to JSON, however that only operates on a single message. So where I’m having difficulty is how to convert a list of messages to a properly formatted JSON array as shown in the example above without resorting to using string manipulation to manually format it myself.
I have briefly looked at using the Gson library but I’m not sure if this is the right approach.
Patrick Johnson is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.