@@ -149,11 +149,18 @@ public JSONArray(String source) throws JSONException {
149149 * A Collection.
150150 */
151151 public JSONArray (Collection <?> collection ) {
152+ this (collection , 0 );
153+ }
154+
155+ protected JSONArray (Collection <?> collection , int recursionDepth ) {
156+ if (recursionDepth > JSONObject .RECURSION_DEPTH_LIMIT ) {
157+ throw new JSONException ("JSONArray has reached recursion depth limit of " + JSONObject .RECURSION_DEPTH_LIMIT );
158+ }
152159 if (collection == null ) {
153160 this .myArrayList = new ArrayList <Object >();
154161 } else {
155162 this .myArrayList = new ArrayList <Object >(collection .size ());
156- this .addAll (collection , true );
163+ this .addAll (collection , true , recursionDepth );
157164 }
158165 }
159166
@@ -205,7 +212,7 @@ public JSONArray(Object array) throws JSONException {
205212 throw new JSONException (
206213 "JSONArray initial value should be a string or collection or array." );
207214 }
208- this .addAll (array , true );
215+ this .addAll (array , true , 0 );
209216 }
210217
211218 /**
@@ -1779,13 +1786,15 @@ public boolean isEmpty() {
17791786 * @param wrap
17801787 * {@code true} to call {@link JSONObject#wrap(Object)} for each item,
17811788 * {@code false} to add the items directly
1789+ * @param recursionDepth
1790+ * variable to keep the count of how nested the object creation is happening.
17821791 *
17831792 */
1784- private void addAll (Collection <?> collection , boolean wrap ) {
1793+ private void addAll (Collection <?> collection , boolean wrap , int recursionDepth ) {
17851794 this .myArrayList .ensureCapacity (this .myArrayList .size () + collection .size ());
17861795 if (wrap ) {
17871796 for (Object o : collection ){
1788- this .put (JSONObject .wrap (o ));
1797+ this .put (JSONObject .wrap (o , recursionDepth + 1 ));
17891798 }
17901799 } else {
17911800 for (Object o : collection ){
@@ -1815,6 +1824,10 @@ private void addAll(Iterable<?> iter, boolean wrap) {
18151824 }
18161825 }
18171826
1827+ private void addAll (Object array , boolean wrap ) throws JSONException {
1828+ this .addAll (array , wrap , 0 );
1829+ }
1830+
18181831 /**
18191832 * Add an array's elements to the JSONArray.
18201833 *
@@ -1825,19 +1838,21 @@ private void addAll(Iterable<?> iter, boolean wrap) {
18251838 * @param wrap
18261839 * {@code true} to call {@link JSONObject#wrap(Object)} for each item,
18271840 * {@code false} to add the items directly
1841+ * @param recursionDepth
1842+ * Variable to keep the count of how nested the object creation is happening.
18281843 *
18291844 * @throws JSONException
18301845 * If not an array or if an array value is non-finite number.
18311846 * @throws NullPointerException
18321847 * Thrown if the array parameter is null.
18331848 */
1834- private void addAll (Object array , boolean wrap ) throws JSONException {
1849+ private void addAll (Object array , boolean wrap , int recursionDepth ) throws JSONException {
18351850 if (array .getClass ().isArray ()) {
18361851 int length = Array .getLength (array );
18371852 this .myArrayList .ensureCapacity (this .myArrayList .size () + length );
18381853 if (wrap ) {
18391854 for (int i = 0 ; i < length ; i += 1 ) {
1840- this .put (JSONObject .wrap (Array .get (array , i )));
1855+ this .put (JSONObject .wrap (Array .get (array , i ), recursionDepth + 1 ));
18411856 }
18421857 } else {
18431858 for (int i = 0 ; i < length ; i += 1 ) {
@@ -1850,7 +1865,7 @@ private void addAll(Object array, boolean wrap) throws JSONException {
18501865 // JSONArray
18511866 this .myArrayList .addAll (((JSONArray )array ).myArrayList );
18521867 } else if (array instanceof Collection ) {
1853- this .addAll ((Collection <?>)array , wrap );
1868+ this .addAll ((Collection <?>)array , wrap , recursionDepth );
18541869 } else if (array instanceof Iterable ) {
18551870 this .addAll ((Iterable <?>)array , wrap );
18561871 } else {
0 commit comments