From 5d828d2c0b462dc22b9414a5d27067b4bd40aa24 Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Wed, 29 Jul 2020 16:13:54 -0400 Subject: [PATCH] update comments and increase speed of merging JSONArray objects --- src/main/java/org/json/JSONArray.java | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/json/JSONArray.java b/src/main/java/org/json/JSONArray.java index 7cba255..f034499 100644 --- a/src/main/java/org/json/JSONArray.java +++ b/src/main/java/org/json/JSONArray.java @@ -194,15 +194,16 @@ public class JSONArray implements Iterable { /** * Construct a JSONArray from another JSONArray. This is a shallow copy. * - * @param collection - * A Collection. + * @param array + * A array. */ public JSONArray(JSONArray array) { if (array == null) { this.myArrayList = new ArrayList(); } else { - this.myArrayList = new ArrayList(array.length()); - this.addAll(array.myArrayList); + // shallow copy directly the internal array lists as any wrapping + // should have been done already in the original JSONArray + this.myArrayList = new ArrayList(array.myArrayList); } } @@ -1213,7 +1214,7 @@ public class JSONArray implements Iterable { * Put an Iterable's elements in to the JSONArray. * * @param iter - * A Collection. + * An Iterable. * @return this. */ public JSONArray putAll(Iterable iter) { @@ -1229,7 +1230,9 @@ public class JSONArray implements Iterable { * @return this. */ public JSONArray putAll(JSONArray array) { - this.addAll(array.myArrayList); + // directly copy the elements from the source array to this one + // as all wrapping should have been done already in the source. + this.myArrayList.addAll(array.myArrayList); return this; } @@ -1606,8 +1609,9 @@ 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, an - * exception will be thrown. + * Array. If the parameter passed is null, or not an array, + * JSONArray, Collection, or Iterable, an exception will be + * thrown. * * @throws JSONException * If not an array or if an array value is non-finite number. @@ -1622,7 +1626,10 @@ public class JSONArray implements Iterable { this.put(JSONObject.wrap(Array.get(array, i))); } } else if (array instanceof JSONArray) { - this.addAll(((JSONArray)array).myArrayList); + // use the built in array list `addAll` as all object + // wrapping should have been completed in the original + // JSONArray + this.myArrayList.addAll(((JSONArray)array).myArrayList); } else if (array instanceof Collection) { this.addAll((Collection)array); } else if (array instanceof Iterable) {