Protostuff序列化

来源:互联网 发布:诺基亚e63塞班软件下载 编辑:程序博客网 时间:2024/05/22 16:34
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);

阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 小孩儿保险 平安保险智慧星的弊端 保险小孩 平安智能星优缺点 平安智能星 平安保险智慧星介绍 教育金保险 平安婴儿保险种类 平安保险智慧星 平安智慧星 智慧星保险 平安的智能星保险怎么样 儿童保险公司排名 小孩买哪种保险好 幼儿意外保险 少年儿童保险 儿童保险有哪些 怎样购买儿童保险 友邦儿童保险 保险儿童 幼儿 保险 车辆保险险种介绍 儿童重疾保险 给幼儿买什么保险好 汽车保险险种介绍 儿童上什么保险好 社区儿童保险 幼儿园幼儿保险 儿童什么保险好 儿童保险什么好 幼儿保险险种介绍 如何购买儿童保险 比较好的儿童保险 儿童保险要不要买 儿童高端保险 少年儿童幸福保险 儿童适合什么保险 幼儿教育金保险 人寿保险儿童险种 小孩 保险 友邦保险儿童险