Is there a standard mapping between JSON and Protocol Buffers?

后端 未结 6 1403
后悔当初
后悔当初 2020-12-13 19:02

From a comment on the announcement blog post:

Regarding JSON: JSON is structured similarly to Protocol Buffers, but protocol buffer binary format

6条回答
  •  鱼传尺愫
    2020-12-13 19:21

    I needed to marshal from GeneratedMessageLite to a JSON object but did not need to unmarshal. I couldn't use the protobuf library in Pangea's answer because it doesn't work with the LITE_RUNTIME option. I also didn't want to burden our already large legacy system with generating more compiled code for the existing protocol buffers. For mashalling to JSON, I went with this simple solution to marshal

        final Person gpb = Person.newBuilder().setName("Bill Monroe").build();
        final Gson gson = new Gson();
        final String jsonString = gson.toJson(gpb);
    

提交回复
热议问题