Skip to content

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

@LYoung93

Description

@LYoung93

问题描述

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,数组内容一致

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfixed

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions