0

I Want to Return a JSON response from server to client in gRPC.

one possible way is to convert it to string return the response then convert back to Json Object in client side, but i want to know can we do better?.

i am doing some google and found we can do it with the help of google.protobuf.struct but didn't actually find any good example.

i want an example how i can use it as JSON in java.

Mohit Singh
  • 401
  • 1
  • 10
  • 30
  • JSON is a string. Your original solution is how to get a JSON result. (If you want to do it more efficiently, then using an _actual_ protobuf instead of JSON might help.) – Louis Wasserman Aug 13 '20 at 18:49

1 Answers1

1

If you are using proto3, one option is to define a protobuf message that mirrors the JSON object you wish to populate. Then you can use JsonFormat to convert between protobuf and JSON.

Using a com.google.protobuf.Struct instead of a self-defined message can also work. There is an example shown in the similar question.

voidzcy
  • 520
  • 2
  • 7