Protostuff序列化和反序列化

序列化和反序列化是在应对网络编程最常遇到的问题之一。
序列化就是将Java Object转成byte[];反序列化就是将byte[]转成Java Object。
这里不介绍JDK serializable的序列化方式,而是介绍一个更高效的序列化库-protostuff。


Protostuff的项目主页: http://www.protostuff.io/


Protostuff是一个序列化库,支持一下序列化格式:
protobuf
protostuff(本地)
graph
json
smile
xml
yaml
kvp


序列化

@SuppressWarnings("unchecked")
public static  byte[] serialize(T obj) {
    Class cls = (Class) obj.getClass();
    LinkedBuffer buffer = LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE);
    try {
        Schema schema = getSchema(cls);
        return ProtostuffIOUtil.toByteArray(obj, schema, buffer);
    } catch (Exception e) {
        throw new IllegalStateException(e.getMessage(), e);
    } finally {
        buffer.clear();
    }
}

第3行:获得对象的类;
第4行:使用LinkedBuffer分配一块默认大小的buffer空间;
第6行:通过对象的类构建对应的schema;
第7行:使用给定的schema将对象序列化为一个byte数组,并返回。


反序列化

public static  T deserialize(byte[] data, Class cls) {
    try {
        T message = objenesis.newInstance(cls);
        Schema schema = getSchema(cls);
        ProtostuffIOUtil.mergeFrom(data, message, schema);
        return message;
    } catch (Exception e) {
        throw new IllegalStateException(e.getMessage(), e);
    }
}

第3行:使用objenesis实例化一个类的对象;
第4行:通过对象的类构建对应的schema;
第5,6行:使用给定的schema将byte数组和对象合并,并返回


构建schema

构建schema的过程可能会比较耗时,因此希望使用过的类对应的schema能被缓存起来。代码如下,不再赘述

private static Map, Schema> cachedSchema = new ConcurrentHashMap<>();

private static  Schema getSchema(Class cls) {
    Schema schema = (Schema) cachedSchema.get(cls);
    if (schema == null) {
        schema = RuntimeSchema.createFrom(cls);
        if (schema != null) {
            cachedSchema.put(cls, schema);
        }
    }
    return schema;
}

你可能感兴趣的:(Protostuff序列化和反序列化)