Skip to content

Commit f35194b

Browse files
author
John J. Aylward
committed
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. ```
1 parent 5d828d2 commit f35194b

1 file changed

Lines changed: 46 additions & 18 deletions

File tree

src/main/java/org/json/JSONArray.java

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ public JSONArray(Collection<?> collection) {
173173
this.myArrayList = new ArrayList<Object>();
174174
} else {
175175
this.myArrayList = new ArrayList<Object>(collection.size());
176-
this.addAll(collection);
176+
this.addAll(collection, true);
177177
}
178178
}
179179

@@ -188,7 +188,7 @@ public JSONArray(Iterable<?> iter) {
188188
if (iter == null) {
189189
return;
190190
}
191-
this.addAll(iter);
191+
this.addAll(iter, true);
192192
}
193193

194194
/**
@@ -225,7 +225,7 @@ public JSONArray(Object array) throws JSONException {
225225
throw new JSONException(
226226
"JSONArray initial value should be a string or collection or array.");
227227
}
228-
this.addAll(array);
228+
this.addAll(array, true);
229229
}
230230

231231
/**
@@ -1206,7 +1206,7 @@ public JSONArray put(int index, Object value) throws JSONException {
12061206
* @return this.
12071207
*/
12081208
public JSONArray putAll(Collection<?> collection) {
1209-
this.addAll(collection);
1209+
this.addAll(collection, false);
12101210
return this;
12111211
}
12121212

@@ -1218,7 +1218,7 @@ public JSONArray putAll(Collection<?> collection) {
12181218
* @return this.
12191219
*/
12201220
public JSONArray putAll(Iterable<?> iter) {
1221-
this.addAll(iter);
1221+
this.addAll(iter, false);
12221222
return this;
12231223
}
12241224

@@ -1250,7 +1250,7 @@ public JSONArray putAll(JSONArray array) {
12501250
* Thrown if the array parameter is null.
12511251
*/
12521252
public JSONArray putAll(Object array) throws JSONException {
1253-
this.addAll(array);
1253+
this.addAll(array, false);
12541254
return this;
12551255
}
12561256

@@ -1585,11 +1585,21 @@ public boolean isEmpty() {
15851585
*
15861586
* @param collection
15871587
* A Collection.
1588+
* @param wrap
1589+
* {@code true} to call {@link JSONObject#wrap(Object)} for each item,
1590+
* {@code false} to add the items directly
1591+
*
15881592
*/
1589-
private void addAll(Collection<?> collection) {
1593+
private void addAll(Collection<?> collection, boolean wrap) {
15901594
this.myArrayList.ensureCapacity(this.myArrayList.size() + collection.size());
1591-
for (Object o: collection){
1592-
this.myArrayList.add(JSONObject.wrap(o));
1595+
if (wrap) {
1596+
for (Object o: collection){
1597+
this.put(JSONObject.wrap(o));
1598+
}
1599+
} else {
1600+
for (Object o: collection){
1601+
this.put(o);
1602+
}
15931603
}
15941604
}
15951605

@@ -1598,10 +1608,19 @@ private void addAll(Collection<?> collection) {
15981608
*
15991609
* @param iter
16001610
* An Iterable.
1601-
*/
1602-
private void addAll(Iterable<?> iter) {
1603-
for (Object o: iter){
1604-
this.myArrayList.add(JSONObject.wrap(o));
1611+
* @param wrap
1612+
* {@code true} to call {@link JSONObject#wrap(Object)} for each item,
1613+
* {@code false} to add the items directly
1614+
*/
1615+
private void addAll(Iterable<?> iter, boolean wrap) {
1616+
if (wrap) {
1617+
for (Object o: iter){
1618+
this.put(JSONObject.wrap(o));
1619+
}
1620+
} else {
1621+
for (Object o: iter){
1622+
this.put(o);
1623+
}
16051624
}
16061625
}
16071626

@@ -1612,28 +1631,37 @@ private void addAll(Iterable<?> iter) {
16121631
* Array. If the parameter passed is null, or not an array,
16131632
* JSONArray, Collection, or Iterable, an exception will be
16141633
* thrown.
1634+
* @param wrap
1635+
* {@code true} to call {@link JSONObject#wrap(Object)} for each item,
1636+
* {@code false} to add the items directly
16151637
*
16161638
* @throws JSONException
16171639
* If not an array or if an array value is non-finite number.
16181640
* @throws NullPointerException
16191641
* Thrown if the array parameter is null.
16201642
*/
1621-
private void addAll(Object array) throws JSONException {
1643+
private void addAll(Object array, boolean wrap) throws JSONException {
16221644
if (array.getClass().isArray()) {
16231645
int length = Array.getLength(array);
16241646
this.myArrayList.ensureCapacity(this.myArrayList.size() + length);
1625-
for (int i = 0; i < length; i += 1) {
1626-
this.put(JSONObject.wrap(Array.get(array, i)));
1647+
if (wrap) {
1648+
for (int i = 0; i < length; i += 1) {
1649+
this.put(JSONObject.wrap(Array.get(array, i)));
1650+
}
1651+
} else {
1652+
for (int i = 0; i < length; i += 1) {
1653+
this.put(Array.get(array, i));
1654+
}
16271655
}
16281656
} else if (array instanceof JSONArray) {
16291657
// use the built in array list `addAll` as all object
16301658
// wrapping should have been completed in the original
16311659
// JSONArray
16321660
this.myArrayList.addAll(((JSONArray)array).myArrayList);
16331661
} else if (array instanceof Collection) {
1634-
this.addAll((Collection<?>)array);
1662+
this.addAll((Collection<?>)array, wrap);
16351663
} else if (array instanceof Iterable) {
1636-
this.addAll((Iterable<?>)array);
1664+
this.addAll((Iterable<?>)array, wrap);
16371665
} else {
16381666
throw new JSONException(
16391667
"JSONArray initial value should be a string or collection or array.");

0 commit comments

Comments
 (0)