From abea194120cbfe684444a9012aac70fafaef5cfa Mon Sep 17 00:00:00 2001 From: sk02241994 Date: Fri, 22 Dec 2023 15:44:33 +0530 Subject: [PATCH] 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 eec7852d5..6e19a5482 100644 --- a/src/main/java/org/json/JSONArray.java +++ b/src/main/java/org/json/JSONArray.java @@ -149,18 +149,22 @@ public JSONArray(String source) throws JSONException { * 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 JSONArray put(int index, long value) throws JSONException { * 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 boolean isEmpty() { * 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 @@ private void addAll(Iterable iter, boolean wrap) { } } } - + + /** + * 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 @@ private void addAll(Object array, boolean wrap) throws JSONException { * 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 72c0ebd78..18721f7f6 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -147,7 +147,6 @@ public String toString() { * 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 JSONObject(JSONTokener x) throws JSONException { * 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 @@ protected JSONObject(Map m, int recursionDepth) { 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 static Object wrap(Object object) { 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 @@ private static Object wrap(Object object, Set objectsRecord, int recursi 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 000000000..910d1cfa5 --- /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 5f50fc706..349422dcf 100644 --- a/src/test/java/org/json/junit/JSONArrayTest.java +++ b/src/test/java/org/json/junit/JSONArrayTest.java @@ -28,6 +28,7 @@ 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 void testRecursiveDepthArray() { } @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 void testRecursiveDepthArrayLimitedMaps() { } @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 e157fd58e..1a911d8a9 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.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 void testCircularReferenceMultipleLevel() { } @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 void issue743SerializationMapWith999Objects() { @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(); }