Google Protocolbuffer (PB) Introduction and use of instance 2-wire

Source: Internet
Author: User

Wire Introduction
    • Lightweight protocol buffers, Java Library for mobile devices
    • (compared to the code generated by PROTOC), the number of code methods generated by wire will be greatly reduced, which will help Android apps avoid limiting the number of 65k methods
How to use
  1. Download the most recent wire jar package, currently the latest version is Wire-compiler-1.7.0-jar-with-dependencies.jar, with a download link on GitHub
  2. Write the. proto file, which is directly used in the previous article introducing PB
  3. Compile the. proto file and generate the Java class with Wrie, with the following command:java -jar wire-compiler-1.7.0-jar-with-dependencies.jar --proto_path=. --java_out=. addressbook.proto
  4. Serialization and deserialization

    // 构建对象PhoneNumber pb = new PhoneNumber.Builder().number("123---adf")        .type(PhoneType.HOME).build();PhoneNumber pb2 = new PhoneNumber.Builder().number("123---adf")        .type(PhoneType.MOBILE).build();List<PhoneNumber> phones = new ArrayList<PhoneNumber>();phones.add(pb);phones.add(pb2);Person john = new Person.Builder().id(1234).name("John Doe")        .email("[email protected]").phone(phones).build();System.out.println(john.toString());// 序列化byte[] johnbyte = john.toByteArray();// 反序列化Wire wire = new Wire();try {    Person john2 = wire.parseFrom(johnbyte, Person.class);    System.out.println(john2.toString());} catch (IOException e) {    // TODO Auto-generated catch block    e.printStackTrace();}
Number of methods Comparison
    • Wire generated code and PROTOC generated code method number ratio is: 82:508
dd@ubuntu:~/Desktop$ ‘/home/dd/Desktop/shard_with_win7/pb-wire.jar‘82dd@ubuntu:~/Desktop$ ‘/home/dd/Desktop/shard_with_win7/pb-c.jar‘508
Reference links
    • Https://github.com/square/wire
    • http://blog.csdn.net/cheyiliu/article/details/44918403

Google Protocolbuffer (PB) Introduction and use of instance 2-wire

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.