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
- 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
- Write the. proto file, which is directly used in the previous article introducing PB
- 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
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