问题描述
在使用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"}]
问题描述
在使用fastjson替换jackson的全局处理
配置如下
处理过程中在重复引用的处理上会出现,key丢失的情况,如下:
"companyCorporation": "黄明望"{
"$ref": "$.result.data[0].contacts"
}
正确结果应该是
"companyCorporation": "黄明望",
"contacts":{
"$ref": "$.result.data[0].contacts"
}
复现场景
需要的结果
[{"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"}]