irpas技术客

protobuf转json_yuhui66666688gfbfdy_protobuf转json

网络 1773

以字节码为交接处实现pbf和josn互转

//protobuf转json ?? ?public static void main3(String[] args) throws InvalidProtocolBufferException { ?? ??? ? ?? ??? ?//序列化 ?? ??? ?helloworld.Builder builder=helloworld.newBuilder(); ?? ??? ?builder.setId(10); ?? ??? ?builder.setStr("fx"); ? ? ? ? builder.setOpt(20); ?? ??? ?helloworld info=builder.build(); ? ?? ? ? ?byte[] result=info.toByteArray() ; ? ?? ? ? ? ?? ? ?? ? ? ?//返序列化 ?? ? ? ?helloworld hello = helloworld.parseFrom(result); ? ? ? ? ? System.out.println(hello);? ? ? ? ?? ?? ??? ?String jsonFormat =JsonFormat.printToString(hello); ?? ??? ?System.out.println(jsonFormat); ?? ??? ? ?? ?} ?? ? ?? ?示例2: ?? ?byte[] result = FileUtil.InputStream2ByteArray(rmsPath); ? ? ? ? ? ? ? ? ? ? ? ? //返序列化 ? ? ? ? ? ? ? ? ? ? ? ? RMS.RMSBody rmsBody = RMS.RMSBody.parseFrom(result); ? ? ? ? ? ? ? ? ? ? ? ? String jsonFormat = JsonFormat.printer().print(rmsBody);


1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,会注明原创字样,如未注明都非原创,如有侵权请联系删除!;3.作者投稿可能会经我们编辑修改或补充;4.本站不提供任何储存功能只提供收集或者投稿人的网盘链接。

标签: #Protobuf转Json #PUBLIC #static #void #main3String #args #throws