java -jar wire-compiler-1.7.0-jar-with-dependencies.jar --proto_path=. --java_out=. addressbook.proto
序列化和反序列化
// 构建对象
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("jdoe@example.com").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();
}
dd@ubuntu:~/Desktop$ ./jarmethod.sh ‘/home/dd/Desktop/shard_with_win7/pb-wire.jar‘
82
dd@ubuntu:~/Desktop$ ./jarmethod.sh ‘/home/dd/Desktop/shard_with_win7/pb-c.jar‘
508
Google ProtocolBuffer(PB) 简介和实例2-Wire的使用
原文地址:http://blog.csdn.net/cheyiliu/article/details/45459353