@@ -149,11 +149,40 @@ public JSONArray(String source) throws JSONException {
149149 * A Collection.
150150 */
151151 public JSONArray (Collection <?> collection ) {
152+ this (collection , 0 , new JSONParserConfiguration ());
153+ }
154+
155+ /**
156+ * Construct a JSONArray from a Collection.
157+ *
158+ * @param collection
159+ * A Collection.
160+ * @param jsonParserConfiguration
161+ * Configuration object for the JSON parser
162+ */
163+ public JSONArray (Collection <?> collection , JSONParserConfiguration jsonParserConfiguration ) {
164+ this (collection , 0 , jsonParserConfiguration );
165+ }
166+
167+ /**
168+ * Construct a JSONArray from a collection with recursion depth.
169+ *
170+ * @param collection
171+ * A Collection.
172+ * @param recursionDepth
173+ * Variable for tracking the count of nested object creations.
174+ * @param jsonParserConfiguration
175+ * Configuration object for the JSON parser
176+ */
177+ JSONArray (Collection <?> collection , int recursionDepth , JSONParserConfiguration jsonParserConfiguration ) {
178+ if (recursionDepth > jsonParserConfiguration .getMaxNestingDepth ()) {
179+ throw new JSONException ("JSONArray has reached recursion depth limit of " + jsonParserConfiguration .getMaxNestingDepth ());
180+ }
152181 if (collection == null ) {
153182 this .myArrayList = new ArrayList <Object >();
154183 } else {
155184 this .myArrayList = new ArrayList <Object >(collection .size ());
156- this .addAll (collection , true );
185+ this .addAll (collection , true , recursionDepth , jsonParserConfiguration );
157186 }
158187 }
159188
@@ -205,7 +234,7 @@ public JSONArray(Object array) throws JSONException {
205234 throw new JSONException (
206235 "JSONArray initial value should be a string or collection or array." );
207236 }
208- this .addAll (array , true );
237+ this .addAll (array , true , 0 );
209238 }
210239
211240 /**
@@ -1338,7 +1367,27 @@ public JSONArray put(int index, long value) throws JSONException {
13381367 * If a key in the map is <code>null</code>
13391368 */
13401369 public JSONArray put (int index , Map <?, ?> value ) throws JSONException {
1341- this .put (index , new JSONObject (value ));
1370+ this .put (index , new JSONObject (value , new JSONParserConfiguration ()));
1371+ return this ;
1372+ }
1373+
1374+ /**
1375+ * Put a value in the JSONArray, where the value will be a JSONObject that
1376+ * is produced from a Map.
1377+ *
1378+ * @param index
1379+ * The subscript
1380+ * @param value
1381+ * The Map value.
1382+ * @param jsonParserConfiguration
1383+ * Configuration object for the JSON parser
1384+ * @return
1385+ * @throws JSONException
1386+ * If the index is negative or if the value is an invalid
1387+ * number.
1388+ */
1389+ public JSONArray put (int index , Map <?, ?> value , JSONParserConfiguration jsonParserConfiguration ) throws JSONException {
1390+ this .put (index , new JSONObject (value , jsonParserConfiguration ));
13421391 return this ;
13431392 }
13441393
@@ -1779,13 +1828,14 @@ public boolean isEmpty() {
17791828 * @param wrap
17801829 * {@code true} to call {@link JSONObject#wrap(Object)} for each item,
17811830 * {@code false} to add the items directly
1782- *
1831+ * @param recursionDepth
1832+ * Variable for tracking the count of nested object creations.
17831833 */
1784- private void addAll (Collection <?> collection , boolean wrap ) {
1834+ private void addAll (Collection <?> collection , boolean wrap , int recursionDepth , JSONParserConfiguration jsonParserConfiguration ) {
17851835 this .myArrayList .ensureCapacity (this .myArrayList .size () + collection .size ());
17861836 if (wrap ) {
17871837 for (Object o : collection ){
1788- this .put (JSONObject .wrap (o ));
1838+ this .put (JSONObject .wrap (o , recursionDepth + 1 , jsonParserConfiguration ));
17891839 }
17901840 } else {
17911841 for (Object o : collection ){
@@ -1814,7 +1864,24 @@ private void addAll(Iterable<?> iter, boolean wrap) {
18141864 }
18151865 }
18161866 }
1817-
1867+
1868+ /**
1869+ * Add an array's elements to the JSONArray.
1870+ *
1871+ * @param array
1872+ * Array. If the parameter passed is null, or not an array,
1873+ * JSONArray, Collection, or Iterable, an exception will be
1874+ * thrown.
1875+ * @param wrap
1876+ * {@code true} to call {@link JSONObject#wrap(Object)} for each item,
1877+ * {@code false} to add the items directly
1878+ * @throws JSONException
1879+ * If not an array or if an array value is non-finite number.
1880+ */
1881+ private void addAll (Object array , boolean wrap ) throws JSONException {
1882+ this .addAll (array , wrap , 0 );
1883+ }
1884+
18181885 /**
18191886 * Add an array's elements to the JSONArray.
18201887 *
@@ -1823,21 +1890,40 @@ private void addAll(Iterable<?> iter, boolean wrap) {
18231890 * JSONArray, Collection, or Iterable, an exception will be
18241891 * thrown.
18251892 * @param wrap
1893+ * {@code true} to call {@link JSONObject#wrap(Object)} for each item,
1894+ * {@code false} to add the items directly
1895+ * @param recursionDepth
1896+ * Variable for tracking the count of nested object creations.
1897+ */
1898+ private void addAll (Object array , boolean wrap , int recursionDepth ) {
1899+ addAll (array , wrap , recursionDepth , new JSONParserConfiguration ());
1900+ }
1901+ /**
1902+ * Add an array's elements to the JSONArray.
1903+ *`
1904+ * @param array
1905+ * Array. If the parameter passed is null, or not an array,
1906+ * JSONArray, Collection, or Iterable, an exception will be
1907+ * thrown.
1908+ * @param wrap
18261909 * {@code true} to call {@link JSONObject#wrap(Object)} for each item,
18271910 * {@code false} to add the items directly
1828- *
1911+ * @param recursionDepth
1912+ * Variable for tracking the count of nested object creations.
1913+ * @param jsonParserConfiguration
1914+ * Variable to pass parser custom configuration for json parsing.
18291915 * @throws JSONException
18301916 * If not an array or if an array value is non-finite number.
18311917 * @throws NullPointerException
18321918 * Thrown if the array parameter is null.
18331919 */
1834- private void addAll (Object array , boolean wrap ) throws JSONException {
1920+ private void addAll (Object array , boolean wrap , int recursionDepth , JSONParserConfiguration jsonParserConfiguration ) throws JSONException {
18351921 if (array .getClass ().isArray ()) {
18361922 int length = Array .getLength (array );
18371923 this .myArrayList .ensureCapacity (this .myArrayList .size () + length );
18381924 if (wrap ) {
18391925 for (int i = 0 ; i < length ; i += 1 ) {
1840- this .put (JSONObject .wrap (Array .get (array , i )));
1926+ this .put (JSONObject .wrap (Array .get (array , i ), recursionDepth + 1 , jsonParserConfiguration ));
18411927 }
18421928 } else {
18431929 for (int i = 0 ; i < length ; i += 1 ) {
@@ -1850,7 +1936,7 @@ private void addAll(Object array, boolean wrap) throws JSONException {
18501936 // JSONArray
18511937 this .myArrayList .addAll (((JSONArray )array ).myArrayList );
18521938 } else if (array instanceof Collection ) {
1853- this .addAll ((Collection <?>)array , wrap );
1939+ this .addAll ((Collection <?>)array , wrap , recursionDepth );
18541940 } else if (array instanceof Iterable ) {
18551941 this .addAll ((Iterable <?>)array , wrap );
18561942 } else {
0 commit comments