@@ -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 /**
@@ -1330,15 +1359,36 @@ public JSONArray put(int index, long value) throws JSONException {
13301359 * The subscript.
13311360 * @param value
13321361 * The Map value.
1333- * @return this.
1362+ * @return
1363+ * reference to self
13341364 * @throws JSONException
13351365 * If the index is negative or if the value is an invalid
13361366 * number.
13371367 * @throws NullPointerException
13381368 * If a key in the map is <code>null</code>
13391369 */
13401370 public JSONArray put (int index , Map <?, ?> value ) throws JSONException {
1341- this .put (index , new JSONObject (value ));
1371+ this .put (index , new JSONObject (value , new JSONParserConfiguration ()));
1372+ return this ;
1373+ }
1374+
1375+ /**
1376+ * Put a value in the JSONArray, where the value will be a JSONObject that
1377+ * is produced from a Map.
1378+ *
1379+ * @param index
1380+ * The subscript
1381+ * @param value
1382+ * The Map value.
1383+ * @param jsonParserConfiguration
1384+ * Configuration object for the JSON parser
1385+ * @return reference to self
1386+ * @throws JSONException
1387+ * If the index is negative or if the value is an invalid
1388+ * number.
1389+ */
1390+ public JSONArray put (int index , Map <?, ?> value , JSONParserConfiguration jsonParserConfiguration ) throws JSONException {
1391+ this .put (index , new JSONObject (value , jsonParserConfiguration ));
13421392 return this ;
13431393 }
13441394
@@ -1779,13 +1829,14 @@ public boolean isEmpty() {
17791829 * @param wrap
17801830 * {@code true} to call {@link JSONObject#wrap(Object)} for each item,
17811831 * {@code false} to add the items directly
1782- *
1832+ * @param recursionDepth
1833+ * Variable for tracking the count of nested object creations.
17831834 */
1784- private void addAll (Collection <?> collection , boolean wrap ) {
1835+ private void addAll (Collection <?> collection , boolean wrap , int recursionDepth , JSONParserConfiguration jsonParserConfiguration ) {
17851836 this .myArrayList .ensureCapacity (this .myArrayList .size () + collection .size ());
17861837 if (wrap ) {
17871838 for (Object o : collection ){
1788- this .put (JSONObject .wrap (o ));
1839+ this .put (JSONObject .wrap (o , recursionDepth + 1 , jsonParserConfiguration ));
17891840 }
17901841 } else {
17911842 for (Object o : collection ){
@@ -1814,30 +1865,66 @@ private void addAll(Iterable<?> iter, boolean wrap) {
18141865 }
18151866 }
18161867 }
1817-
1868+
18181869 /**
18191870 * Add an array's elements to the JSONArray.
18201871 *
18211872 * @param array
1873+ * Array. If the parameter passed is null, or not an array,
1874+ * JSONArray, Collection, or Iterable, an exception will be
1875+ * thrown.
1876+ * @param wrap
1877+ * {@code true} to call {@link JSONObject#wrap(Object)} for each item,
1878+ * {@code false} to add the items directly
1879+ * @throws JSONException
1880+ * If not an array or if an array value is non-finite number.
1881+ */
1882+ private void addAll (Object array , boolean wrap ) throws JSONException {
1883+ this .addAll (array , wrap , 0 );
1884+ }
1885+
1886+ /**
1887+ * Add an array's elements to the JSONArray.
1888+ *
1889+ * @param array
1890+ * Array. If the parameter passed is null, or not an array,
1891+ * JSONArray, Collection, or Iterable, an exception will be
1892+ * thrown.
1893+ * @param wrap
1894+ * {@code true} to call {@link JSONObject#wrap(Object)} for each item,
1895+ * {@code false} to add the items directly
1896+ * @param recursionDepth
1897+ * Variable for tracking the count of nested object creations.
1898+ */
1899+ private void addAll (Object array , boolean wrap , int recursionDepth ) {
1900+ addAll (array , wrap , recursionDepth , new JSONParserConfiguration ());
1901+ }
1902+ /**
1903+ * Add an array's elements to the JSONArray.
1904+ *`
1905+ * @param array
18221906 * Array. If the parameter passed is null, or not an array,
18231907 * JSONArray, Collection, or Iterable, an exception will be
18241908 * thrown.
18251909 * @param wrap
18261910 * {@code true} to call {@link JSONObject#wrap(Object)} for each item,
18271911 * {@code false} to add the items directly
1828- *
1912+ * @param recursionDepth
1913+ * Variable for tracking the count of nested object creations.
1914+ * @param jsonParserConfiguration
1915+ * Variable to pass parser custom configuration for json parsing.
18291916 * @throws JSONException
18301917 * If not an array or if an array value is non-finite number.
18311918 * @throws NullPointerException
18321919 * Thrown if the array parameter is null.
18331920 */
1834- private void addAll (Object array , boolean wrap ) throws JSONException {
1921+ private void addAll (Object array , boolean wrap , int recursionDepth , JSONParserConfiguration jsonParserConfiguration ) throws JSONException {
18351922 if (array .getClass ().isArray ()) {
18361923 int length = Array .getLength (array );
18371924 this .myArrayList .ensureCapacity (this .myArrayList .size () + length );
18381925 if (wrap ) {
18391926 for (int i = 0 ; i < length ; i += 1 ) {
1840- this .put (JSONObject .wrap (Array .get (array , i )));
1927+ this .put (JSONObject .wrap (Array .get (array , i ), recursionDepth + 1 , jsonParserConfiguration ));
18411928 }
18421929 } else {
18431930 for (int i = 0 ; i < length ; i += 1 ) {
@@ -1850,7 +1937,7 @@ private void addAll(Object array, boolean wrap) throws JSONException {
18501937 // JSONArray
18511938 this .myArrayList .addAll (((JSONArray )array ).myArrayList );
18521939 } else if (array instanceof Collection ) {
1853- this .addAll ((Collection <?>)array , wrap );
1940+ this .addAll ((Collection <?>)array , wrap , recursionDepth );
18541941 } else if (array instanceof Iterable ) {
18551942 this .addAll ((Iterable <?>)array , wrap );
18561943 } else {
0 commit comments