Protobuf与JSON互相转换

Java

http://code.google.com/p/protobuf-java-format/

maven配置

[html] view plain copy
print ?
  1. <dependency>  
  2. <groupId>com.googlecode.protobuf-java-formatgroupId>  
  3. <artifactId>protobuf-java-formatartifactId>  
  4. <version>1.2version>  
  5. dependency>  

com.googlecode.protobuf-java-format
protobuf-java-format
1.2

从protobuf转json

[java] view plain copy
print ?
  1. Message someProto = SomeProto.getDefaultInstance();  
  2. String jsonFormat = JsonFormat.printToString(someProto);  
Message someProto = SomeProto.getDefaultInstance();
String jsonFormat = JsonFormat.printToString(someProto);

从json转protobuf

[java] view plain copy
print ?
  1. Message.Builder builder = SomeProto.newBuilder();  
  2. String jsonFormat = load json from source;  
  3. JsonFormat.merge(jsonFormat, builder);  
Message.Builder builder = SomeProto.newBuilder();
String jsonFormat = load json from source;
JsonFormat.merge(jsonFormat, builder);

C++

https://github.com/shramov/json2pb

Python

https://github.com/NextTuesday/py-pb-converters

你可能感兴趣的:(Java,精华文章,html,json)