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

JSONWriter.Feature.ReferenceDetection重复引用序列化错误 #2712

Closed
zero20121222 opened this issue Jun 18, 2024 · 1 comment
Closed
Assignees
Labels
bug Something isn't working fixed
Milestone

Comments

@zero20121222
Copy link

问题描述

在使用fastjson替换jackson的全局处理
配置如下

FastJsonConfig config = new FastJsonConfig();
    config.setWriterFeatures(
    JSONWriter.Feature.ReferenceDetection, // 处理循环引用和树形结构的处理
    JSONWriter.Feature.WriteEnumsUsingName, // 序列化enum使用name
    JSONWriter.Feature.BrowserCompatible, // 兼容IE6
    JSONWriter.Feature.BrowserSecure // 浏览器安全,将会’<’ ‘>’ ‘(’ ')'字符做转义输出
);

处理过程中在重复引用的处理上会出现,key丢失的情况,如下:
"companyCorporation": "黄明望"{
"$ref": "$.result.data[0].contacts"
}
正确结果应该是
"companyCorporation": "黄明望",
"contacts":{
"$ref": "$.result.data[0].contacts"
}

复现场景

@Data
public class ClueListInfo implements Serializable {
    private static final long serialVersionUID = 8170584149020082450L;

    private Long clueId;

    private String name;

    private List<ContactInfo> contacts;
}

@Data
public class ContactInfo implements Serializable {
    private static final long serialVersionUID = -5313505758293424804L;
    
    private Long id;
    
    private String name;
}

public class Issue2687 {
    @Test
    void test(){
        ClueListInfo info1 = new ClueListInfo();
        info1.setClueId(1L);
        info1.setName("Clue 01");

        ClueListInfo info2 = new ClueListInfo();
        info2.setClueId(2L);
        info2.setName("Clue 02");

        ContactInfo contactInfo = new ContactInfo();
        contactInfo.setId(1L);
        contactInfo.setName("contact");
        ArrayList<ContactInfo> contacts = Lists.newArrayList(contactInfo);

        info1.setContacts(contacts);
        info2.setContacts(contacts);
        List infos = Lists.newArrayList(info1,info2);

        String jsonString = JSON.toJSONString(infos, JSONWriter.Feature.ReferenceDetection);
        System.out.println(jsonString);

    }
}

需要的结果

[{"clueId":1,"contacts":[{"id":1,"name":"contact"}],"name":"Clue 01"},{"clueId":2,"contacts":{"$ref":"$[0].contacts"},"name":"Clue 02"}]

实际结果

[{"clueId":1,"contacts":[{"id":1,"name":"contact"}],"name":"Clue 01"},{"clueId":2{"$ref":"$[0].contacts"},"name":"Clue 02"}]
@zero20121222 zero20121222 added the bug Something isn't working label Jun 18, 2024
@yanxutao89 yanxutao89 self-assigned this Jun 19, 2024
wenshao pushed a commit that referenced this issue Jun 21, 2024
* fix add field name write for list value, for issue #2712
@wenshao wenshao added this to the 2.0.52 milestone Jun 21, 2024
@wenshao wenshao added the fixed label Jun 21, 2024
@wenshao
Copy link
Member

wenshao commented Jul 14, 2024

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

3 participants