protobuf对象不能直接使用jsonlib去转,因为protobuf生成的对象的get方法返回的类型有byte[],而只有String类型可以作为json的key,protobuf提供方法进行转换。

引用maven依赖:

<dependency>
    <groupId>com.googlecode.protobuf-java-format</groupId>
    <artifactId>protobuf-java-format</artifactId>
    <version>1.2</version>
</dependency>

protobuf对象转换成json:

String jsonFormat = JsonFormat.printToString(SomeProto);

json转成protobuf对象:

Message.Builder builder =SomeProto.newBuilder();
String jsonFormat = "json字符串";
JsonFormat.merge(jsonFormat, builder);

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-02-25
  • 2019-10-23
  • 2021-06-10
  • 2021-07-22
  • 2021-09-08
  • 2021-07-18
猜你喜欢
  • 2022-12-23
  • 2021-09-26
  • 2021-11-14
  • 2022-12-23
  • 2021-09-27
  • 2021-12-18
  • 2022-12-23
相关资源
相似解决方案