From 4504e7206a1aed8c094867ae2d5b0b03ccd3e091 Mon Sep 17 00:00:00 2001 From: Nishant Gupta Date: Thu, 14 Oct 2021 16:15:14 -0400 Subject: [PATCH] Issue:632 - Improved upon the description of the error message for cylic dependency --- src/main/java/org/json/JSONObject.java | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/json/JSONObject.java b/src/main/java/org/json/JSONObject.java index 59f84e3c3..f615d23de 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -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 null + * @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(); } else { @@ -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 @@ -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"); + } + }