redis缓存序列化与反序列化使⽤fastjson package fr.falum.cache;
/**
* Created by zhangsong on 2019/7/2.
*/
/*
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.dule.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;*/
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.ParserConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.apache.log4j.Logger;
import org.springframework.cache.support.NullValue;
import org.dis.serializer.GenericJackson2JsonRedisSerializer;
import org.dis.serializer.RedisSerializer;
import org.dis.serializer.SerializationException;
import java.io.IOException;
import java.nio.charset.Charset;
public class CustomFastJsonGenericJackson2JsonRedisSerializerExt<T> implements RedisSerializer<T> {
Logger Logger(getClass());
public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
static {
//        GlobalInstance().addAccept("");
}
private Class<T> clazz;
public CustomFastJsonGenericJackson2JsonRedisSerializerExt(){
//        this(Object.class);
super();
}
public CustomFastJsonGenericJackson2JsonRedisSerializerExt(Class<T> clazz) {
super();
logger.warn("缓存初始化参数:"+Name());
this.clazz = clazz;
}
@Override
public byte[] serialize(T t) throws SerializationException {
if (t == null) {
return new byte[0];
}
/
/      加上 WriteClassName 带有@type属性的⽂本将会被⾃动识别类型
JSONString(t, SerializerFeature.WriteClassName).getBytes(DEFAULT_CHARSET);
}
@Override
public T deserialize(byte[] bytes) throws SerializationException {
if (bytes == null || bytes.length <= 0) {
return null;
}
String str = new String(bytes, DEFAULT_CHARSET);
//        if(clazz!=null)
logger.info("缓存反序列化: "+(clazz!=Name():"clazz缺失"));
logger.info(clazz);
//        System.out.println("clazz:"+Name());
//        System.out.println("clazz:"+Name());
//        logger.info("缓存反序列化: "+T. ..getName());
//        if(clazz==null)
//            clazz=Class<Object.class>;
//            return  JSON.parseObject(str,SerializerFeature.
//        return (T) JSON.parseObject(str, clazz);
//        fastjson通过SerializerFeature.WriteClassName实现⾃省功能优化
//        带有@type属性的⽂本将会被⾃动识别类型,上⾯的⽂本可以这样处理:Employee x = (Employee) JSON.parse(text); //        return (T) JSON.parseObject(str,clazz);报错 com.alibaba.fastjson.JSONException: expect className
return (T) JSON.parse (str);
}
/*
fastjson怎么用protected GenericJackson2JsonRedisSerializer serializer = null;
public CustomGenericJackson2JsonRedisSerializerExt() {
ObjectMapper om = new ObjectMapper();
om.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
.addSerializer(new NullValueSerializer()));
this.serializer = new GenericJackson2JsonRedisSerializer(om);
}
public CustomGenericJackson2JsonRedisSerializerExt(ObjectMapper om) {
this.serializer = new GenericJackson2JsonRedisSerializer(om);
}
@Override
public byte[] serialize(Object o) throws SerializationException {
return serializer.serialize(o);
}
@Override
public Object deserialize(byte[] bytes) throws SerializationException {
return serializer.deserialize(bytes);
}
protected class NullValueSerializer extends StdSerializer<NullValue> {
private static final long serialVersionUID = 1999052150548658807L;
private final String classIdentifier="@class";
NullValueSerializer() {
super(NullValue.class);
}
public void serialize(NullValue value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
jgen.writeStartObject();
jgen.writeStringField(this.classIdentifier, Name());
jgen.writeEndObject();
}
}*/
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。