Redis序列化及反序列化應用

Redis序列化及反序列化

需要引入的jar包:

protostuff-api-1.1.3.jar

protostuff-collectionschema-1.1.3.jar

protostuff-core-1.1.3.jar

protostuff-runtime-1.1.3.jar

1、java對象序列化工具

import java.io.Serializable;

/** 
 * [概 要] java對象序列化工具
 * [環 境] J2SE 1.7 
 * @version 1.0 
 */  
public class VO<T> implements Serializable {  
    private T value;  
    public VO(T value) {  
        this.value = value;  
    }  
    public VO() {  
    }  
    public T getValue() {  
        return value;  
    }  
    @Override  
    public String toString() {  
        return "VO{" +  
                "value=" + value +  
                '}';  
    }  
}  

2、protostuff對象序列化工具

import java.util.concurrent.ConcurrentHashMap;  
import com.dyuproject.protostuff.LinkedBuffer;  
import com.dyuproject.protostuff.ProtostuffIOUtil;  
import com.dyuproject.protostuff.Schema;  
import com.dyuproject.protostuff.runtime.RuntimeSchema;  
  
/** 
 * [概 要] protostuff對象序列化工具
 * [環 境] J2SE 1.7 
 * @version 1.0 
 */  
public class ProtostuffSerializer {  
    private static ConcurrentHashMap<Class<?>, Schema<?>> cachedSchema = new ConcurrentHashMap<Class<?>, Schema<?>>();  
    public <T> byte[] serialize(final T source) {  
        VO<T> vo = new VO<T>(source);  
        final LinkedBuffer buffer = LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE);  
        try {  
            final Schema<VO> schema = getSchema(VO.class);  
            return serializeInternal(vo, schema, buffer);  
        } catch (final Exception e) {  
            throw new IllegalStateException(e.getMessage(), e);  
        } finally {  
            buffer.clear();  
        }  
    }  
    public <T> T deserialize(final byte[] bytes) {  
        try {  
            Schema<VO> schema = getSchema(VO.class);
            VO vo = deserializeInternal(bytes, schema.newMessage(), schema);  
            if (vo != null && vo.getValue() != null) {  
                return (T) vo.getValue();  
            }  
        } catch (final Exception e) {  
            throw new IllegalStateException(e.getMessage(), e);  
        }  
        return null;  
    }  
    private <T> byte[] serializeInternal(final T source, final Schema<T> schema, final LinkedBuffer buffer) {  
        return ProtostuffIOUtil.toByteArray(source, schema, buffer);  
    }  
    private <T> T deserializeInternal(final byte[] bytes, final T result, final Schema<T> schema) {  
        ProtostuffIOUtil.mergeFrom(bytes, result, schema);  
        return result;  
    }  
    private static <T> Schema<T> getSchema(Class<T> clazz) {  
        @SuppressWarnings("unchecked")  
        Schema<T> schema = (Schema<T>) cachedSchema.get(clazz);  
        if (schema == null) {  
            schema = RuntimeSchema.createFrom(clazz);  
            cachedSchema.put(clazz, schema);  
        }  
        return schema;  
    }  
}  

3、序列化及反序列化調用

public class UnOrSerialize {

	/** 
     *  
     * [概 要] 序列化對象 
     *  
     * @param object 要序列化的對象 
     * @return byte[] 對象序列化後的字節信息 
     */  
	public static  byte[] serialize(Object object) {
        ProtostuffSerializer protostuffSerializer = new ProtostuffSerializer();  
        byte[] result = protostuffSerializer.serialize(object);  
          
        return result;  
    }  
      
    /** 
     *  
     * [概 要] 反序列化 
     * @param bytes 對象的字節信息 
     * @return Object 反序列化的對象 
     */  
    public static Object unserialize(byte[] bytes) {  
          
        ProtostuffSerializer protostuffSerializer = new ProtostuffSerializer();  
        Object object=protostuffSerializer.deserialize(bytes);  
          
        return object;  
    }  
}

注:本文藉助網絡資源整理,只用於個人學習,如有侵權,請聯繫博主。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章