Skip to content

Commit

Permalink
Issue:632 - Improved upon the description of the error message for cy…
Browse files Browse the repository at this point in the history
…lic dependency
  • Loading branch information
Nishant Gupta committed Oct 14, 2021
1 parent ded5f53 commit 4504e72
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/main/java/org/json/JSONObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,9 @@ public JSONObject(JSONTokener x) throws JSONException {
* the JSONObject.
* @throws JSONException If a value in the map is non-finite number.
* @throws NullPointerException If a key in the map is <code>null</code>
* @throws JSONException If there exists a cyclic dependency in the object provided.
*/
public JSONObject(Map<?, ?> m) {
public JSONObject(Map<?, ?> m) throws JSONException {
if (m == null) {
this.map = new HashMap<String, Object>();
} else {
Expand Down Expand Up @@ -2563,7 +2564,7 @@ && isValidMethodName(method.getName())) {
}

if (setOfInstanceVariables.contains(result)) {
throw new JSONException("Cyclic Dependency Detected");
throw cyclicDependencyFormatException(key);
}

//adding the currently checked object to the ancestor path
Expand All @@ -2582,4 +2583,15 @@ && isValidMethodName(method.getName())) {
}
}
}

/**
* Create a new JSONException in a common format for cyclic dependency.
*
* @return JSONException that can be thrown.
*/
private static JSONException cyclicDependencyFormatException(String key) {
return new JSONException(
"JSONObject[" + quote(key) + "] cannot be written because of a Cyclic Dependency");
}

}

0 comments on commit 4504e72

Please sign in to comment.