Skip to content

[BUG]fastjson2无法反序列化包含$ref的数组字符串 #3436

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

Open
LYoung93 opened this issue Mar 22, 2025 · 1 comment
Open

[BUG]fastjson2无法反序列化包含$ref的数组字符串 #3436

LYoung93 opened this issue Mar 22, 2025 · 1 comment
Labels
bug Something isn't working fixed
Milestone

Comments

@LYoung93
Copy link

问题描述

fastjson1.x将对象数组序列化后(包含重复引用,故序列化后字符串包含$ref),使用fastjson2反序列化对象,引用对象无法反序列化

环境信息

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

重现步骤

如何操作可以重现该问题:

1.创建一个包含重复对象的List,使用 com.alibaba.fastjson.JSON#toJSONString方法序列化
2.使用 com.alibaba.fastjson2.JSON#parseArray,进行反序列化
3.出现反序列化后的list,不包含重复对象

public class Fastjson2Test {
    @Getter
    @Setter
    public static class Bean {
        private double a;
        private double b;
        private double c;
    }

    public static void main(String[] args) {
        // 序列化
        Bean bean = new Bean();
        bean.setA(1.0);
        bean.setB(2.0);
        bean.setC(3.0);

        List<Bean> beanList = new ArrayList<>();
        beanList.add(bean);
        beanList.add(bean);

        // jsonStr1="[{\"a\":1.0,\"b\":2.0,\"c\":3.0},{\"$ref\":\"$[0]\"}]"
        String json1 = com.alibaba.fastjson.JSON.toJSONString(beanList);

        // 反序列化,json2Bean数组无法还原为beanList
        List<Bean> json2Bean = com.alibaba.fastjson2.JSON.parseArray(json1, Bean.class,
            com.alibaba.fastjson2.JSONReader.Feature.SupportSmartMatch);
        // 结果为[{"a":1.0,"b":2.0,"c":3.0},{"a":0.0,"b":0.0,"c":0.0}]
        com.alibaba.fastjson2.JSON.toJSONString(json2Bean);
    }
}

期待的正确结果

对您期望发生的结果进行清晰简洁的描述。
json2Bean数组可以还原为beanList,数组内容一致

@LYoung93 LYoung93 added the bug Something isn't working label Mar 22, 2025
@wenshao wenshao added this to the 2.0.57 milestone Mar 31, 2025
@wenshao wenshao added fixed and removed fixed labels Mar 31, 2025
@wenshao wenshao modified the milestones: 2.0.57, 2.0.58 Mar 31, 2025
@wenshao
Copy link
Member

wenshao commented Apr 1, 2025

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.58-SNAPSHOT/
问题已修复,请帮忙用2.0.58-SNAPSHOT版本验证,2.0.58版本预计在5月初发布。

@wenshao wenshao added the fixed label Apr 1, 2025
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