All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.obatis.redis.RedisObjectSerializer Maven / Gradle / Ivy

There is a newer version: 3.0.1-release
Show newest version
package com.obatis.redis;

import org.springframework.core.convert.converter.Converter;
import org.springframework.core.serializer.support.DeserializingConverter;
import org.springframework.core.serializer.support.SerializingConverter;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;

/**
 * 此时定义的序列化操作表示可以序列化所有类的对象,当然,这个对象所在的类一定要实现序列化接口
 */
public class RedisObjectSerializer implements RedisSerializer {

    private Converter serializingConverter = new SerializingConverter();
    private Converter deserializingConverter = new DeserializingConverter();

    private static final byte[] EMPTY_BYTE_ARRAY = new byte[0];

    @Override
    public byte[] serialize(Object obj) throws SerializationException {
        if (obj == null) {
            return EMPTY_BYTE_ARRAY;
        }
        return this.serializingConverter.convert(obj);
    }

    @Override
    public Object deserialize(byte[] data) throws SerializationException {
        if (data == null || data.length == 0) {
            return null;
        }
        return this.deserializingConverter.convert(data);
    }

}