fastJson 解決 把對象轉化成json避免$ref

轉:http://blog.csdn.net/mephistodemon1/article/details/19118493

DisableCircularReferenceDetect來禁止循環引用檢測:

JSON.toJSONString(…, SerializerFeature.DisableCircularReferenceDetect)
當進行toJSONString的時候,默認如果重用對象的話,會使用引用的方式進行引用對象。

"顏色": [
      {
        "$ref": "$.itemSkuList[0].itemSpecificationList[0]"
      }, 
      {
        "$ref": "$.itemSkuList[1].itemSpecificationList[0]"
      }
    ]

循環引用

很多場景中,我們需要序列化的對象中存在循環引用,在許多的json庫中,這會導致stackoverflow。在功能強大的fastjson中,你不需要擔心這個問題。例如:

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());

引用是通過”ref ref”:”..” 上一級
ref:@ ref":" ref":"$.children.0” 基於路徑的引用,相當於 root.getChildren().get(0)

使用fastjson 可以避免以上的空對象數據出現!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章