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.
```
This commit is contained in:
John J. Aylward 2020-07-30 09:51:24 -04:00
parent 5d828d2c0b
commit f35194bc1d

View File

@ -173,7 +173,7 @@ public class JSONArray implements Iterable<Object> {
this.myArrayList = new ArrayList<Object>(); this.myArrayList = new ArrayList<Object>();
} else { } else {
this.myArrayList = new ArrayList<Object>(collection.size()); this.myArrayList = new ArrayList<Object>(collection.size());
this.addAll(collection); this.addAll(collection, true);
} }
} }
@ -188,7 +188,7 @@ public class JSONArray implements Iterable<Object> {
if (iter == null) { if (iter == null) {
return; return;
} }
this.addAll(iter); this.addAll(iter, true);
} }
/** /**
@ -225,7 +225,7 @@ public class JSONArray implements Iterable<Object> {
throw new JSONException( throw new JSONException(
"JSONArray initial value should be a string or collection or array."); "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<Object> {
* @return this. * @return this.
*/ */
public JSONArray putAll(Collection<?> collection) { public JSONArray putAll(Collection<?> collection) {
this.addAll(collection); this.addAll(collection, false);
return this; return this;
} }
@ -1218,7 +1218,7 @@ public class JSONArray implements Iterable<Object> {
* @return this. * @return this.
*/ */
public JSONArray putAll(Iterable<?> iter) { public JSONArray putAll(Iterable<?> iter) {
this.addAll(iter); this.addAll(iter, false);
return this; return this;
} }
@ -1250,7 +1250,7 @@ public class JSONArray implements Iterable<Object> {
* Thrown if the array parameter is null. * Thrown if the array parameter is null.
*/ */
public JSONArray putAll(Object array) throws JSONException { public JSONArray putAll(Object array) throws JSONException {
this.addAll(array); this.addAll(array, false);
return this; return this;
} }
@ -1585,11 +1585,21 @@ public class JSONArray implements Iterable<Object> {
* *
* @param collection * @param collection
* A 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()); this.myArrayList.ensureCapacity(this.myArrayList.size() + collection.size());
if (wrap) {
for (Object o: collection){ for (Object o: collection){
this.myArrayList.add(JSONObject.wrap(o)); this.put(JSONObject.wrap(o));
}
} else {
for (Object o: collection){
this.put(o);
}
} }
} }
@ -1598,10 +1608,19 @@ public class JSONArray implements Iterable<Object> {
* *
* @param iter * @param iter
* An Iterable. * 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) { private void addAll(Iterable<?> iter, boolean wrap) {
if (wrap) {
for (Object o: iter){ for (Object o: iter){
this.myArrayList.add(JSONObject.wrap(o)); this.put(JSONObject.wrap(o));
}
} else {
for (Object o: iter){
this.put(o);
}
} }
} }
@ -1612,28 +1631,37 @@ public class JSONArray implements Iterable<Object> {
* Array. If the parameter passed is null, or not an array, * Array. If the parameter passed is null, or not an array,
* JSONArray, Collection, or Iterable, an exception will be * JSONArray, Collection, or Iterable, an exception will be
* thrown. * thrown.
* @param wrap
* {@code true} to call {@link JSONObject#wrap(Object)} for each item,
* {@code false} to add the items directly
* *
* @throws JSONException * @throws JSONException
* If not an array or if an array value is non-finite number. * If not an array or if an array value is non-finite number.
* @throws NullPointerException * @throws NullPointerException
* Thrown if the array parameter is null. * 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()) { if (array.getClass().isArray()) {
int length = Array.getLength(array); int length = Array.getLength(array);
this.myArrayList.ensureCapacity(this.myArrayList.size() + length); this.myArrayList.ensureCapacity(this.myArrayList.size() + length);
if (wrap) {
for (int i = 0; i < length; i += 1) { for (int i = 0; i < length; i += 1) {
this.put(JSONObject.wrap(Array.get(array, i))); 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) { } else if (array instanceof JSONArray) {
// use the built in array list `addAll` as all object // use the built in array list `addAll` as all object
// wrapping should have been completed in the original // wrapping should have been completed in the original
// JSONArray // JSONArray
this.myArrayList.addAll(((JSONArray)array).myArrayList); this.myArrayList.addAll(((JSONArray)array).myArrayList);
} else if (array instanceof Collection) { } else if (array instanceof Collection) {
this.addAll((Collection<?>)array); this.addAll((Collection<?>)array, wrap);
} else if (array instanceof Iterable) { } else if (array instanceof Iterable) {
this.addAll((Iterable<?>)array); this.addAll((Iterable<?>)array, wrap);
} else { } else {
throw new JSONException( throw new JSONException(
"JSONArray initial value should be a string or collection or array."); "JSONArray initial value should be a string or collection or array.");