How to convert from Json to Protobuf?

java, json, protocol-buffers

Solution

With proto3 you can do this using JsonFormat. It parses directly from the JSON representation, so there is no need for separately calling `MyMessage.parseFrom(...)`. Something like this should work:

JsonFormat.parser().merge(json_string, builder);

Problem

I'm new to using protobuf, and was wondering if there is a simple way to convert a json stream/string to a protobuf stream/string in Java? For example, ``` protoString = convertToProto(jsonString) ``` I have a json string that I want to parse into a protobuf message. So, I want to first convert the json string to protobuf, and then call `Message.parseFrom()` on it.

Original source