Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

提供Redisson的Codec #2420

Closed
tywo45 opened this issue Apr 9, 2024 · 2 comments
Closed

提供Redisson的Codec #2420

tywo45 opened this issue Apr 9, 2024 · 2 comments
Labels
enhancement New feature or request fixed
Milestone

Comments

@tywo45
Copy link

tywo45 commented Apr 9, 2024

请描述您的需求或者改进建议

对比过几大顶流序列化框架,FastJson的JSONB性能非常优异,想在redisson中使用FastJson的JSONB作为Codec

请描述你建议的实现方案

实现Redisson的接口Codec
image

描述您考虑过的替代方案

除了自己实现Codec,也可以和Redisson的开发人员沟通,让Redisson实现FastJson版的Redisson的Codec

附加信息

暂无

@tywo45 tywo45 added the enhancement New feature or request label Apr 9, 2024
@wenshao wenshao added this to the 2.0.49 milestone Apr 10, 2024
@wenshao
Copy link
Member

wenshao commented Apr 13, 2024

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点

@wenshao wenshao added the fixed label Apr 13, 2024
@wenshao
Copy link
Member

wenshao commented Apr 14, 2024

@wenshao wenshao closed this as completed Apr 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request fixed
Projects
None yet
Development

No branches or pull requests

2 participants