Closed tywo45 closed 7 months ago
https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2-extension/2.0.49-SNAPSHOT/ 已经支持,请帮忙用2.0.49-SNAPSHOT版本验证,2.0.49版本预计在明天(4月14日)发布
Maven依赖
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2-extension</artifactId>
<version>2.0.49-SNAPSHOT</version>
</dependency>
示例代码
@Test
public void json() throws Exception {
JSONCodec codec = new JSONCodec(Bean.class);
Bean bean = new Bean();
bean.name = "abc";
ByteBuf encoded = codec.getValueEncoder()
.encode(bean);
Bean decoded = (Bean) codec.getValueDecoder().decode(encoded, null);
assertEquals(bean.name, decoded.name);
}
@Test
public void jsonAutoType() throws Exception {
JSONCodec codec = new JSONCodec(
JSONFactory.createWriteContext(JSONWriter.Feature.WriteClassName),
JSONFactory.createReadContext(JSONReader.autoTypeFilter(Bean.class))
);
Bean bean = new Bean();
bean.name = "abc";
ByteBuf encoded = codec.getValueEncoder()
.encode(bean);
Bean decoded = (Bean) codec.getValueDecoder().decode(encoded, null);
assertEquals(bean.name, decoded.name);
}
@Test
public void jsonb() throws Exception {
JSONBCodec codec = new JSONBCodec(Bean.class);
Bean bean = new Bean();
bean.name = "abc";
ByteBuf encoded = codec.getValueEncoder()
.encode(bean);
Bean decoded = (Bean) codec.getValueDecoder().decode(encoded, null);
assertEquals(bean.name, decoded.name);
}
@Test
public void jsonbAutoType() throws Exception {
JSONBCodec codec = new JSONBCodec(
JSONFactory.createWriteContext(JSONWriter.Feature.WriteClassName),
JSONFactory.createReadContext(JSONReader.autoTypeFilter(Bean.class))
);
Bean bean = new Bean();
bean.name = "abc";
ByteBuf encoded = codec.getValueEncoder()
.encode(bean);
Bean decoded = (Bean) codec.getValueDecoder()
.decode(encoded, null);
assertEquals(bean.name, decoded.name);
}
public static class Bean {
public String name;
}
其中autoType的例子,为了避免自动类型带来的安全问题,手动配置JSONReader.autoTypeFilter是配置自动类型支持的类名前缀,参考 https://github.com/alibaba/fastjson2/wiki/fastjson2_autotype_cn 这里的第5点
请描述您的需求或者改进建议
对比过几大顶流序列化框架,FastJson的JSONB性能非常优异,想在redisson中使用FastJson的JSONB作为Codec
请描述你建议的实现方案
实现Redisson的接口Codec
描述您考虑过的替代方案
除了自己实现Codec,也可以和Redisson的开发人员沟通,让Redisson实现FastJson版的Redisson的Codec
附加信息
暂无