The method of Protobuf and JSON to convert each other

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.