JDK提供的序列化技术相对而已效率较低。在转换二进制数组过程中空间利用率较差。
在github上有个专门对比序列化技术做对比的数据:https://github.com/eishay/jvm-serializers/wiki
本文使用protobuf的改良版protostuff,该技术的性能远远高于JDK提供的Serializable。
1、假设有个实体类:Person;
在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群: | |