Google's Protobuf object to JSON, not directly using tools such as Fastjson to convert, because PROTOBUF generated the object's Get method, the returned type has byte[], and only the string type can be used as the JSON key. Google offers a dedicated rack package that facilitates protobuf and JSON conversion. Here's how:
1. Add Maven dependency for conversion:
1 <Dependency>2 <groupId>Com.googlecode.protobuf-java-format</groupId>3 <Artifactid>Protobuf-java-format</Artifactid>4 <version>1.2</version>5 </Dependency>
2, Protobuf to JSON method
1 // Protobuf Turn JSON 2 Message.builder message = message.newbuilder (); 3 String json = jsonformat.printtostring (Message.build ());
3. JSON transfer Protobuf method
1 // JSON turn Protobuf 2 Try {3 jsonformat.merge (JSON, message); 4 Catch (ParseException e) {5 e.printstacktrace (); 6 }
The method of Protobuf and JSON to convert each other