4
fastjson.version = 1.2.75
- 创建两个 Person 类实例,并引用同一个 HashMap 实例
Map<String, String> roles = new HashMap<>();
roles.put("1", "Master");
roles.put("2", "Student");
Person p1 = new Person(roles);
Person p2 = new Person(roles);
- 分别序列化,没有问题
System.out.println(JSON.toJSONString(p1)); System.out.println(JSON.toJSONString(p2));
{"roles":{"1":"Master","2":"Student"}} {"roles":{"1":"Master","2":"Student"}}
-
将两个对象一起放入同一个 LinkedList 之后再序列化,BUG happens...
List<Person> oneGroupOfPeople = new LinkedList<>(); oneGroupOfPeople.add(p1); oneGroupOfPeople.add(p2); System.out.println(JSON.toJSONString(oneGroupOfPeople));
[{"roles":{"1":"Master","2":"Student"}},{"roles":{"$ref":"$[0].roles"}}]
Person 类的定义
@Data
@AllArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
static class Person {
Map<String, String> roles;
}