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

[BUG]fastjson2解析由fastjson1转换的带重复引用对象的字符串时解析为null #2072

Closed
cbib-guoguifang opened this issue Dec 4, 2023 · 2 comments
Labels
bug Something isn't working fixed
Milestone

Comments

@cbib-guoguifang
Copy link

问题描述

在使用fastjson1解析有重复引用对象的时候,会使用$ref来减少内容大小,由于下游系统使用的是fastjson1,上游系统使用的是fastjson2,fastjson2在解析fastjson1转换的字符串的时候会解析为null,造成业务系统报错

环境信息

请填写以下信息:

  • OS信息: [e.g.:CentOS 8.4.2105 4Core 3.10GHz 16 GB]
  • JDK信息: [e.g.:Openjdk 1.8.0_312]
  • 版本信息:[e.g.:Fastjson2 2.0.43]

重现步骤

public class TestFastJsonRef {
    
    public static void main(String[] args) {
        Test2 test21 = new Test2();
        test21.value = "test21";
        
        Test2 test22 = new Test2();
        test22.value = "test22";
        
        Test1 test1 = new Test1();
        test1.test11 = test21;
        test1.test12 = new HashMap<>(2);
        test1.test12.put("test21", test21);
        test1.test13 = new HashMap<>(2);
        test1.test13.put("test22", test22);
        
        String fastJson1Str = com.alibaba.fastjson.JSON.toJSONString(test1);
        System.out.println("使用fastjson1转换重复引用字符串结果: " + fastJson1Str);
        Test1 fastJson1Parse = com.alibaba.fastjson.JSON.parseObject(fastJson1Str, Test1.class);
        System.out.println("使用fastjson1解析转换后的字符串结果: " + com.alibaba.fastjson2.JSON.toJSONString(fastJson1Parse));
        Test1 fastJson2Parse = com.alibaba.fastjson2.JSON.parseObject(fastJson1Str, Test1.class);
        System.out.println("使用fastjson2解析转换后的字符串结果: " + com.alibaba.fastjson2.JSON.toJSONString(fastJson2Parse));
    }
    
    @Data
    private static class Test1 {
        
        private Test2 test11;
        
        private Map<String, Test2> test12;
        
        private Map<String, Test2> test13;
        
    }
    
    @Data
    private static class Test2 {
        
        private String value;
        
    }
    
}

期待的正确结果

fastjson2支持正确解析fastjson1转换的带$ref的字符串

相关日志输出

image

@cbib-guoguifang cbib-guoguifang added the bug Something isn't working label Dec 4, 2023
@wenshao wenshao added this to the 2.0.44 milestone Dec 5, 2023
@wenshao wenshao added the fixed label Dec 5, 2023
@wenshao
Copy link
Member

wenshao commented Dec 6, 2023

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.44-SNAPSHOT/
问题重现并且修复,请帮忙用2.0.44-SNAPSHOT版本验证,2.0.44版本预计在2024年元旦发布

@wenshao
Copy link
Member

wenshao commented Dec 24, 2023

@wenshao wenshao closed this as completed Dec 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed
Projects
None yet
Development

No branches or pull requests

2 participants