package fr.falum.cache;
/**
* Created by zhangsong on 2019/7/2.
*/
/*
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.module.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.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;
import java.io.IOException;
import java.nio.charset.Charset;
public class CustomFastJsonGenericJackson2JsonRedisSerializerExt<T> implements RedisSerializer<T> {
Logger logger=Logger.getLogger(getClass());
public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
static {
// ParserConfig.getGlobalInstance().addAccept("");
}
private Class<T> clazz;
public CustomFastJsonGenericJackson2JsonRedisSerializerExt(){
// this(Object.class);
super();
}
public CustomFastJsonGenericJackson2JsonRedisSerializerExt(Class<T> clazz) {
super();
logger.warn("缓存初始化参数:"+clazz.getName());
this.clazz = clazz;
}
@Override
public byte[] serialize(T t) throws SerializationException {
if (t == null) {
return new byte[0];
}
// 加上 WriteClassName 带有@type属性的文本将会被自动识别类型
return JSON.toJSONString(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!=null?clazz.getName():"clazz缺失"));
logger.info(clazz);
// System.out.println("clazz:"+clazz.getName());
// 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);
}
/*
protected GenericJackson2JsonRedisSerializer serializer = null;
public CustomGenericJackson2JsonRedisSerializerExt() {
ObjectMapper om = new ObjectMapper();
om.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
om.registerModule(new JavaTimeModule());
om.registerModule((new SimpleModule())
.addSerializer(new NullValueSerializer()));
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
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, NullValue.class.getName());
jgen.writeEndObject();
}
}*/
}
redis缓存序列化与反序列化使用fastjson
最新推荐文章于 2024-10-07 17:49:53 发布