java protostuff 序列化反序列化工具

protostuff是由谷歌开发的一个非常优秀的序列化反序列化工具

maven导入包:

<dependency>
  <groupId>io.protostuff</groupId>
  <artifactId>protostuff-runtime</artifactId>
  <version>1.6.0</version>
</dependency>


<dependency>
  <groupId>io.protostuff</groupId>
  <artifactId>protostuff-core</artifactId>
  <version>1.6.0</version>
</dependency>

java代码如下:

package com.common.utils;

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

/**
 * 
 * @desc protobuf序列化反序列化工具
 * @author wulm
 */
public class SerializationProtostuffUtil {

    /**
     * @desc protostuff 目前不支持直接序列化List等对象,需要使用普通的POJO包装一下
     * @author wulm
     */
    private static class SerializeData {
        private Object target;

        public Object getTarget() {
            return target;
        }

        public void setTarget(Object target) {
            this.target = target;
        }
    }

    private static final ThreadLocal<LinkedBuffer> BUFFER_THREAD_LOCAL = ThreadLocal
            .withInitial(() -> LinkedBuffer.allocate(512));

    /**
     * @desc 序列化
     * @auth wulm
     */
    @SuppressWarnings("unchecked")
    public static byte[] serialize(Object obj) {
        SerializeData data = new SerializeData();
        data.setTarget(obj);

        // this is lazily created and cached by RuntimeSchema
        // so its safe to call RuntimeSchema.getSchema(Foo.class) over and over
        // The getSchema method is also thread-safe
        Schema<SerializeData> schema = RuntimeSchema.getSchema((Class<SerializeData>) data.getClass());

        // Re-use (manage) this buffer to avoid allocating on every serialization
        // LinkedBuffer buffer = LinkedBuffer.allocate(512);
        LinkedBuffer buffer = BUFFER_THREAD_LOCAL.get();

        // ser
        try {
            return ProtostuffIOUtil.toByteArray(data, schema, buffer);
        } catch (Exception e) {
            throw new IllegalStateException(e.getMessage(), e);
        } finally {
            buffer.clear();
        }

    }

    /**
     * 
     * @desc 反序列化
     * @auth wulm
     */
    @SuppressWarnings("unchecked")
    public static <T> T deserialize(byte[] data, Class<T> cls) {
        Schema<SerializeData> schema = RuntimeSchema.getSchema(SerializeData.class);
        // deser
        SerializeData message = schema.newMessage();
        ProtostuffIOUtil.mergeFrom(data, message, schema);

        return (T) message.getTarget();
    }

    // public static class Aaa {
    //
    // public static void main(String[] args) {
    //
    // Aaa aaa = new Aaa();
    // aaa.setA("你好呀");
    // aaa.setB("我是佩琪");
    // aaa.setC("你好");
    // aaa.setD("我是猪爸爸");
    //
    // List<Aaa> list = new ArrayList<>();
    // list.add(aaa);
    //
    // byte[] serialize = SerializationProtostuffUtil.serialize(list);
    //
    // List<Aaa> bb = SerializationProtostuffUtil.deserialize(serialize,
    // List.class);
    //
    // System.out.println(JacksonUtils.writeValueAsString(bb));
    //
    // }
    //
    // private String a;
    // private String b;
    // private String c;
    // private String d;
    //
    // public String getA() {
    // return a;
    // }
    //
    // public void setA(String a) {
    // this.a = a;
    // }
    //
    // public String getB() {
    // return b;
    // }
    //
    // public void setB(String b) {
    // this.b = b;
    // }
    //
    // public String getC() {
    // return c;
    // }
    //
    // public void setC(String c) {
    // this.c = c;
    // }
    //
    // public String getD() {
    // return d;
    // }
    //
    // public void setD(String d) {
    // this.d = d;
    // }
    //
    // }
}

参阅资料:

  https://github.com/protostuff/protostuff

  http://www.zhangleiup.com/post/112.htm

  http://www.cnblogs.com/wolf-bin/p/9269987.html

猜你喜欢

转载自www.cnblogs.com/wulm/p/9588345.html
今日推荐