美文网首页码农的世界程序员
Protostuff序列化和反序列化使用说明

Protostuff序列化和反序列化使用说明

作者: zhglance | 来源:发表于2017-11-08 17:45 被阅读326次

google原生的protobuffer使用起来相当麻烦,首先要写.proto文件,然后编译.proto文件,生成对应的.java文件,鄙人试了一次,发现真的很麻烦。而protostuff的官方网站(http://www.protostuff.io/documentation/runtime-schema/),对于智商比较低的小编来说也略显生涩,于是鄙人就根据项目中用到的protostuff,撰写此文,以方便自己和他人加深印象和学习。

1.实战

1.maven依赖:

        <dependency>

            <groupId>io.protostuff</groupId>

            <artifactId>protostuff-core</artifactId>

            <version>1.4.0</version>

        </dependency>

        <dependency>

            <groupId>io.protostuff</groupId>

            <artifactId>protostuff-runtime</artifactId>

            <version>1.4.0</version>

        </dependency>

2.ProtoBufUtil工具类:ProtoBufUtil.java

import io.protostuff.LinkedBuffer;

import io.protostuff.ProtobufIOUtil;

import io.protostuff.ProtostuffIOUtil;

import io.protostuff.Schema;

import io.protostuff.runtime.RuntimeSchema;

/**

* Created by lance on 2017/2/20.

*/

public class ProtoBufUtil {

    public ProtoBufUtil() {

    }

    public static <T> byte[] serializer(T o) {

        Schema schema = RuntimeSchema.getSchema(o.getClass());

        return ProtobufIOUtil.toByteArray(o, schema, LinkedBuffer.allocate(256));

    }

    public static <T> T deserializer(byte[] bytes, Class<T> clazz) {

        T obj = null;

        try {

            obj = clazz.newInstance();

            Schema schema = RuntimeSchema.getSchema(obj.getClass());

            ProtostuffIOUtil.mergeFrom(bytes, obj, schema);

        } catch (InstantiationException e) {

            e.printStackTrace();

        } catch (IllegalAccessException e) {

            e.printStackTrace();

        }

        return obj;

    }

}

  3. bean类Student.java:   

import io.protostuff.Tag;

/**

* Created by lance on 2017/2/20.

*/

public class Student {

    @Tag(1)

    private String name;

    @Tag(2)

    private String studentNo;

    @Tag(3)

    private int age;

    @Tag(4)

    private String schoolName;

  // 关于@Tag,要么所有属性都有@Tag注解,要么都没有,不能一个类中只有部分属性有@Tag注解

    public String getName() {

        return name;

    }

    public void setName(String name) {

        this.name = name;

    }

    public String getStudentNo() {

        return studentNo;

    }

    public void setStudentNo(String studentNo) {

        this.studentNo = studentNo;

    }

    public int getAge() {

        return age;

    }

    public void setAge(int age) {

        this.age = age;

    }

    public String getSchoolName() {

        return schoolName;

    }

    public void setSchoolName(String schoolName) {

        this.schoolName = schoolName;

    }

    @Override

    public String toString() {

        return "Student{" +

                "name='" + name + '\'' +

                ", studentNo='" + studentNo + '\'' +

                ", age=" + age +

                ", schoolName='" + schoolName + '\'' +

                '}';

    }

}

4.test类ProtoBufUtilTest.java: 

import java.util.Arrays;

/**

* Created by lance on 2017/2/20.

*/

public class ProtoBufUtilTest {

    public static void main(String[] args) {

        Student student = new Student();

        student.setName("lance");

        student.setAge(28);

        student.setStudentNo("2011070122");

        student.setSchoolName("BJUT");

        byte[] serializerResult = ProtoBufUtil.serializer(student);

        System.out.println("serializer result:" + Arrays.toString(serializerResult));

        Student deSerializerResult = ProtoBufUtil.deserializer(serializerResult,Student.class);

        System.out.println("deSerializerResult:" + deSerializerResult.toString());

    }

}

4.输出结果: 

serializer result:[10, 5, 108, 97, 110, 99, 101, 18, 10, 50, 48, 49, 49, 48, 55, 48, 49, 50, 50, 24, 28, 34, 4, 66, 74, 85, 84]

deSerializerResult:Student{name='lance', studentNo='2011070122', age=28, schoolName='BJUT'}

看,简单吧!

参考资料:1. protostuff官方网站.

                    2. Protostuff序列化.

相关文章

  • Protostuff

    使用Protostuff进行序列化和反序列化(官网:http://www.protostuff.io/)工具类如下:

  • protostuff

    protostuff是什么 Google的protobuf因为其编码后体积小,序列化/反序列化性能好,被广泛使用。...

  • Protostuff序列化和反序列化使用说明

    google原生的protobuffer使用起来相当麻烦,首先要写.proto文件,然后编译.proto文件,生成...

  • Protostuff序列化和反序列化

    序列化和反序列化是在应对网络编程最常遇到的问题之一。序列化就是将Java Object转成byte[];反序列化就...

  • Protostuff序列化和反序列化

    Java序列化和反序列化 序列化和反序列化是在应对网络编程最常遇到的问题之一。序列化就是将Java Object转...

  • protostuff序列化

    github地址: https://github.com/protostuff/protostuff Docume...

  • 【Flink 精选】常见的性能问题及其定位思路

    常见的性能问题及其定位思路 1.常见的性能问题 (1)JSON序列化和反序列化 source的序列化,sink的反...

  • 序列化和反序列化的概念

    1序列化:把对象转为字节序列的过程 为序列化; 2相反:字节---------->对象 为反序列化 3 序列化的场...

  • java专题之序列化

    一、基本概念 1、什么是序列化和反序列化 (1)Java序列化是指把Java对象转换为字节序列的过程,而Java反...

  • 序列化和反序列细节处理

    python 中pickle 和 json 都是序列化和反序列化的模块。 关于序列化和反序列化 序列化和反序列化[...

网友评论

    本文标题:Protostuff序列化和反序列化使用说明

    本文链接:https://www.haomeiwen.com/subject/mppymxtx.html