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]序列化对象中包含自身引用的场景下与 fastjson1不兼容 #3144

Closed
fantasy1713 opened this issue Oct 31, 2024 · 2 comments
Closed
Labels
bug Something isn't working fixed
Milestone

Comments

@fantasy1713
Copy link

问题描述

简要描述您碰到的问题。

环境信息

请填写以下信息:

  • 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.52]

使用fastjson2反序列化对象,当对象持有自身this引用时,序列化的占位符是“..”,与fastjson1行为不兼容

{"id":"abc","self":{"$ref":".."}}

public class DN {
    private DN self;
    private String id;

    public DN getSelf() {
        return this.self;
    }

    public void setSelf(DN self) {
        this.self = self;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
}

 @Test
    public void testFastjson2SelfRef(){
        JSON.config(JSONWriter.Feature.ReferenceDetection,true);
        DN dn = new DN();
        dn.setId("abc");
        dn.setSelf(dn);
        String jsonString  = JSON.toJSONString(dn);
        System.out.println(jsonString);  // {"id":"abc","self":{"$ref":".."}}
    }

    @Test
    public void testFastjson1SelfRef(){
        DN dn = new DN();
        dn.setId("abc");
        dn.setSelf(dn);
        String jsonString  = com.alibaba.fastjson.JSON.toJSONString(dn);
        System.out.println(jsonString);  // {"id":"abc","self":{"$ref":"@"}}
    }

期待的正确结果

序列化后自身引用使用@占位符
{"id":"abc","self":{"$ref":"@"}}

相关日志输出

请复制并粘贴任何相关的日志输出。

附加信息

如果你还有其他需要提供的信息,可以在这里填写(可以提供截图、视频等)。

@fantasy1713 fantasy1713 added the bug Something isn't working label Oct 31, 2024
@wenshao wenshao added the fixed label Nov 2, 2024
@wenshao wenshao added this to the 2.0.54 milestone Nov 2, 2024
@wenshao
Copy link
Member

wenshao commented Nov 2, 2024

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson/2.0.54-SNAPSHOT/
问题已修复,请帮忙用2.0.54-SNAPSHOT快照版本验证,2.0.54正式版本预计在月底发布

@wenshao
Copy link
Member

wenshao commented Jan 12, 2025

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

@wenshao wenshao closed this as completed Jan 12, 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