曹耘豪的博客

fastjson之ref

  1. 问题代码

问题代码

先看下面的代码,会输出什么

1
2
3
4
5
6
Object obj = Map.of("a", 1);
List<Object> arr = List.of(obj, obj);

ObjectMapper objectMapper = new ObjectMapper();
System.out.println(objectMapper.writeValueAsString(arr));
System.out.println(JSON.toJSONString(arr));

输出:

1
2
[{"a":1},{"a":1}]
[{"a":1},{"$ref":"$[0]"}]

如何解决ref问题

1
System.out.println(JSON.toJSONString(arr, SerializerFeature.DisableCircularReferenceDetect));

输出

1
[{"a":1},{"a":1}]
   /