From f35194bc1de99846ba1f86ebf0f55867fe838527 Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Thu, 30 Jul 2020 09:51:24 -0400 Subject: [PATCH] Updates the `addAll` methods to have optional wrapping. When called from the constructor, the individual items in the collection/array are wrapped as done originally before the `putAll` methods were added. However this commit changes how `putAll` works. The items are no longer wrapped in order to keep consistency with the other `put` methods. However this can lead to inconsistencies with expectations. For example code like this will create a mixed JSONArray, some items wrapped, others not: ```java SomeBean[] myArr = new SomeBean[]{ new SomeBean(1), new SomeBean(2) }; JSONArray jArr = new JSONArray(myArr); // these will be wrapped // these will not be wrapped jArr.putAll(new SomeBean[]{ new SomeBean(3), new SomeBean(4) }); ``` For consistency, it would be recommended that the above code is changed to look like 1 of 2 ways. Option 1: ```Java SomeBean[] myArr = new SomeBean[]{ new SomeBean(1), new SomeBean(2) }; JSONArray jArr = new JSONArray(); jArr.putAll(myArr); // will not be wrapped // these will not be wrapped jArr.putAll(new SomeBean[]{ new SomeBean(3), new SomeBean(4) }); // our jArr is now consistent. ``` Option 2: ```Java SomeBean[] myArr = new SomeBean[]{ new SomeBean(1), new SomeBean(2) }; JSONArray jArr = new JSONArray(myArr); // these will be wrapped // these will be wrapped jArr.putAll(new JSONArray(new SomeBean[]{ new SomeBean(3), new SomeBean(4) })); // our jArr is now consistent. ``` --- src/main/java/org/json/JSONArray.java | 62 +++++++++++++++++++-------- 1 file changed, 45 insertions(+), 17 deletions(-) diff --git a/src/main/java/org/json/JSONArray.java b/src/main/java/org/json/JSONArray.java index f034499..f11b328 100644 --- a/src/main/java/org/json/JSONArray.java +++ b/src/main/java/org/json/JSONArray.java @@ -173,7 +173,7 @@ public class JSONArray implements Iterable { this.myArrayList = new ArrayList(); } else { this.myArrayList = new ArrayList(collection.size()); - this.addAll(collection); + this.addAll(collection, true); } } @@ -188,7 +188,7 @@ public class JSONArray implements Iterable { if (iter == null) { return; } - this.addAll(iter); + this.addAll(iter, true); } /** @@ -225,7 +225,7 @@ public class JSONArray implements Iterable { throw new JSONException( "JSONArray initial value should be a string or collection or array."); } - this.addAll(array); + this.addAll(array, true); } /** @@ -1206,7 +1206,7 @@ public class JSONArray implements Iterable { * @return this. */ public JSONArray putAll(Collection collection) { - this.addAll(collection); + this.addAll(collection, false); return this; } @@ -1218,7 +1218,7 @@ public class JSONArray implements Iterable { * @return this. */ public JSONArray putAll(Iterable iter) { - this.addAll(iter); + this.addAll(iter, false); return this; } @@ -1250,7 +1250,7 @@ public class JSONArray implements Iterable { * Thrown if the array parameter is null. */ public JSONArray putAll(Object array) throws JSONException { - this.addAll(array); + this.addAll(array, false); return this; } @@ -1585,11 +1585,21 @@ public class JSONArray implements Iterable { * * @param collection * A Collection. + * @param wrap + * {@code true} to call {@link JSONObject#wrap(Object)} for each item, + * {@code false} to add the items directly + * */ - private void addAll(Collection collection) { + private void addAll(Collection collection, boolean wrap) { this.myArrayList.ensureCapacity(this.myArrayList.size() + collection.size()); - for (Object o: collection){ - this.myArrayList.add(JSONObject.wrap(o)); + if (wrap) { + for (Object o: collection){ + this.put(JSONObject.wrap(o)); + } + } else { + for (Object o: collection){ + this.put(o); + } } } @@ -1598,10 +1608,19 @@ public class JSONArray implements Iterable { * * @param iter * An Iterable. + * @param wrap + * {@code true} to call {@link JSONObject#wrap(Object)} for each item, + * {@code false} to add the items directly */ - private void addAll(Iterable iter) { - for (Object o: iter){ - this.myArrayList.add(JSONObject.wrap(o)); + private void addAll(Iterable iter, boolean wrap) { + if (wrap) { + for (Object o: iter){ + this.put(JSONObject.wrap(o)); + } + } else { + for (Object o: iter){ + this.put(o); + } } } @@ -1612,18 +1631,27 @@ public class JSONArray implements Iterable { * 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 * * @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) throws JSONException { + private void addAll(Object array, boolean wrap) throws JSONException { if (array.getClass().isArray()) { int length = Array.getLength(array); this.myArrayList.ensureCapacity(this.myArrayList.size() + length); - for (int i = 0; i < length; i += 1) { - this.put(JSONObject.wrap(Array.get(array, i))); + if (wrap) { + for (int i = 0; i < length; i += 1) { + this.put(JSONObject.wrap(Array.get(array, i))); + } + } else { + for (int i = 0; i < length; i += 1) { + this.put(Array.get(array, i)); + } } } else if (array instanceof JSONArray) { // use the built in array list `addAll` as all object @@ -1631,9 +1659,9 @@ public class JSONArray implements Iterable { // JSONArray this.myArrayList.addAll(((JSONArray)array).myArrayList); } else if (array instanceof Collection) { - this.addAll((Collection)array); + this.addAll((Collection)array, wrap); } else if (array instanceof Iterable) { - this.addAll((Iterable)array); + this.addAll((Iterable)array, wrap); } else { throw new JSONException( "JSONArray initial value should be a string or collection or array.");