java主流对象序列化方式使用样例

1、运行结果

先看运行结果,本机自行测试,非官方数据,仅作参考 :

初始化数据============================
Student{id='00011', name='Y', age=18, grade='100', teacher=Teacher{id='00001', name='X', age=18}}

原生API序列化============================
序列化后数据长度:239
初始化时间:1ms
序列化10000次花费时间(包含初始化时间):17ms
序列化10000次花费时间(不包含初始化时间):16ms
反序列化后数据:Student{id='00011', name='Y', age=18, grade='100', teacher=Teacher{id='00001', name='X', age=18}}

jackson序列化============================
序列化后数据长度:93
初始化时间:383ms
序列化10000次花费时间(包含初始化时间):538ms
序列化10000次花费时间(不包含初始化时间):155ms
反序列化后数据:Student{id='00011', name='Y', age=18, grade='100', teacher=Teacher{id='00001', name='X', age=18}}

FastJson序列化============================
序列化后数据长度:93
初始化时间:0ms
序列化10000次花费时间(包含初始化时间):204ms
序列化10000次花费时间(不包含初始化时间):204ms
反序列化后数据:Student{id='00011', name='Y', age=18, grade='100', teacher=Teacher{id='00001', name='X', age=18}}

Hessian序列化============================
序列化后数据长度:146
初始化时间:28ms
序列化10000次花费时间(包含初始化时间):33ms
序列化10000次花费时间(不包含初始化时间):5ms
反序列化后数据:Student{id='00011', name='Y', age=18, grade='100', teacher=Teacher{id='00001', name='X', age=18}}

ProtoBuf序列化============================
序列化后数据长度:31
初始化时间:1913ms
序列化10000次花费时间(包含初始化时间):7018ms
序列化10000次花费时间(不包含初始化时间):5105ms
反序列化后数据:Student{id='00011', name='Y', age=18, grade='100', teacher=Teacher{id='00001', name='X', age=18}}

Kyro序列化============================
序列化后数据长度:49
初始化时间:49ms
序列化10000次花费时间(包含初始化时间):725ms
序列化10000次花费时间(不包含初始化时间):676ms
反序列化后数据:Student{id='00011', name='Y', age=18, grade='100', teacher=Teacher{id='00001', name='X', age=18}}

2、代码

pom.xml文件依赖:
	
        
            junit
            junit
            3.8.1
            test
        
        
            org.codehaus.jackson
            jackson-mapper-asl
            1.9.13
        
        
            com.alibaba
            fastjson
            1.2.52
        
        
            com.baidu
            jprotobuf
            2.2.6
        
        
            com.caucho
            hessian
            4.0.51
        
        
            com.esotericsoftware
            kryo
            4.0.2
        
    
实例化对象:
Student:
public class Teacher implements Serializable{
    private static final long serialVersionUID = 1L;
    @Protobuf(required = true, order = 1, fieldType = FieldType.STRING)
    private String id;
    @Protobuf(required = true, order = 2, fieldType = FieldType.STRING)
    private String name;
    @Protobuf(required = true, order = 3, fieldType = FieldType.INT32)
    private int age;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Teacher{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
Teacher:
public class Student implements Serializable{
    private static final long serialVersionUID = 1L;
    @Protobuf(required = true, order = 1, fieldType = FieldType.STRING)
    private String id;
    @Protobuf(required = true, order = 2, fieldType = FieldType.STRING)
    private String name;
    @Protobuf(required = true, order = 3, fieldType = FieldType.INT32)
    private int age;
    @Protobuf(required = true, order = 4, fieldType = FieldType.STRING)
    private String grade;
    @Protobuf(required = true, order = 5, fieldType = FieldType.OBJECT)
    private Teacher teacher;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getGrade() {
        return grade;
    }

    public void setGrade(String grade) {
        this.grade = grade;
    }

    public Teacher getTeacher() {
        return teacher;
    }

    public void setTeacher(Teacher teacher) {
        this.teacher = teacher;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", grade='" + grade + '\'' +
                ", teacher=" + teacher.toString() +
                '}';
    }
}
序列化实现:
public class SerializableDemo {

    //初始化
    private final static int SERIALIZABLE_NUM = 10000;


    public static void main(String[] args) throws Exception {
        System.out.println("初始化数据============================\n" + init().toString() + "\n");
        //java原生
        excuteWithNative();
        //jackson
        excuteWithJackSon();
        //fastJson
        excuteWithFastJson();
        //hessian
        excuteWithHessian();
        //protobuf,baidu公司开发,底层给予protobuf,不用proto文件,通过注解即可实现序列化反序列化
        excuteWithProtoBuf();
        //kryo
        excuteWithKryo();

        //主流序列化方式JSON/Hessian(2) /xml/protobuf/kryo/MsgPack/FST/thrift/protostuff/Avro
        //更多序列化方式查看网络资料即可
    }

    private static void excuteWithNative() throws IOException, ClassNotFoundException {
        Student student = init();
        System.out.println("原生API序列化============================");
        Long start1=System.currentTimeMillis();//初始化时间
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);

        Long start2=System.currentTimeMillis();//序列化时间
        for(int i=0;i studentCodec= ProtobufProxy.create(Student.class,false);
        byte[] bytes=null;

        Long start2=System.currentTimeMillis();//序列化时间
        for(int i=0;i
更多序列化方式及分析请自行查阅网络资料

你可能感兴趣的:(++++,Java,based)