Protostuff 序列化和反序列化工具

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_41558061/article/details/80886738

google 原生的Protobuf用起来非常麻烦,需要编写.proto ,然后又要编译成.proto文件,生成对应的java文件,但是速度是上又是相当可观的,在项目中设计对序列化工具进行调优,就看见了一篇《java序列化/反序列化之xstream、protobuf、protostuff 的比较与使用例子》
发现 Protostuff 便捷,简单, very nice !

在maven 项目pom 中加入

        <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>

编写序列化工具类:

import io.protostuff.LinkedBuffer;
import io.protostuff.ProtobufIOUtil;
import io.protostuff.ProtostuffIOUtil;
import io.protostuff.Schema;
import io.protostuff.runtime.RuntimeSchema;

/**
 * 序列化and反序列化工具
 * @author Lilu
 *
 */
public class ProtoBufUtil {

    public ProtoBufUtil(){
        super();
    }

    @SuppressWarnings("unchecked")
    public static <T> byte[] Serializable(T o){

        Schema schema =RuntimeSchema.getSchema(o.getClass());   
        return ProtobufIOUtil.toByteArray(o, schema, LinkedBuffer.allocate(256));       
    }

     @SuppressWarnings("unchecked")
    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;
        }   
}

接下来编写好我们的模型类:

public class Model {

    private String name;

    private int age;

    private String type;

    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 getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    @Override
    public String toString() {
        return "Model [name=" + name + ", age=" + age + ", type=" + type + "]";
    }   
}

测试代码:

import java.util.Arrays;

public class Main {

    public static void main(String[] args) {

        Model model = new Model();
        model.setName("jack");
        model.setAge(22);
        model.setType("学生");

        byte[] serializable = ProtoBufUtil.Serializable(model);
        System.out.println("serializable : "+Arrays.toString(serializable));

        Model deserializer = ProtoBufUtil.deserializer(serializable, Model.class);
        System.out.println("deserializer : "+deserializer.toString());
    }
}

输出结果:

serializable : [10, 4, 106, 97, 99, 107, 16, 22, 26, 6, -27, -83, -90, -25, -108, -97]
deserializer : Model [name=jack, age=22, type=学生]

总结:
当文件较小的时候就可以采用java 自带的Serializable工具,这样会快一些。。

猜你喜欢

转载自blog.csdn.net/weixin_41558061/article/details/80886738