This article is participating in “Java Theme Month – Java Debug Notes Event”, see < Event link > for more details.

Abnormal problem

While we were working with JSON strings, I encountered the following exception:

net.sf.json.JSONException: There is a cycle in the hierarchy! at net.sf.json.util.CycleDetectionStrategy$StrictCycleDetectionStrategy.handleRepeatedReferenceAsObject(CycleDetectionStrat egy.java:97) at net.sf.json.JSONObject._fromBean(JSONObject.java:657) at net.sf.json.JSONObject.fromObject(JSONObject.java:172) at net.sf.json.AbstractJSON._processValue(AbstractJSON.java:274) at net.sf.json.JSONObject._processValue(JSONObject.java:2655) at net.sf.json.JSONObject.processValue(JSONObject.java:2721) at net.sf.json.JSONObject.setInternal(JSONObject.java:2736) at  net.sf.json.JSONObject.setValue(JSONObject.java:1424) at net.sf.json.JSONObject.defaultBeanProcessing(JSONObject.java:765) at net.sf.json.JSONObject._fromBean(JSONObject.java:699) at net.sf.json.JSONObject.fromObject(JSONObject.java:172) at net.sf.json.AbstractJSON._processValue(AbstractJSON.java:274) at net.sf.json.JSONArray._processValue(JSONArray.java:2513) at net.sf.json.JSONArray.processValue(JSONArray.java:2538) at net.sf.json.JSONArray.addValue(JSONArray.java:2525) at net.sf.json.JSONArray._fromCollection(JSONArray.java:1056) at net.sf.json.JSONArray.fromObject(JSONArray.java:123) at net.sf.json.AbstractJSON._processValue(AbstractJSON.java:240) at  net.sf.json.JSONObject._processValue(JSONObject.java:2655) at net.sf.json.JSONObject.processValue(JSONObject.java:2721)  at net.sf.json.JSONObject.setInternal(JSONObject.java:2736) at net.sf.json.JSONObject.setValue(JSONObject.java:1424) at  net.sf.json.JSONObject.defaultBeanProcessing(JSONObject.java:765) at net.sf.json.JSONObject._fromBean(JSONObject.java:699) at net.sf.json.JSONObject.fromObject(JSONObject.java:172) at net.sf.json.AbstractJSON._processValue(AbstractJSON.java:274) at net.sf.json.JSONArray._processValue(JSONArray.java:2513) at net.sf.json.JSONArray.processValue(JSONArray.java:2538) at net.sf.json.JSONArray.addValue(JSONArray.java:2525) at net.sf.json.JSONArray.element(JSONArray.java:1724) at net.sf.json.JSONArray.add(JSONArray.java:1249) at net.sf.json.JSONArray.add(JSONArray.java:1245)Copy the code

Cause analysis,

Since JSONObject internally disassembles the object you pass in indefinitely until there is nothing left to disassemble, an infinite loop of calls occurs when parsing beans, that is, multiple beans call each other. If the objects you pass in have foreign keys, or reference each other, then the inner loop will be infinite and this exception resolution will be thrown. For example, with Hibernate, objects in queries have multiple table dependency associations.

The solution

The resulting data is filtered to remove properties in the bean that cause an infinite loop:

List<DataObject> list= this.baseService.find(xxx); // result data list DataObject: DataObject
		
// Custom JsonConfig for filtering recursive data generated by Hibernate configuration files
JsonConfig config = new JsonConfig();
config.setExcludes(new String[]{"a"."b"}); // Specify which fields and objects to filter
JSONArray result = JSONArray.fromObject(list, config);
Copy the code