0

I am trying to find the best approach to print multiple protobuf generated Java classes to a custom human readable JSON format.

Given following code that uses Java class generated from protobuf:

Person.Builder personBuilder = PersonData.newBuilder();
Person person = personBuilder
  .setName("John Doe")
  .setAge("99")
  .build();

The following is the default JSON representation when using new JsonFormat().printToString(person):

{ "name": "John Doe", "age":99 }

The desired JSON representation is:

"John Doe":{
  "age":99
}

Is there a common generic way to achieve the above for multiple protobuf models?

0

1 Answer 1

1

You could achieve that by converting from Person object to a Map structure and place the name-value pairs are you choose. Natively, the JsonFormat will adhere to the spec https://developers.google.com/protocol-buffers/docs/proto3 If you move the name of the person to the name of the object, it might make interoperability harder - depending on what you are trying to do with it of course.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.