zoukankan      html  css  js  c++  java
  • Protostuff序列化和反序列化

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

    Protostuff简介


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

    Protostuff是一个序列化库,支持一下序列化格式:

    • protobuf
    • protostuff(本地)
    • graph
    • json
    • smile
    • xml
    • yaml
    • kvp

    序列化和反序列化工具


    序列化
    @SuppressWarnings("unchecked")
    public static <T> byte[] serialize(T obj) {
        Class<T> cls = (Class<T>) obj.getClass();
        LinkedBuffer buffer = LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE);
        try {
            Schema<T> 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> T deserialize(byte[] data, Class<T> cls) {
        try {
            T message = objenesis.newInstance(cls);
            Schema<T> 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<Class<?>, Schema<?>> cachedSchema = new ConcurrentHashMap<>();
    
    private static <T> Schema<T> getSchema(Class<T> cls) {
        Schema<T> schema = (Schema<T>) cachedSchema.get(cls);
        if (schema == null) {
            schema = RuntimeSchema.createFrom(cls);
            if (schema != null) {
                cachedSchema.put(cls, schema);
            }
        }
        return schema;
    }

    可以看到方法第4行使用了RuntimeSchema,关于RuntimeSchema的用法,参考:// TODO

    作者:JohnShen 链接:http://www.jianshu.com/p/f017d4518b8f 來源:简书 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
  • 相关阅读:
    vue笔记
    vue工具
    vue笔记
    vuex
    css方法
    html2canvas
    Fiddler
    vue经验
    vue经验
    html2canvas
  • 原文地址:https://www.cnblogs.com/firstdream/p/7809332.html
Copyright © 2011-2022 走看看