Google Protocal buffer Introduction and examples

Source: Internet
Author: User

Brief introduction
    • Protocolbuffer (PB) is a lightweight and efficient structured data storage format that can be used for the serialization of structured data.
    • Like XML and JSON, but PB is more efficient and space-saving than the previous two, and more users save traffic in mobile development.
    • How does PB achieve more flow-saving? From its coding approach, PB uses zigzag encoding and leverages varint technology to achieve two-stage space savings.
Instance
    • Step one, write the. proto file, which is directly on Google official instance and stored as Addressbook.proto
Package tutorial;optionJava_package ="Com.example.tutorial";optionJava_outer_classname ="Addressbookprotos";messageperson {RequiredstringName =1; Required Int32 id =2; Optionalstringemail =3; Enum Phonetype {MOBILE =0; HOME =1; Work =2; }messagePhoneNumber {RequiredstringNumber =1; Optional Phonetype type =2[default = HOME]; } repeated PhoneNumber phone =4;}messageaddressbook {repeated person person =1;}
    • Step two, compile the. proto file, use the PB Supporting Compilation tool Protoc, and generate the Java class
-I=$SRC_DIR--cpp_out=$DST_DIR$SRC_DIR/addressbook.proto
protoc -I=. --java_out=. addressbook.proto
    • Step three, apply the generated class and do the serialization and deserialization operations, relying on the library Protobuf-java-2.6.1.jar (and the version must match the version of the PROTOC in the previous step)
Generate Object John = Person. Newbuilder(). SetId(1234). SetName("John Doe"). Setemail("[email protected]"). Addphone(Person. PhoneNumber. Newbuilder(). Setnumber("555-4321"). SetType(Person. Phonetype. HOME)). Build();System. out. println(John. toString());Serialize to binary array byte[] Johnbyte = John. Tobytearray();Deserialization (intermediate process you can think of is through the network transmission, file storage, etc.)//pay attention to the comparison of two system. outThe output of the try {person john2 = person. Parsefrom(Johnbyte);System. out. println(john2. toString());} catch (Invalidprotocolbufferexception e) {//TODO auto-generated catch block E. Printstacktrace();}
Extended
    • The details of writing. Proto
    • The algorithm of PB coding
    • Based on PB protocol upgrade considerations, etc.
Download
    • The following address provides a complete download of the above examples of projects, resources include: Protobuf-2.6.1.zip source; protoc-2.6.1-win32; protobuf-java-2.6.1.jar; a serialized deserialization instance
    • Download Portal
Reference
    • http://www.ibm.com/developerworks/cn/linux/l-cn-gpb/#ibm-pcon
    • Http://www.cnblogs.com/shrimps/archive/2008/11/06/1327988.html
    • Http://stackoverflow.com/questions/14028293/google-protocol-buffers-vs-json-vs-xml
    • Https://github.com/google/protobuf
    • Https://developers.google.com/protocol-buffers/docs/downloads
    • Https://developers.google.com/protocol-buffers/docs/javatutorial
    • Http://repo2.maven.org/maven2/com/google/protobuf/protobuf-java/2.6.1/

Google Protocal buffer Introduction and examples

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.