对于json 解析, 除了jackson之外, fastjson 也是一种常用的解析方式。虽然SDR 并为直接提供Fastjson 的序列化方式, 但是fastjson 却提供了两个RedisSerializer 的实现类, 我们可以直接整合使用.
1. FastJson 提供的RedisSerializer 实现类
fastjson 为RedisSerializer 接口提供了两个实现类GenericFastJsonRedisSerializer 和 FastJsonRedisSerializer, 位于包com.alibaba.fastjson.support.spring 下.
- GenericFastJsonRedisSerializer: 不支持设置FastJsonConfig 特性, 生成json串会多一个type参数, type的值为class 名称
- FastJsonRedisSerializer: 支持设置FastJsonCofing 特性, 默认生成json串不包含type属性
1.1 GenericFastJsonRedisSerializer
public class GenericFastJsonRedisSerializer implements RedisSerializer<Object> {
private final static ParserConfig defaultRedisConfig = new ParserConfig();
static { defaultRedisConfig.setAutoTypeSupport(true);}
public byte[] serialize(Object object) throws SerializationException {
if (object == null) {
return new byte[0];
}
try {
return JSON.toJSONBytes(object, SerializerFeature.WriteClassName);
} catch (Exception ex) {
throw new SerializationException("Could not serialize: " + ex.getMessage(), ex);
}
}
public Object deserialize(byte[] bytes) throws SerializationException {
if (bytes == null || bytes.length == 0) {
return null;
}
try {
return JSON.parseObject(new String(bytes, IOUtils.UTF8), Object.class, defaultRedisConfig);
} catch (Exception ex) {
throw new SerializationException("Could not deserialize: " + ex.getMessage(), ex);
}
}
}
1.2 FastJsonRedisSerializer
public class FastJsonRedisSerializer<T> implements RedisSerializer<T> {
private FastJsonConfig fastJsonConfig = new FastJsonConfig();
private Class<T> type;
public FastJsonRedisSerializer(Class<T> type) {
this.type = type;
}
public FastJsonConfig getFastJsonConfig() {
return this.fastJsonConfig;
}
public void setFastJsonConfig(FastJsonConfig fastJsonConfig) {
this.fastJsonConfig = fastJsonConfig;
}
public byte[] serialize(T t) throws SerializationException {
if (t == null) {
return new byte[0];
} else {
try {
return JSON.toJSONBytes(t, this.fastJsonConfig.getSerializeConfig(), this.fastJsonConfig.getSerializerFeatures());
} catch (Exception var3) {
throw new SerializationException("Could not serialize: " + var3.getMessage(), var3);
}
}
}
public T deserialize(byte[] bytes) throws SerializationException {
if (bytes != null && bytes.length != 0) {
try {
return JSON.parseObject(bytes, this.type, this.fastJsonConfig.getFeatures());
} catch (Exception var3) {
throw new SerializationException("Could not deserialize: " + var3.getMessage(), var3);
}
} else {
return null;
}
}
}
2. 集成fastjson 序列化方式
2.1 引入依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.54</version>
</dependency>
2.2 配置自定义RedisTemplate
- 笔者对key设置String 序列化方式, 对value 设置为fastjson 序列化方式
- 笔者使用GenericJackson2JsonRedisSerializer 序列化方式
@Bean("fastJsonRedisTemplate")
public RedisTemplate<String, Object> fastJsonRedisTemplate(RedisConnectionFactory redisConnectionFactory)throws UnknownHostException {
RedisTemplate<String,Object> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory);
template.setKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new GenericFastJsonRedisSerializer());
template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
return template;
}
2.3 测试用例
public class Test_fastJsonRedisTemplate {
@Autowired
private RedisTemplate fastJsonRedisTemplate;
@Test
public void test_set() {
ValueOperations ops = fastJsonRedisTemplate.opsForValue();
UserPO userPO = new UserPO("zhangsan", "123456", 20);
ops.set("zhangsan", userPO);
}
@Test
public void test_get(){
ValueOperations ops = fastJsonRedisTemplate.opsForValue();
UserPO userPO = (UserPO) ops.get("zhangsan");
System.out.println(userPO);
}
}
2.4 Redis 内存储
通过命令行查看redis存储
127.0.0.1:6379> get zhangsan
"{\"@type\":\"org.zongf.learn.sdr.l01.UserPO\",\"age\":20,\"password\":\"123456\",\"username\":\"zhangsan\"}"