Protostuff序列化

JDK提供的序列化技术相对而已效率较低。在转换二进制数组过程中空间利用率较差。
在github上有个专门对比序列化技术做对比的数据:https://github.com/eishay/jvm-serializers/wiki


本文使用protobuf的改良版protostuff,该技术的性能远远高于JDK提供的Serializable。


Maven的pom中加入依赖:

<dependency>  
    <groupId>com.dyuproject.protostuff</groupId>  
    <artifactId>protostuff-core</artifactId>  
    <version>1.1.2</version>  
</dependency>  
<dependency>  
    <groupId>com.dyuproject.protostuff</groupId>  
    <artifactId>protostuff-runtime</artifactId>  
    <version>1.1.2</version>  
</dependency>  

或者

<dependency>  
    <groupId>io.protostuff</groupId>  
    <artifactId>protostuff-core</artifactId>  
    <version>1.6.0</version>  
</dependency>  
<dependency>  
    <groupId>io.protostuff</groupId>  
    <artifactId>protostuff-runtime</artifactId>  
    <version>1.6.0</version>  
</dependency>  
简单使用protostuff:
1、假设有个实体类:Person;

2、序列化

private RuntimeSchema<Person> schema = RuntimeSchema.createFrom(Person.class);  
  
  
Person person = new Person();  
person.setxxx();  
...  
  
  
byte[] bytes = ProtostuffIOUtil.toByteArray(person, schema, LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE));  

3、反序列化

private RuntimeSchema<Person> schema = RuntimeSchema.createFrom(Person.class);  
byte[] bytes = ...//Person对象序列化后的byte数组  
 
//准备一个空对象  
Person person = schema.newMessage();  
//将byte数组反序列化后放入person空对象中,  
ProtostuffIOUtil.mergeFrom(bytes, person, schema);  
System.out.println(person);  

====================打个广告,欢迎关注====================

QQ:412425870
csdn博客:
http://blog.csdn.net/caychen
码云:
https://gitee.com/caychen/
github:
https://github.com/caychen

点击群号或者扫描二维码即可加入QQ群:

328243383(1群)



点击群号或者扫描二维码即可加入QQ群:

180479701(2群)



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值