Google ProtocolBuffer(PB) 简介和实例2

Google ProtocolBuffer(PB) 简介和实例2-Wire的使用

分类:java andriod

Wire简介轻量级的protocol buffers,针对移动设备的java库(和protoc产生的代码相比较)由Wire生成的代码方法数量将大大减少,这将有助于android应用避免方法数65k的限制使用方法

序列化和反序列化

// 构建对象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 blocke.printStackTrace();}方法数量对比wire产生的代码和protoc产生的代码方法数比是: 82: 508dd./jarmethod.sh ‘/home/dd/Desktop/shard_with_win7/pb-wire.jar’ 82dd./jarmethod.sh ‘/home/dd/Desktop/shard_with_win7/pb-c.jar’ 508参考链接https://github.com/square/wire

上一篇方法论-项目开发,,少点包袱,多点潇洒

顶1踩0

你的脸是为了呈现上帝赐给人类最贵重的礼物–微笑,

Google ProtocolBuffer(PB) 简介和实例2

相关文章:

你感兴趣的文章:

标签云: