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]开启JSONWriter.Feature.ReferenceDetection进行序列化,结果却没有将重复引用替换成$ref #304

Closed
BottomCoder-Ander opened this issue May 22, 2022 · 2 comments
Labels
bug Something isn't working
Milestone

Comments

@BottomCoder-Ander
Copy link

BottomCoder-Ander commented May 22, 2022

问题描述

开启JSONWriter.Feature.ReferenceDetection进行序列化,结果却没有将重复引用替换成$ref

环境信息

  • OS信息: Win10 4Core 3.10GHz 16 GB
  • JDK信息: Openjdk 1.8.0_332
  • 版本信息:Fastjson2 2.0.1~2.0.4

重现步骤

运行下面测试代码,序列化结果并没有检测引用。预期应该是序列化成$refxxx的。如果使用fastjson (1.2.78)而不是fastjson2,就不会有这个问题。

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONWriter;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class Test {

    public static void main(String[] args){
        Test tt = new Test();
        tt.setTest(null);
        tt.setName("testtt");

        List<Test> list = new LinkedList<>();
        for(int i = 0; i < 4; i++) {
            Test t = new Test();
            t.setTest(tt);
            t.setName("test");
            list.add(t);
        }

        JSONWriter.Feature[] features = {
                JSONWriter.Feature.ReferenceDetection,
                //JSONWriter.Feature.FieldBased
        };
        String result = JSON.toJSONString(list,features );
        System.out.println(result);
    }

    private String name;
    private Test test;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Test getTest() {
        return test;
    }

    public void setTest(Test test) {
        this.test = test;
    }
}

预期结果

[{"name":"test","test":{"name":"testtt"}},{"name":"test","test":{"$ref":"$[0].test"}},{"name":"test","test":{"$ref":"$[0].test"}},{"name":"test","test":{"$ref":"$[0].test"}}]

实际结果

[{"name":"test","test":{"name":"testtt"}},{"name":"test","test":{"name":"testtt"}},{"name":"test","test":{"name":"testtt"}},{"name":"test","test":{"name":"testtt"}}]
@BottomCoder-Ander BottomCoder-Ander added the bug Something isn't working label May 22, 2022
@BottomCoder-Ander BottomCoder-Ander changed the title 开启JSONWriter.Feature.ReferenceDetection进行序列化,结果却没有将重复引用替换成$ref [Bug] [Bug]开启JSONWriter.Feature.ReferenceDetection进行序列化,结果却没有将重复引用替换成$ref May 22, 2022
@BottomCoder-Ander BottomCoder-Ander changed the title [Bug]开启JSONWriter.Feature.ReferenceDetection进行序列化,结果却没有将重复引用替换成$ref [BUG]开启JSONWriter.Feature.ReferenceDetection进行序列化,结果却没有将重复引用替换成$ref May 22, 2022
@wenshao wenshao added this to the 2.0.5 milestone May 22, 2022
wenshao added a commit that referenced this issue May 22, 2022
wenshao added a commit that referenced this issue May 22, 2022
wenshao added a commit that referenced this issue May 22, 2022
wenshao added a commit that referenced this issue May 22, 2022
@wenshao
Copy link
Member

wenshao commented May 22, 2022

感谢反馈,问题已经修复,你可以用2.0.5-SNAPSHOT验证
https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.5-SNAPSHOT/

@wenshao
Copy link
Member

wenshao commented May 28, 2022

https://github.com/alibaba/fastjson2/releases/tag/2.0.5
问题已修复,请用新版本

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

No branches or pull requests

2 participants