|
|
|
|
@ -1,34 +1,27 @@
|
|
|
|
|
package com.ruoyi.framework.config;
|
|
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
|
|
import com.alibaba.fastjson.serializer.SerializerFeature;
|
|
|
|
|
import com.fasterxml.jackson.databind.JavaType;
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
import com.fasterxml.jackson.databind.type.TypeFactory;
|
|
|
|
|
import com.alibaba.fastjson2.JSON;
|
|
|
|
|
import com.alibaba.fastjson2.JSONReader;
|
|
|
|
|
import com.alibaba.fastjson2.JSONWriter;
|
|
|
|
|
import com.alibaba.fastjson2.filter.Filter;
|
|
|
|
|
import com.ruoyi.common.constant.Constants;
|
|
|
|
|
import org.springframework.data.redis.serializer.RedisSerializer;
|
|
|
|
|
import org.springframework.data.redis.serializer.SerializationException;
|
|
|
|
|
import com.alibaba.fastjson.parser.ParserConfig;
|
|
|
|
|
import org.springframework.util.Assert;
|
|
|
|
|
|
|
|
|
|
import java.nio.charset.Charset;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Redis使用FastJson序列化
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @author ruoyi
|
|
|
|
|
*/
|
|
|
|
|
public class FastJson2JsonRedisSerializer<T> implements RedisSerializer<T>
|
|
|
|
|
{
|
|
|
|
|
@SuppressWarnings("unused")
|
|
|
|
|
private ObjectMapper objectMapper = new ObjectMapper();
|
|
|
|
|
|
|
|
|
|
public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
|
|
|
|
|
|
|
|
|
|
private Class<T> clazz;
|
|
|
|
|
static final Filter AUTO_TYPE_FILTER = JSONReader.autoTypeFilter(Constants.JSON_WHITELIST_STR);
|
|
|
|
|
|
|
|
|
|
static
|
|
|
|
|
{
|
|
|
|
|
ParserConfig.getGlobalInstance().setAutoTypeSupport(true);
|
|
|
|
|
}
|
|
|
|
|
private Class<T> clazz;
|
|
|
|
|
|
|
|
|
|
public FastJson2JsonRedisSerializer(Class<T> clazz)
|
|
|
|
|
{
|
|
|
|
|
@ -43,7 +36,7 @@ public class FastJson2JsonRedisSerializer<T> implements RedisSerializer<T>
|
|
|
|
|
{
|
|
|
|
|
return new byte[0];
|
|
|
|
|
}
|
|
|
|
|
return JSON.toJSONString(t, SerializerFeature.WriteClassName).getBytes(DEFAULT_CHARSET);
|
|
|
|
|
return JSON.toJSONString(t, JSONWriter.Feature.WriteClassName).getBytes(DEFAULT_CHARSET);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
@ -55,17 +48,6 @@ public class FastJson2JsonRedisSerializer<T> implements RedisSerializer<T>
|
|
|
|
|
}
|
|
|
|
|
String str = new String(bytes, DEFAULT_CHARSET);
|
|
|
|
|
|
|
|
|
|
return JSON.parseObject(str, clazz);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setObjectMapper(ObjectMapper objectMapper)
|
|
|
|
|
{
|
|
|
|
|
Assert.notNull(objectMapper, "'objectMapper' must not be null");
|
|
|
|
|
this.objectMapper = objectMapper;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected JavaType getJavaType(Class<?> clazz)
|
|
|
|
|
{
|
|
|
|
|
return TypeFactory.defaultInstance().constructType(clazz);
|
|
|
|
|
return JSON.parseObject(str, clazz, AUTO_TYPE_FILTER);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|