alibaba / fastjson2

🚄 FASTJSON2 is a Java JSON library with excellent performance.
Apache License 2.0
3.79k stars 495 forks source link

[BUG]JSONWriter.Feature.ReferenceDetection重复引用序列化错误 #3162

Open feijiangjun123 opened 3 days ago

feijiangjun123 commented 3 days ago

问题描述

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

改bug再2.0.52已修复,在兼容包2.0.53依然存在

希望兼容包也修复该问题

环境信息

请填写以下信息:

重现步骤

@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"}]

附加信息

image