Protostuff序列化
来源:互联网 发布:诺基亚e63塞班软件下载 编辑:程序博客网 时间:2024/05/22 16:34
JDK提供的序列化技术相对而已效率较低。在转换二进制数组过程中空间利用率较差。
在github上有个专门对比序列化技术做对比的数据:https://github.com/eishay/jvm-serializers/wiki
本文使用protobuf的改良版protostuff,该技术的性能远远高于JDK提供的Serializable。
Maven的pom中加入依赖:
或者
简单使用protostuff:
1、假设有个实体类:Person;
2、序列化
3、反序列化
在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);
阅读全文