Turn: fastjson converts objects to json to avoid $ref
DisableCircularReferenceDetect to disable circular reference detection:
JSON.toJSONString(..., SerializerFeature.DisableCircularReferenceDetect)
When toJSONString, by default, if the object is reused, the reference method will be used to refer to the object.
However, in some cases it does not apply. For example, when passing a value to the front-end JavaScript, you cannot use a reference object, and you have to pass the original value.
"color" : [ { "$ref": "$.itemSkuList[0].itemSpecificationList[0]" }, { "$ref": "$.itemSkuList[1].itemSpecificationList[0]" } ]
circular reference
In many scenarios, there are circular references in the objects we need to serialize, which can lead to stackoverflow in many json libraries. In powerful fastjson you don't need to worry about this. E.g:
A a = new A(); B b = new B(a); a.setB(b); String text = JSON.toJSONString(a); //{"b":{"a":{"$ref":".."}}} A a1 = JSON.parseObject(text, A.class); Assert.assertTrue(a1 == a1.getB().getA());
References are denoted by "$ref"
quote | describe |
---|---|
"$ref":".." | upper level |
"$ref":"@" | The current object, which is self-referencing |
"$ref":"$" | root object |
"$ref":"$.children.0" | Path-based reference, equivalent to root.getChildren().get(0) |
DisableCircularReferenceDetect to disable circular reference detection:
JSON.toJSONString(..., SerializerFeature.DisableCircularReferenceDetect)
When toJSONString, by default, if the object is reused, the reference method will be used to refer to the object.