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

com.feingto.cloud.config.redis.support.Jackson2JsonRedisSerializer Maven / Gradle / Ivy

The newest version!
package com.feingto.cloud.config.redis.support;

import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import com.feingto.cloud.config.redis.bean.BaseClass;
import com.feingto.cloud.kit.json.JSONObjectMapper;
import org.springframework.cache.support.NullValue;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.io.IOException;

/**
 * Redis Jackson2 序列化/反序列化
 *
 * @author longfei
 */
public class Jackson2JsonRedisSerializer implements RedisSerializer {
    private final ObjectMapper mapper = new JSONObjectMapper();

    public Jackson2JsonRedisSerializer() {
        mapper.registerModule(new SimpleModule().addSerializer(new NullValueSerializer(JsonTypeInfo.Id.CLASS.getDefaultPropertyName())));
        //mapper.enableDefaultTyping(DefaultTyping.NON_FINAL, As.PROPERTY);
    }

    @Override
    public byte[] serialize(Object source) throws SerializationException {
        if (source == null) {
            return new byte[0];
        }
        try {
            return mapper.writeValueAsBytes(source);
        } catch (JsonProcessingException e) {
            throw new SerializationException("Could not write JSON: " + e.getMessage(), e);
        }
    }

    @Override
    public Object deserialize(byte[] source) throws SerializationException {
        return deserialize(source, BaseClass.class);
    }

    public  T deserialize(byte[] source, Class type) throws SerializationException {
        Assert.notNull(type, "Deserialization type must not be null! Pleaes provide Object.class to make use of Jackson2 default typing");
        if (source == null || source.length == 0) {
            return null;
        }
        try {
            return mapper.readValue(source, type);
        } catch (Exception ex) {
            throw new SerializationException("Could not read JSON: " + ex.getMessage(), ex);
        }
    }

    private static class NullValueSerializer extends StdSerializer {
        private static final long serialVersionUID = 1999052150548658808L;
        private final String classIdentifier;

        NullValueSerializer(String classIdentifier) {
            super(NullValue.class);
            this.classIdentifier = StringUtils.hasText(classIdentifier) ? classIdentifier : "@class";
        }

        @Override
        public void serialize(NullValue value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
            jgen.writeStartObject();
            jgen.writeStringField(classIdentifier, NullValue.class.getName());
            jgen.writeEndObject();
        }
    }
}