From 097a401f3f38f7ac8ec6b4cc28fca1b6486f6d48 Mon Sep 17 00:00:00 2001 From: Aditya Purohit Date: Sun, 19 Nov 2023 09:11:32 -0400 Subject: [PATCH 01/13] refactor: rename variable boolean 'b' to 'isEndOfPair' in CookieList.toString() --- src/main/java/org/json/CookieList.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/json/CookieList.java b/src/main/java/org/json/CookieList.java index 8ad8b58..03e54b9 100644 --- a/src/main/java/org/json/CookieList.java +++ b/src/main/java/org/json/CookieList.java @@ -46,19 +46,19 @@ public class CookieList { * @throws JSONException if a called function fails */ public static String toString(JSONObject jo) throws JSONException { - boolean b = false; + boolean isEndOfPair = false; final StringBuilder sb = new StringBuilder(); // Don't use the new entrySet API to maintain Android support for (final String key : jo.keySet()) { final Object value = jo.opt(key); if (!JSONObject.NULL.equals(value)) { - if (b) { + if (isEndOfPair) { sb.append(';'); } sb.append(Cookie.escape(key)); sb.append("="); sb.append(Cookie.escape(value.toString())); - b = true; + isEndOfPair = true; } } return sb.toString(); From 75419e3f257af9c365b0ef3e5f4428a335564f8d Mon Sep 17 00:00:00 2001 From: Aditya Purohit Date: Sun, 19 Nov 2023 09:21:05 -0400 Subject: [PATCH 02/13] refactor: introduce explaining variable 'indentationSuffix' in XML.toString() --- src/main/java/org/json/XML.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/json/XML.java b/src/main/java/org/json/XML.java index 04c8bcd..a94c3fc 100644 --- a/src/main/java/org/json/XML.java +++ b/src/main/java/org/json/XML.java @@ -847,14 +847,14 @@ public class XML { string = (object == null) ? "null" : escape(object.toString()); - + String indentationSuffix = (indentFactor > 0) ? "\n" : ""; if(tagName == null){ - return indent(indent) + "\"" + string + "\"" + ((indentFactor > 0) ? "\n" : ""); + return indent(indent) + "\"" + string + "\"" + indentationSuffix; } else if(string.length() == 0){ - return indent(indent) + "<" + tagName + "/>" + ((indentFactor > 0) ? "\n" : ""); + return indent(indent) + "<" + tagName + "/>" + indentationSuffix; } else { return indent(indent) + "<" + tagName - + ">" + string + "" + ((indentFactor > 0) ? "\n" : ""); + + ">" + string + "" + indentationSuffix; } } From 7f1cb8bf62015016d4b02879b00cc7477b62c570 Mon Sep 17 00:00:00 2001 From: Aditya Purohit Date: Sun, 19 Nov 2023 09:51:44 -0400 Subject: [PATCH 03/13] refactor: decompose condition of digit checks by using extra method 'isNumericChar(...)' in NumberConversionUtil. --- src/main/java/org/json/NumberConversionUtil.java | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/json/NumberConversionUtil.java b/src/main/java/org/json/NumberConversionUtil.java index d53c5e2..30ca74d 100644 --- a/src/main/java/org/json/NumberConversionUtil.java +++ b/src/main/java/org/json/NumberConversionUtil.java @@ -24,7 +24,7 @@ class NumberConversionUtil { val = "-0."+val.substring(2); } char initial = val.charAt(0); - if ((initial >= '0' && initial <= '9') || initial == '-' ) { + if ( isNumericChar(initial) || initial == '-' ) { // decimal representation if (isDecimalNotation(val)) { // Use a BigDecimal all the time so we keep the original @@ -53,13 +53,13 @@ class NumberConversionUtil { initial = val.charAt(0); if(initial == '0' && val.length() > 1) { char at1 = val.charAt(1); - if(at1 >= '0' && at1 <= '9') { + if(isNumericChar(at1)) { throw new NumberFormatException("val ["+input+"] is not a valid number."); } } else if (initial == '-' && val.length() > 2) { char at1 = val.charAt(1); char at2 = val.charAt(2); - if(at1 == '0' && at2 >= '0' && at2 <= '9') { + if(at1 == '0' && isNumericChar(at2)) { throw new NumberFormatException("val ["+input+"] is not a valid number."); } } @@ -83,6 +83,16 @@ class NumberConversionUtil { throw new NumberFormatException("val ["+input+"] is not a valid number."); } + /** + * Checks if the character is a numeric digit ('0' to '9'). + * + * @param c The character to be checked. + * @return true if the character is a numeric digit, false otherwise. + */ + private static boolean isNumericChar(char c) { + return (c >= '0' && c <= '9'); + } + /** * Checks if the value could be considered a number in decimal number system. * @param value From aba82d9cc474c13ee981737432af98c19bb6f5b2 Mon Sep 17 00:00:00 2001 From: Aditya Purohit Date: Tue, 28 Nov 2023 02:56:10 +0000 Subject: [PATCH 04/13] isNumericChar() - switch comparison order --- src/main/java/org/json/NumberConversionUtil.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/json/NumberConversionUtil.java b/src/main/java/org/json/NumberConversionUtil.java index 30ca74d..c2f16d7 100644 --- a/src/main/java/org/json/NumberConversionUtil.java +++ b/src/main/java/org/json/NumberConversionUtil.java @@ -90,7 +90,7 @@ class NumberConversionUtil { * @return true if the character is a numeric digit, false otherwise. */ private static boolean isNumericChar(char c) { - return (c >= '0' && c <= '9'); + return (c <= '9' && c >= '0'); } /** From 7cbeb35498798dad51c7d8bd0e904858b1b91074 Mon Sep 17 00:00:00 2001 From: LaFriska Date: Tue, 28 Nov 2023 17:39:46 -0500 Subject: [PATCH 05/13] deleted redundant .toString() call in README test method Sysout --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 1db1f54..9806f2a 100644 --- a/README.md +++ b/README.md @@ -66,7 +66,7 @@ import org.json.JSONObject; public class Test { public static void main(String args[]){ JSONObject jo = new JSONObject("{ \"abc\" : \"def\" }"); - System.out.println(jo.toString()); + System.out.println(jo); } } ``` From 6d811607ddf4922a49fb37e3a813e2a59ca809a7 Mon Sep 17 00:00:00 2001 From: sk02241994 Date: Fri, 3 Nov 2023 19:54:23 +0530 Subject: [PATCH 06/13] Resolving issue #743 - Recursive depth issue found in JSONObject - Recursive depth issue found in JSONArray --- src/main/java/org/json/JSONArray.java | 29 ++++++++++++++----- src/main/java/org/json/JSONObject.java | 26 +++++++++++++++-- .../java/org/json/junit/JSONArrayTest.java | 21 ++++++++++++++ .../java/org/json/junit/JSONObjectTest.java | 17 +++++++++++ 4 files changed, 83 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/json/JSONArray.java b/src/main/java/org/json/JSONArray.java index ed7982f..eec7852 100644 --- a/src/main/java/org/json/JSONArray.java +++ b/src/main/java/org/json/JSONArray.java @@ -149,11 +149,18 @@ public class JSONArray implements Iterable { * A Collection. */ public JSONArray(Collection collection) { + this(collection, 0); + } + + protected JSONArray(Collection collection, int recursionDepth) { + if (recursionDepth > JSONObject.RECURSION_DEPTH_LIMIT) { + throw new JSONException("JSONArray has reached recursion depth limit of " + JSONObject.RECURSION_DEPTH_LIMIT); + } if (collection == null) { this.myArrayList = new ArrayList(); } else { this.myArrayList = new ArrayList(collection.size()); - this.addAll(collection, true); + this.addAll(collection, true, recursionDepth); } } @@ -205,7 +212,7 @@ public class JSONArray implements Iterable { throw new JSONException( "JSONArray initial value should be a string or collection or array."); } - this.addAll(array, true); + this.addAll(array, true, 0); } /** @@ -1779,13 +1786,15 @@ public class JSONArray implements Iterable { * @param wrap * {@code true} to call {@link JSONObject#wrap(Object)} for each item, * {@code false} to add the items directly + * @param recursionDepth + * variable to keep the count of how nested the object creation is happening. * */ - private void addAll(Collection collection, boolean wrap) { + private void addAll(Collection collection, boolean wrap, int recursionDepth) { this.myArrayList.ensureCapacity(this.myArrayList.size() + collection.size()); if (wrap) { for (Object o: collection){ - this.put(JSONObject.wrap(o)); + this.put(JSONObject.wrap(o, recursionDepth + 1)); } } else { for (Object o: collection){ @@ -1815,6 +1824,10 @@ public class JSONArray implements Iterable { } } + private void addAll(Object array, boolean wrap) throws JSONException { + this.addAll(array, wrap, 0); + } + /** * Add an array's elements to the JSONArray. * @@ -1825,19 +1838,21 @@ public class JSONArray implements Iterable { * @param wrap * {@code true} to call {@link JSONObject#wrap(Object)} for each item, * {@code false} to add the items directly + * @param recursionDepth + * Variable to keep the count of how nested the object creation is happening. * * @throws JSONException * If not an array or if an array value is non-finite number. * @throws NullPointerException * Thrown if the array parameter is null. */ - private void addAll(Object array, boolean wrap) throws JSONException { + private void addAll(Object array, boolean wrap, int recursionDepth) throws JSONException { if (array.getClass().isArray()) { int length = Array.getLength(array); this.myArrayList.ensureCapacity(this.myArrayList.size() + length); if (wrap) { for (int i = 0; i < length; i += 1) { - this.put(JSONObject.wrap(Array.get(array, i))); + this.put(JSONObject.wrap(Array.get(array, i), recursionDepth + 1)); } } else { for (int i = 0; i < length; i += 1) { @@ -1850,7 +1865,7 @@ public class JSONArray implements Iterable { // JSONArray this.myArrayList.addAll(((JSONArray)array).myArrayList); } else if (array instanceof Collection) { - this.addAll((Collection)array, wrap); + this.addAll((Collection)array, wrap, recursionDepth); } else if (array instanceof Iterable) { this.addAll((Iterable)array, wrap); } else { diff --git a/src/main/java/org/json/JSONObject.java b/src/main/java/org/json/JSONObject.java index 7f4885e..72c0ebd 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -147,6 +147,7 @@ public class JSONObject { * The map where the JSONObject's properties are kept. */ private final Map map; + public static final int RECURSION_DEPTH_LIMIT = 1000; public Class getMapType() { return map.getClass(); @@ -276,6 +277,17 @@ public class JSONObject { * If a key in the map is null */ public JSONObject(Map m) { + this(m, 0); + } + + /** + * Construct a JSONObject from a map with recursion depth. + * + */ + protected JSONObject(Map m, int recursionDepth) { + if (recursionDepth > RECURSION_DEPTH_LIMIT) { + throw new JSONException("JSONObject has reached recursion depth limit of " + RECURSION_DEPTH_LIMIT); + } if (m == null) { this.map = new HashMap(); } else { @@ -287,7 +299,7 @@ public class JSONObject { final Object value = e.getValue(); if (value != null) { testValidity(value); - this.map.put(String.valueOf(e.getKey()), wrap(value)); + this.map.put(String.valueOf(e.getKey()), wrap(value, recursionDepth + 1)); } } } @@ -2566,7 +2578,15 @@ public class JSONObject { return wrap(object, null); } + public static Object wrap(Object object, int recursionDepth) { + return wrap(object, null, recursionDepth); + } + private static Object wrap(Object object, Set objectsRecord) { + return wrap(object, objectsRecord, 0); + } + + private static Object wrap(Object object, Set objectsRecord, int recursionDepth) { try { if (NULL.equals(object)) { return NULL; @@ -2584,14 +2604,14 @@ public class JSONObject { if (object instanceof Collection) { Collection coll = (Collection) object; - return new JSONArray(coll); + return new JSONArray(coll, recursionDepth); } if (object.getClass().isArray()) { return new JSONArray(object); } if (object instanceof Map) { Map map = (Map) object; - return new JSONObject(map); + return new JSONObject(map, recursionDepth); } Package objectPackage = object.getClass().getPackage(); String objectPackageName = objectPackage != null ? objectPackage diff --git a/src/test/java/org/json/junit/JSONArrayTest.java b/src/test/java/org/json/junit/JSONArrayTest.java index 7b0d52e..44a1d7b 100644 --- a/src/test/java/org/json/junit/JSONArrayTest.java +++ b/src/test/java/org/json/junit/JSONArrayTest.java @@ -1417,4 +1417,25 @@ public class JSONArrayTest { .put(2); assertFalse(ja1.similar(ja3)); } + + @Test(expected = JSONException.class) + public void testRecursiveDepth() { + HashMap map = new HashMap<>(); + map.put("t", map); + new JSONArray().put(map); + } + + @Test(expected = JSONException.class) + public void testRecursiveDepthAtPosition() { + HashMap map = new HashMap<>(); + map.put("t", map); + new JSONArray().put(0, map); + } + + @Test(expected = JSONException.class) + public void testRecursiveDepthArray() { + ArrayList array = new ArrayList<>(); + array.add(array); + new JSONArray(array); + } } diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index d9adbca..dff4503 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -3718,4 +3718,21 @@ public class JSONObjectTest { assertThrows(JSONException.class, () -> new JSONObject(bean)); } } + + @Test(expected = JSONException.class) + public void issue743SerializationMap() { + HashMap map = new HashMap<>(); + map.put("t", map); + JSONObject object = new JSONObject(map); + String jsonString = object.toString(); + } + + @Test(expected = JSONException.class) + public void testCircularReferenceMultipleLevel() { + HashMap inside = new HashMap<>(); + HashMap jsonObject = new HashMap<>(); + inside.put("inside", jsonObject); + jsonObject.put("test", inside); + new JSONObject(jsonObject); + } } From dcac3bc18e2acbdf617f3debab234bae857fe5b8 Mon Sep 17 00:00:00 2001 From: sk02241994 Date: Tue, 28 Nov 2023 10:29:30 +0530 Subject: [PATCH 07/13] Adding test case for nested json with depth of 999, 1000, 1001 --- .../java/org/json/junit/JSONArrayTest.java | 52 +++++++++++++++++++ .../java/org/json/junit/JSONObjectTest.java | 37 +++++++++++++ 2 files changed, 89 insertions(+) diff --git a/src/test/java/org/json/junit/JSONArrayTest.java b/src/test/java/org/json/junit/JSONArrayTest.java index 44a1d7b..5f50fc7 100644 --- a/src/test/java/org/json/junit/JSONArrayTest.java +++ b/src/test/java/org/json/junit/JSONArrayTest.java @@ -1438,4 +1438,56 @@ public class JSONArrayTest { array.add(array); new JSONArray(array); } + + @Test + public void testRecursiveDepthAtPosition999Object() { + HashMap map = JSONObjectTest.buildNestedMap(999); + new JSONArray().put(0, map); + } + + @Test + public void testRecursiveDepthAtPosition1000Object() { + HashMap map = JSONObjectTest.buildNestedMap(1000); + new JSONArray().put(0, map); + } + + @Test(expected = JSONException.class) + public void testRecursiveDepthAtPosition1001Object() { + HashMap map = JSONObjectTest.buildNestedMap(1001); + new JSONArray().put(0, map); + } + + @Test(expected = JSONException.class) + public void testRecursiveDepthArrayLimitedMaps() { + ArrayList array = new ArrayList<>(); + array.add(array); + new JSONArray(array); + } + + @Test + public void testRecursiveDepthArrayFor999Levels() { + ArrayList array = buildNestedArray(999); + new JSONArray(array); + } + + @Test + public void testRecursiveDepthArrayFor1000Levels() { + ArrayList array = buildNestedArray(1000); + new JSONArray(array); + } + + @Test(expected = JSONException.class) + public void testRecursiveDepthArrayFor1001Levels() { + ArrayList array = buildNestedArray(1001); + new JSONArray(array); + } + + public static ArrayList buildNestedArray(int maxDepth) { + if (maxDepth <= 0) { + return new ArrayList<>(); + } + ArrayList nestedArray = new ArrayList<>(); + nestedArray.add(buildNestedArray(maxDepth - 1)); + return nestedArray; + } } diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index dff4503..e157fd5 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -3735,4 +3735,41 @@ public class JSONObjectTest { jsonObject.put("test", inside); new JSONObject(jsonObject); } + + @Test + public void issue743SerializationMapWith999Objects() { + HashMap map = buildNestedMap(999); + JSONObject object = new JSONObject(map); + String jsonString = object.toString(); + } + + @Test + public void issue743SerializationMapWith1000Objects() { + HashMap map = buildNestedMap(1000); + JSONObject object = new JSONObject(map); + String jsonString = object.toString(); + } + + @Test(expected = JSONException.class) + public void issue743SerializationMapWith1001Objects() { + HashMap map = buildNestedMap(1001); + JSONObject object = new JSONObject(map); + String jsonString = object.toString(); + } + + /** + * Method to build nested map of max maxDepth + * + * @param maxDepth + * @return + */ + public static HashMap buildNestedMap(int maxDepth) { + if (maxDepth <= 0) { + return new HashMap<>(); + } + HashMap nestedMap = new HashMap<>(); + nestedMap.put("t", buildNestedMap(maxDepth - 1)); + return nestedMap; + } + } From abea194120cbfe684444a9012aac70fafaef5cfa Mon Sep 17 00:00:00 2001 From: sk02241994 Date: Fri, 22 Dec 2023 15:44:33 +0530 Subject: [PATCH 08/13] Adding JSONParserConfiguration for configuring the depth of nested maps --- src/main/java/org/json/JSONArray.java | 69 +++++++++++++++---- src/main/java/org/json/JSONObject.java | 27 ++++---- .../org/json/JSONParserConfiguration.java | 29 ++++++++ .../java/org/json/junit/JSONArrayTest.java | 16 +++-- .../java/org/json/junit/JSONObjectTest.java | 8 ++- 5 files changed, 115 insertions(+), 34 deletions(-) create mode 100644 src/main/java/org/json/JSONParserConfiguration.java diff --git a/src/main/java/org/json/JSONArray.java b/src/main/java/org/json/JSONArray.java index eec7852..6e19a54 100644 --- a/src/main/java/org/json/JSONArray.java +++ b/src/main/java/org/json/JSONArray.java @@ -149,18 +149,22 @@ public class JSONArray implements Iterable { * A Collection. */ public JSONArray(Collection collection) { - this(collection, 0); + this(collection, 0, new JSONParserConfiguration()); } - protected JSONArray(Collection collection, int recursionDepth) { - if (recursionDepth > JSONObject.RECURSION_DEPTH_LIMIT) { - throw new JSONException("JSONArray has reached recursion depth limit of " + JSONObject.RECURSION_DEPTH_LIMIT); + public JSONArray(Collection collection, JSONParserConfiguration jsonParserConfiguration) { + this(collection, 0, jsonParserConfiguration); + } + + protected JSONArray(Collection collection, int recursionDepth, JSONParserConfiguration jsonParserConfiguration) { + if (recursionDepth > jsonParserConfiguration.getMaxNestingDepth()) { + throw new JSONException("JSONArray has reached recursion depth limit of " + jsonParserConfiguration.getMaxNestingDepth()); } if (collection == null) { this.myArrayList = new ArrayList(); } else { this.myArrayList = new ArrayList(collection.size()); - this.addAll(collection, true, recursionDepth); + this.addAll(collection, true, recursionDepth, jsonParserConfiguration); } } @@ -1345,7 +1349,27 @@ public class JSONArray implements Iterable { * If a key in the map is null */ public JSONArray put(int index, Map value) throws JSONException { - this.put(index, new JSONObject(value)); + this.put(index, new JSONObject(value, new JSONParserConfiguration())); + return this; + } + + /** + * Put a value in the JSONArray, where the value will be a JSONObject that + * is produced from a Map. + * + * @param index + * The subscript + * @param value + * The Map value. + * @param jsonParserConfiguration + * Configuration for recursive depth + * @return + * @throws JSONException + * If the index is negative or if the value is an invalid + * number. + */ + public JSONArray put(int index, Map value, JSONParserConfiguration jsonParserConfiguration) throws JSONException { + this.put(index, new JSONObject(value, jsonParserConfiguration)); return this; } @@ -1790,11 +1814,11 @@ public class JSONArray implements Iterable { * variable to keep the count of how nested the object creation is happening. * */ - private void addAll(Collection collection, boolean wrap, int recursionDepth) { + private void addAll(Collection collection, boolean wrap, int recursionDepth, JSONParserConfiguration jsonParserConfiguration) { this.myArrayList.ensureCapacity(this.myArrayList.size() + collection.size()); if (wrap) { for (Object o: collection){ - this.put(JSONObject.wrap(o, recursionDepth + 1)); + this.put(JSONObject.wrap(o, recursionDepth + 1, jsonParserConfiguration)); } } else { for (Object o: collection){ @@ -1823,7 +1847,14 @@ public class JSONArray implements Iterable { } } } - + + /** + * Add an array's elements to the JSONArray. + * + * @param array + * @param wrap + * @throws JSONException + */ private void addAll(Object array, boolean wrap) throws JSONException { this.addAll(array, wrap, 0); } @@ -1836,23 +1867,37 @@ public class JSONArray implements Iterable { * JSONArray, Collection, or Iterable, an exception will be * thrown. * @param wrap + * @param recursionDepth + */ + private void addAll(Object array, boolean wrap, int recursionDepth) { + addAll(array, wrap, recursionDepth, new JSONParserConfiguration()); + } + /** + * Add an array's elements to the JSONArray. + *` + * @param array + * Array. If the parameter passed is null, or not an array, + * JSONArray, Collection, or Iterable, an exception will be + * thrown. + * @param wrap * {@code true} to call {@link JSONObject#wrap(Object)} for each item, * {@code false} to add the items directly * @param recursionDepth * Variable to keep the count of how nested the object creation is happening. - * + * @param recursionDepth + * Variable to pass parser custom configuration for json parsing. * @throws JSONException * If not an array or if an array value is non-finite number. * @throws NullPointerException * Thrown if the array parameter is null. */ - private void addAll(Object array, boolean wrap, int recursionDepth) throws JSONException { + private void addAll(Object array, boolean wrap, int recursionDepth, JSONParserConfiguration jsonParserConfiguration) throws JSONException { if (array.getClass().isArray()) { int length = Array.getLength(array); this.myArrayList.ensureCapacity(this.myArrayList.size() + length); if (wrap) { for (int i = 0; i < length; i += 1) { - this.put(JSONObject.wrap(Array.get(array, i), recursionDepth + 1)); + this.put(JSONObject.wrap(Array.get(array, i), recursionDepth + 1, jsonParserConfiguration)); } } else { for (int i = 0; i < length; i += 1) { diff --git a/src/main/java/org/json/JSONObject.java b/src/main/java/org/json/JSONObject.java index 72c0ebd..18721f7 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -147,7 +147,6 @@ public class JSONObject { * The map where the JSONObject's properties are kept. */ private final Map map; - public static final int RECURSION_DEPTH_LIMIT = 1000; public Class getMapType() { return map.getClass(); @@ -277,16 +276,20 @@ public class JSONObject { * If a key in the map is null */ public JSONObject(Map m) { - this(m, 0); + this(m, 0, new JSONParserConfiguration()); + } + + public JSONObject(Map m, JSONParserConfiguration jsonParserConfiguration) { + this(m, 0, jsonParserConfiguration); } /** * Construct a JSONObject from a map with recursion depth. * */ - protected JSONObject(Map m, int recursionDepth) { - if (recursionDepth > RECURSION_DEPTH_LIMIT) { - throw new JSONException("JSONObject has reached recursion depth limit of " + RECURSION_DEPTH_LIMIT); + protected JSONObject(Map m, int recursionDepth, JSONParserConfiguration jsonParserConfiguration) { + if (recursionDepth > jsonParserConfiguration.getMaxNestingDepth()) { + throw new JSONException("JSONObject has reached recursion depth limit of " + jsonParserConfiguration.getMaxNestingDepth()); } if (m == null) { this.map = new HashMap(); @@ -299,7 +302,7 @@ public class JSONObject { final Object value = e.getValue(); if (value != null) { testValidity(value); - this.map.put(String.valueOf(e.getKey()), wrap(value, recursionDepth + 1)); + this.map.put(String.valueOf(e.getKey()), wrap(value, recursionDepth + 1, jsonParserConfiguration)); } } } @@ -2578,15 +2581,15 @@ public class JSONObject { return wrap(object, null); } - public static Object wrap(Object object, int recursionDepth) { - return wrap(object, null, recursionDepth); + public static Object wrap(Object object, int recursionDepth, JSONParserConfiguration jsonParserConfiguration) { + return wrap(object, null, recursionDepth, jsonParserConfiguration); } private static Object wrap(Object object, Set objectsRecord) { - return wrap(object, objectsRecord, 0); + return wrap(object, objectsRecord, 0, new JSONParserConfiguration()); } - private static Object wrap(Object object, Set objectsRecord, int recursionDepth) { + private static Object wrap(Object object, Set objectsRecord, int recursionDepth, JSONParserConfiguration jsonParserConfiguration) { try { if (NULL.equals(object)) { return NULL; @@ -2604,14 +2607,14 @@ public class JSONObject { if (object instanceof Collection) { Collection coll = (Collection) object; - return new JSONArray(coll, recursionDepth); + return new JSONArray(coll, recursionDepth, jsonParserConfiguration); } if (object.getClass().isArray()) { return new JSONArray(object); } if (object instanceof Map) { Map map = (Map) object; - return new JSONObject(map, recursionDepth); + return new JSONObject(map, recursionDepth, jsonParserConfiguration); } Package objectPackage = object.getClass().getPackage(); String objectPackageName = objectPackage != null ? objectPackage diff --git a/src/main/java/org/json/JSONParserConfiguration.java b/src/main/java/org/json/JSONParserConfiguration.java new file mode 100644 index 0000000..910d1cf --- /dev/null +++ b/src/main/java/org/json/JSONParserConfiguration.java @@ -0,0 +1,29 @@ +package org.json; + +/** + * Configuration object for the JSON parser. The configuration is immutable. + */ +public class JSONParserConfiguration extends ParserConfiguration { + + /** + * We can override the default maximum nesting depth if needed. + */ + public static final int DEFAULT_MAXIMUM_NESTING_DEPTH = ParserConfiguration.DEFAULT_MAXIMUM_NESTING_DEPTH; + + /** + * Configuration with the default values. + */ + public JSONParserConfiguration() { + this.maxNestingDepth = DEFAULT_MAXIMUM_NESTING_DEPTH; + } + + public JSONParserConfiguration(int maxNestingDepth) { + this.maxNestingDepth = maxNestingDepth; + } + + @Override + protected JSONParserConfiguration clone() { + return new JSONParserConfiguration(DEFAULT_MAXIMUM_NESTING_DEPTH); + } + +} diff --git a/src/test/java/org/json/junit/JSONArrayTest.java b/src/test/java/org/json/junit/JSONArrayTest.java index 5f50fc7..349422d 100644 --- a/src/test/java/org/json/junit/JSONArrayTest.java +++ b/src/test/java/org/json/junit/JSONArrayTest.java @@ -28,6 +28,7 @@ import java.util.Map; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; +import org.json.JSONParserConfiguration; import org.json.JSONPointerException; import org.json.JSONString; import org.json.JSONTokener; @@ -1440,15 +1441,15 @@ public class JSONArrayTest { } @Test - public void testRecursiveDepthAtPosition999Object() { - HashMap map = JSONObjectTest.buildNestedMap(999); + public void testRecursiveDepthAtPositionDefaultObject() { + HashMap map = JSONObjectTest.buildNestedMap(JSONParserConfiguration.DEFAULT_MAXIMUM_NESTING_DEPTH); new JSONArray().put(0, map); } @Test public void testRecursiveDepthAtPosition1000Object() { HashMap map = JSONObjectTest.buildNestedMap(1000); - new JSONArray().put(0, map); + new JSONArray().put(0, map, new JSONParserConfiguration(1000)); } @Test(expected = JSONException.class) @@ -1465,15 +1466,16 @@ public class JSONArrayTest { } @Test - public void testRecursiveDepthArrayFor999Levels() { - ArrayList array = buildNestedArray(999); - new JSONArray(array); + public void testRecursiveDepthArrayForDefaultLevels() { + ArrayList array = buildNestedArray(JSONParserConfiguration.DEFAULT_MAXIMUM_NESTING_DEPTH); + new JSONArray(array, new JSONParserConfiguration()); } @Test public void testRecursiveDepthArrayFor1000Levels() { ArrayList array = buildNestedArray(1000); - new JSONArray(array); + JSONParserConfiguration parserConfiguration = new JSONParserConfiguration(1000); + new JSONArray(array, parserConfiguration); } @Test(expected = JSONException.class) diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index e157fd5..1a911d8 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -32,6 +32,7 @@ import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import org.json.JSONPointerException; +import org.json.JSONParserConfiguration; import org.json.JSONString; import org.json.JSONTokener; import org.json.XML; @@ -3737,8 +3738,8 @@ public class JSONObjectTest { } @Test - public void issue743SerializationMapWith999Objects() { - HashMap map = buildNestedMap(999); + public void issue743SerializationMapWith512Objects() { + HashMap map = buildNestedMap(JSONParserConfiguration.DEFAULT_MAXIMUM_NESTING_DEPTH); JSONObject object = new JSONObject(map); String jsonString = object.toString(); } @@ -3746,7 +3747,8 @@ public class JSONObjectTest { @Test public void issue743SerializationMapWith1000Objects() { HashMap map = buildNestedMap(1000); - JSONObject object = new JSONObject(map); + JSONParserConfiguration parserConfiguration = new JSONParserConfiguration(1000); + JSONObject object = new JSONObject(map, parserConfiguration); String jsonString = object.toString(); } From ffd48afa42f014dfbd48f86c3cef96ccc4e6c7df Mon Sep 17 00:00:00 2001 From: sk02241994 Date: Sat, 23 Dec 2023 10:53:54 +0530 Subject: [PATCH 09/13] Review comments --- .../java/org/json/JSONParserConfiguration.java | 18 +++++++----------- .../java/org/json/junit/JSONArrayTest.java | 9 +++++---- .../java/org/json/junit/JSONObjectTest.java | 5 +++-- 3 files changed, 15 insertions(+), 17 deletions(-) diff --git a/src/main/java/org/json/JSONParserConfiguration.java b/src/main/java/org/json/JSONParserConfiguration.java index 910d1cf..1ec1710 100644 --- a/src/main/java/org/json/JSONParserConfiguration.java +++ b/src/main/java/org/json/JSONParserConfiguration.java @@ -5,25 +5,21 @@ package org.json; */ public class JSONParserConfiguration extends ParserConfiguration { - /** - * We can override the default maximum nesting depth if needed. - */ - public static final int DEFAULT_MAXIMUM_NESTING_DEPTH = ParserConfiguration.DEFAULT_MAXIMUM_NESTING_DEPTH; - /** * Configuration with the default values. */ public JSONParserConfiguration() { - this.maxNestingDepth = DEFAULT_MAXIMUM_NESTING_DEPTH; - } - - public JSONParserConfiguration(int maxNestingDepth) { - this.maxNestingDepth = maxNestingDepth; + super(); } @Override protected JSONParserConfiguration clone() { - return new JSONParserConfiguration(DEFAULT_MAXIMUM_NESTING_DEPTH); + return new JSONParserConfiguration(); + } + + @Override + public JSONParserConfiguration withMaxNestingDepth(final int maxNestingDepth) { + return super.withMaxNestingDepth(maxNestingDepth); } } diff --git a/src/test/java/org/json/junit/JSONArrayTest.java b/src/test/java/org/json/junit/JSONArrayTest.java index 349422d..fd01379 100644 --- a/src/test/java/org/json/junit/JSONArrayTest.java +++ b/src/test/java/org/json/junit/JSONArrayTest.java @@ -32,6 +32,7 @@ import org.json.JSONParserConfiguration; import org.json.JSONPointerException; import org.json.JSONString; import org.json.JSONTokener; +import org.json.ParserConfiguration; import org.json.junit.data.MyJsonString; import org.junit.Ignore; import org.junit.Test; @@ -1442,14 +1443,14 @@ public class JSONArrayTest { @Test public void testRecursiveDepthAtPositionDefaultObject() { - HashMap map = JSONObjectTest.buildNestedMap(JSONParserConfiguration.DEFAULT_MAXIMUM_NESTING_DEPTH); + HashMap map = JSONObjectTest.buildNestedMap(ParserConfiguration.DEFAULT_MAXIMUM_NESTING_DEPTH); new JSONArray().put(0, map); } @Test public void testRecursiveDepthAtPosition1000Object() { HashMap map = JSONObjectTest.buildNestedMap(1000); - new JSONArray().put(0, map, new JSONParserConfiguration(1000)); + new JSONArray().put(0, map, new JSONParserConfiguration().withMaxNestingDepth(1000)); } @Test(expected = JSONException.class) @@ -1467,14 +1468,14 @@ public class JSONArrayTest { @Test public void testRecursiveDepthArrayForDefaultLevels() { - ArrayList array = buildNestedArray(JSONParserConfiguration.DEFAULT_MAXIMUM_NESTING_DEPTH); + ArrayList array = buildNestedArray(ParserConfiguration.DEFAULT_MAXIMUM_NESTING_DEPTH); new JSONArray(array, new JSONParserConfiguration()); } @Test public void testRecursiveDepthArrayFor1000Levels() { ArrayList array = buildNestedArray(1000); - JSONParserConfiguration parserConfiguration = new JSONParserConfiguration(1000); + JSONParserConfiguration parserConfiguration = new JSONParserConfiguration().withMaxNestingDepth(1000); new JSONArray(array, parserConfiguration); } diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 1a911d8..96f3673 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -35,6 +35,7 @@ import org.json.JSONPointerException; import org.json.JSONParserConfiguration; import org.json.JSONString; import org.json.JSONTokener; +import org.json.ParserConfiguration; import org.json.XML; import org.json.junit.data.BrokenToString; import org.json.junit.data.ExceptionalBean; @@ -3739,7 +3740,7 @@ public class JSONObjectTest { @Test public void issue743SerializationMapWith512Objects() { - HashMap map = buildNestedMap(JSONParserConfiguration.DEFAULT_MAXIMUM_NESTING_DEPTH); + HashMap map = buildNestedMap(ParserConfiguration.DEFAULT_MAXIMUM_NESTING_DEPTH); JSONObject object = new JSONObject(map); String jsonString = object.toString(); } @@ -3747,7 +3748,7 @@ public class JSONObjectTest { @Test public void issue743SerializationMapWith1000Objects() { HashMap map = buildNestedMap(1000); - JSONParserConfiguration parserConfiguration = new JSONParserConfiguration(1000); + JSONParserConfiguration parserConfiguration = new JSONParserConfiguration().withMaxNestingDepth(1000); JSONObject object = new JSONObject(map, parserConfiguration); String jsonString = object.toString(); } From 7701f2183966dd4f1efad1b762b47a89009e60ad Mon Sep 17 00:00:00 2001 From: sk02241994 Date: Sun, 24 Dec 2023 11:02:47 +0530 Subject: [PATCH 10/13] Adding comments --- src/main/java/org/json/JSONArray.java | 38 ++++++++++++++++++++++---- src/main/java/org/json/JSONObject.java | 29 ++++++++++++++++++-- 2 files changed, 59 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/json/JSONArray.java b/src/main/java/org/json/JSONArray.java index 6e19a54..50146a8 100644 --- a/src/main/java/org/json/JSONArray.java +++ b/src/main/java/org/json/JSONArray.java @@ -152,11 +152,29 @@ public class JSONArray implements Iterable { this(collection, 0, new JSONParserConfiguration()); } + /** + * Construct a JSONArray from a Collection. + * + * @param collection + * A Collection. + * @param jsonParserConfiguration + * Configuration object for the JSON parser + */ public JSONArray(Collection collection, JSONParserConfiguration jsonParserConfiguration) { this(collection, 0, jsonParserConfiguration); } - protected JSONArray(Collection collection, int recursionDepth, JSONParserConfiguration jsonParserConfiguration) { + /** + * Construct a JSONArray from a collection with recursion depth. + * + * @param collection + * A Collection. + * @param recursionDepth + * Variable for tracking the count of nested object creations. + * @param jsonParserConfiguration + * Configuration object for the JSON parser + */ + JSONArray(Collection collection, int recursionDepth, JSONParserConfiguration jsonParserConfiguration) { if (recursionDepth > jsonParserConfiguration.getMaxNestingDepth()) { throw new JSONException("JSONArray has reached recursion depth limit of " + jsonParserConfiguration.getMaxNestingDepth()); } @@ -1362,7 +1380,7 @@ public class JSONArray implements Iterable { * @param value * The Map value. * @param jsonParserConfiguration - * Configuration for recursive depth + * Configuration object for the JSON parser * @return * @throws JSONException * If the index is negative or if the value is an invalid @@ -1811,8 +1829,7 @@ public class JSONArray implements Iterable { * {@code true} to call {@link JSONObject#wrap(Object)} for each item, * {@code false} to add the items directly * @param recursionDepth - * variable to keep the count of how nested the object creation is happening. - * + * Variable for tracking the count of nested object creations. */ private void addAll(Collection collection, boolean wrap, int recursionDepth, JSONParserConfiguration jsonParserConfiguration) { this.myArrayList.ensureCapacity(this.myArrayList.size() + collection.size()); @@ -1852,8 +1869,14 @@ public class JSONArray implements Iterable { * Add an array's elements to the JSONArray. * * @param array + * Array. If the parameter passed is null, or not an array, + * JSONArray, Collection, or Iterable, an exception will be + * thrown. * @param wrap + * {@code true} to call {@link JSONObject#wrap(Object)} for each item, + * {@code false} to add the items directly * @throws JSONException + * If not an array or if an array value is non-finite number. */ private void addAll(Object array, boolean wrap) throws JSONException { this.addAll(array, wrap, 0); @@ -1867,7 +1890,10 @@ public class JSONArray implements Iterable { * JSONArray, Collection, or Iterable, an exception will be * thrown. * @param wrap + * {@code true} to call {@link JSONObject#wrap(Object)} for each item, + * {@code false} to add the items directly * @param recursionDepth + * Variable for tracking the count of nested object creations. */ private void addAll(Object array, boolean wrap, int recursionDepth) { addAll(array, wrap, recursionDepth, new JSONParserConfiguration()); @@ -1883,8 +1909,8 @@ public class JSONArray implements Iterable { * {@code true} to call {@link JSONObject#wrap(Object)} for each item, * {@code false} to add the items directly * @param recursionDepth - * Variable to keep the count of how nested the object creation is happening. - * @param recursionDepth + * Variable for tracking the count of nested object creations. + * @param jsonParserConfiguration * Variable to pass parser custom configuration for json parsing. * @throws JSONException * If not an array or if an array value is non-finite number. diff --git a/src/main/java/org/json/JSONObject.java b/src/main/java/org/json/JSONObject.java index 18721f7..4bd032b 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -279,6 +279,15 @@ public class JSONObject { this(m, 0, new JSONParserConfiguration()); } + /** + * Construct a JSONObject from a Map with custom json parse configurations. + * + * @param m + * A map object that can be used to initialize the contents of + * the JSONObject. + * @param jsonParserConfiguration + * Variable to pass parser custom configuration for json parsing. + */ public JSONObject(Map m, JSONParserConfiguration jsonParserConfiguration) { this(m, 0, jsonParserConfiguration); } @@ -287,7 +296,7 @@ public class JSONObject { * Construct a JSONObject from a map with recursion depth. * */ - protected JSONObject(Map m, int recursionDepth, JSONParserConfiguration jsonParserConfiguration) { + private JSONObject(Map m, int recursionDepth, JSONParserConfiguration jsonParserConfiguration) { if (recursionDepth > jsonParserConfiguration.getMaxNestingDepth()) { throw new JSONException("JSONObject has reached recursion depth limit of " + jsonParserConfiguration.getMaxNestingDepth()); } @@ -2581,7 +2590,23 @@ public class JSONObject { return wrap(object, null); } - public static Object wrap(Object object, int recursionDepth, JSONParserConfiguration jsonParserConfiguration) { + /** + * Wrap an object, if necessary. If the object is null, return the NULL + * object. If it is an array or collection, wrap it in a JSONArray. If it is + * a map, wrap it in a JSONObject. If it is a standard property (Double, + * String, et al) then it is already wrapped. Otherwise, if it comes from + * one of the java packages, turn it into a string. And if it doesn't, try + * to wrap it in a JSONObject. If the wrapping fails, then null is returned. + * + * @param object + * The object to wrap + * @param recursionDepth + * Variable for tracking the count of nested object creations. + * @param jsonParserConfiguration + * Variable to pass parser custom configuration for json parsing. + * @return The wrapped value + */ + static Object wrap(Object object, int recursionDepth, JSONParserConfiguration jsonParserConfiguration) { return wrap(object, null, recursionDepth, jsonParserConfiguration); } From 23ac2e7bcaf6ec2c62d7e21d8d4254feaf56bbff Mon Sep 17 00:00:00 2001 From: Thomas Gress Date: Fri, 29 Dec 2023 12:28:24 +0100 Subject: [PATCH 11/13] improved annotation search performance --- src/main/java/org/json/JSONObject.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/main/java/org/json/JSONObject.java b/src/main/java/org/json/JSONObject.java index 4bd032b..039f136 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -1865,6 +1865,10 @@ public class JSONObject { } } + //If the superclass is Object, no annotations will be found any more + if (c.getSuperclass().equals(Object.class)) + return null; + try { return getAnnotation( c.getSuperclass().getMethod(m.getName(), m.getParameterTypes()), @@ -1919,6 +1923,10 @@ public class JSONObject { } } + //If the superclass is Object, no annotations will be found any more + if (c.getSuperclass().equals(Object.class)) + return -1; + try { int d = getAnnotationDepth( c.getSuperclass().getMethod(m.getName(), m.getParameterTypes()), From 5ddb8c3d351c155250891968dfc4a1e8442ae14f Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Sat, 30 Dec 2023 16:30:19 -0600 Subject: [PATCH 12/13] cleanup-and-merge-tests: fix warnings, set gradlew permissions, enable unchecked warnings in maven --- gradlew | 0 pom.xml | 3 +++ src/main/java/org/json/JSONArray.java | 5 +++-- src/main/java/org/json/JSONMLParserConfiguration.java | 2 ++ src/main/java/org/json/JSONParserConfiguration.java | 1 + src/main/java/org/json/ParserConfiguration.java | 2 ++ src/main/java/org/json/XMLParserConfiguration.java | 4 +++- src/test/java/org/json/junit/data/WeirdList.java | 6 +++--- 8 files changed, 17 insertions(+), 6 deletions(-) mode change 100644 => 100755 gradlew diff --git a/gradlew b/gradlew old mode 100644 new mode 100755 diff --git a/pom.xml b/pom.xml index 927a989..d01283c 100644 --- a/pom.xml +++ b/pom.xml @@ -104,6 +104,9 @@ 1.8 1.8 + + -Xlint:unchecked + diff --git a/src/main/java/org/json/JSONArray.java b/src/main/java/org/json/JSONArray.java index 50146a8..38b0b31 100644 --- a/src/main/java/org/json/JSONArray.java +++ b/src/main/java/org/json/JSONArray.java @@ -1359,7 +1359,8 @@ public class JSONArray implements Iterable { * The subscript. * @param value * The Map value. - * @return this. + * @return + * reference to self * @throws JSONException * If the index is negative or if the value is an invalid * number. @@ -1381,7 +1382,7 @@ public class JSONArray implements Iterable { * The Map value. * @param jsonParserConfiguration * Configuration object for the JSON parser - * @return + * @return reference to self * @throws JSONException * If the index is negative or if the value is an invalid * number. diff --git a/src/main/java/org/json/JSONMLParserConfiguration.java b/src/main/java/org/json/JSONMLParserConfiguration.java index b2514ab..43ba0db 100644 --- a/src/main/java/org/json/JSONMLParserConfiguration.java +++ b/src/main/java/org/json/JSONMLParserConfiguration.java @@ -55,11 +55,13 @@ public class JSONMLParserConfiguration extends ParserConfiguration { ); } + @SuppressWarnings("unchecked") @Override public JSONMLParserConfiguration withKeepStrings(final boolean newVal) { return super.withKeepStrings(newVal); } + @SuppressWarnings("unchecked") @Override public JSONMLParserConfiguration withMaxNestingDepth(int maxNestingDepth) { return super.withMaxNestingDepth(maxNestingDepth); diff --git a/src/main/java/org/json/JSONParserConfiguration.java b/src/main/java/org/json/JSONParserConfiguration.java index 1ec1710..f95e244 100644 --- a/src/main/java/org/json/JSONParserConfiguration.java +++ b/src/main/java/org/json/JSONParserConfiguration.java @@ -17,6 +17,7 @@ public class JSONParserConfiguration extends ParserConfiguration { return new JSONParserConfiguration(); } + @SuppressWarnings("unchecked") @Override public JSONParserConfiguration withMaxNestingDepth(final int maxNestingDepth) { return super.withMaxNestingDepth(maxNestingDepth); diff --git a/src/main/java/org/json/ParserConfiguration.java b/src/main/java/org/json/ParserConfiguration.java index 36fa508..ede2fc5 100644 --- a/src/main/java/org/json/ParserConfiguration.java +++ b/src/main/java/org/json/ParserConfiguration.java @@ -75,6 +75,7 @@ public class ParserConfiguration { * * @return The existing configuration will not be modified. A new configuration is returned. */ + @SuppressWarnings("unchecked") public T withKeepStrings(final boolean newVal) { T newConfig = (T)this.clone(); newConfig.keepStrings = newVal; @@ -101,6 +102,7 @@ public class ParserConfiguration { * * @return The existing configuration will not be modified. A new configuration is returned. */ + @SuppressWarnings("unchecked") public T withMaxNestingDepth(int maxNestingDepth) { T newConfig = (T)this.clone(); diff --git a/src/main/java/org/json/XMLParserConfiguration.java b/src/main/java/org/json/XMLParserConfiguration.java index 2e4907f..0ac7834 100644 --- a/src/main/java/org/json/XMLParserConfiguration.java +++ b/src/main/java/org/json/XMLParserConfiguration.java @@ -192,6 +192,7 @@ public class XMLParserConfiguration extends ParserConfiguration { * * @return The existing configuration will not be modified. A new configuration is returned. */ + @SuppressWarnings("unchecked") @Override public XMLParserConfiguration withKeepStrings(final boolean newVal) { return super.withKeepStrings(newVal); @@ -309,6 +310,7 @@ public class XMLParserConfiguration extends ParserConfiguration { * @param maxNestingDepth the maximum nesting depth allowed to the XML parser * @return The existing configuration will not be modified. A new configuration is returned. */ + @SuppressWarnings("unchecked") @Override public XMLParserConfiguration withMaxNestingDepth(int maxNestingDepth) { return super.withMaxNestingDepth(maxNestingDepth); @@ -316,7 +318,7 @@ public class XMLParserConfiguration extends ParserConfiguration { /** * To enable explicit end tag with empty value. - * @param closeEmptyTag + * @param closeEmptyTag new value for the closeEmptyTag property * @return same instance of configuration with empty tag config updated */ public XMLParserConfiguration withCloseEmptyTag(boolean closeEmptyTag){ diff --git a/src/test/java/org/json/junit/data/WeirdList.java b/src/test/java/org/json/junit/data/WeirdList.java index 834b81e..3560586 100644 --- a/src/test/java/org/json/junit/data/WeirdList.java +++ b/src/test/java/org/json/junit/data/WeirdList.java @@ -12,7 +12,7 @@ import java.util.List; */ public class WeirdList { /** */ - private final List list = new ArrayList(); + private final List list = new ArrayList<>(); /** * @param vals @@ -25,14 +25,14 @@ public class WeirdList { * @return a copy of the list */ public List get() { - return new ArrayList(this.list); + return new ArrayList<>(this.list); } /** * @return a copy of the list */ public List getALL() { - return new ArrayList(this.list); + return new ArrayList<>(this.list); } /** From 86bb0a1a02d49d2d1799153a45f7b8bcf2ed7015 Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Sat, 30 Dec 2023 17:00:02 -0600 Subject: [PATCH 13/13] cleanup-and-merge-tests: pull in unit tests from #809 --- .../java/org/json/junit/JSONObjectTest.java | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 96f3673..053f17a 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -3760,6 +3760,48 @@ public class JSONObjectTest { String jsonString = object.toString(); } + @Test(expected = JSONException.class) + public void testCircleReferenceFirstLevel() { + Map jsonObject = new HashMap<>(); + + jsonObject.put("test", jsonObject); + + new JSONObject(jsonObject, new JSONParserConfiguration()); + } + + @Test(expected = StackOverflowError.class) + public void testCircleReferenceMultiplyLevel_notConfigured_expectedStackOverflow() { + Map inside = new HashMap<>(); + + Map jsonObject = new HashMap<>(); + inside.put("test", jsonObject); + jsonObject.put("test", inside); + + new JSONObject(jsonObject, new JSONParserConfiguration().withMaxNestingDepth(99999)); + } + + @Test(expected = JSONException.class) + public void testCircleReferenceMultiplyLevel_configured_expectedJSONException() { + Map inside = new HashMap<>(); + + Map jsonObject = new HashMap<>(); + inside.put("test", jsonObject); + jsonObject.put("test", inside); + + new JSONObject(jsonObject, new JSONParserConfiguration()); + } + + @Test + public void testDifferentKeySameInstanceNotACircleReference() { + Map map1 = new HashMap<>(); + Map map2 = new HashMap<>(); + + map1.put("test1", map2); + map1.put("test2", map2); + + new JSONObject(map1); + } + /** * Method to build nested map of max maxDepth *