-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Updates for JSONArray.putAll methods #552
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
f37c2d6
3c9573c
c175a9e
5d828d2
f35194b
d30ecad
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -173,7 +173,37 @@ public JSONArray(Collection<?> collection) { | |
| this.myArrayList = new ArrayList<Object>(); | ||
| } else { | ||
| this.myArrayList = new ArrayList<Object>(collection.size()); | ||
| this.addAll(collection); | ||
| this.addAll(collection, true); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Construct a JSONArray from an Iterable. This is a shallow copy. | ||
| * | ||
| * @param iter | ||
| * A Iterable collection. | ||
| */ | ||
| public JSONArray(Iterable<?> iter) { | ||
| this(); | ||
| if (iter == null) { | ||
| return; | ||
| } | ||
| this.addAll(iter, true); | ||
| } | ||
|
|
||
| /** | ||
| * Construct a JSONArray from another JSONArray. This is a shallow copy. | ||
| * | ||
| * @param array | ||
| * A array. | ||
| */ | ||
| public JSONArray(JSONArray array) { | ||
stleary marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (array == null) { | ||
| this.myArrayList = new ArrayList<Object>(); | ||
| } else { | ||
| // shallow copy directly the internal array lists as any wrapping | ||
| // should have been done already in the original JSONArray | ||
| this.myArrayList = new ArrayList<Object>(array.myArrayList); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -191,7 +221,11 @@ public JSONArray(Collection<?> collection) { | |
| */ | ||
| public JSONArray(Object array) throws JSONException { | ||
| this(); | ||
| this.addAll(array); | ||
| if (!array.getClass().isArray()) { | ||
| throw new JSONException( | ||
| "JSONArray initial value should be a string or collection or array."); | ||
| } | ||
| this.addAll(array, true); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -1165,32 +1199,58 @@ public JSONArray put(int index, Object value) throws JSONException { | |
| } | ||
|
|
||
| /** | ||
| * Put or replace a collection's elements in the JSONArray. | ||
| * Put a collection's elements in to the JSONArray. | ||
| * | ||
| * @param collection | ||
| * A Collection. | ||
| * @return this. | ||
| */ | ||
| public JSONArray putAll(Collection<?> collection) { | ||
| this.addAll(collection); | ||
| this.addAll(collection, false); | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Put an Iterable's elements in to the JSONArray. | ||
| * | ||
| * @param iter | ||
| * An Iterable. | ||
| * @return this. | ||
| */ | ||
| public JSONArray putAll(Iterable<?> iter) { | ||
| this.addAll(iter, false); | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Put or replace an array's elements in the JSONArray. | ||
| * Put a JSONArray's elements in to the JSONArray. | ||
| * | ||
| * @param array | ||
| * Array. If the parameter passed is null, or not an array, an | ||
| * A JSONArray. | ||
| * @return this. | ||
| */ | ||
| public JSONArray putAll(JSONArray array) { | ||
| // 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; | ||
| } | ||
|
|
||
| /** | ||
| * Put an array's elements in to the JSONArray. | ||
| * | ||
| * @param array | ||
| * Array. If the parameter passed is null, or not an array or Iterable, an | ||
| * exception will be thrown. | ||
| * @return this. | ||
| * | ||
| * @throws JSONException | ||
| * If not an array or if an array value is non-finite number. | ||
| * If not an array, JSONArray, Iterable or if an value is non-finite number. | ||
| * @throws NullPointerException | ||
| * Thrown if the array parameter is null. | ||
| */ | ||
| public JSONArray putAll(Object array) throws JSONException { | ||
| this.addAll(array); | ||
| this.addAll(array, false); | ||
| return this; | ||
| } | ||
|
|
||
|
|
@@ -1520,39 +1580,88 @@ public boolean isEmpty() { | |
| return this.myArrayList.isEmpty(); | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Add a collection's elements to the JSONArray. | ||
| * | ||
| * @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); | ||
| } | ||
| } | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just noticed the @stleary some guidance on what to do in this case would be appreciated.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so, to this effect, I actually worked around it in my own copy by adding the wrap calls to the |
||
| } | ||
|
|
||
| /** | ||
| * Add an Iterable's elements to the JSONArray. | ||
| * | ||
| * @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, boolean wrap) { | ||
| if (wrap) { | ||
| for (Object o: iter){ | ||
| this.put(JSONObject.wrap(o)); | ||
| } | ||
| } else { | ||
| for (Object o: iter){ | ||
| this.put(o); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * 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. | ||
| * @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 | ||
| // wrapping should have been completed in the original | ||
| // JSONArray | ||
| this.myArrayList.addAll(((JSONArray)array).myArrayList); | ||
| } else if (array instanceof Collection) { | ||
| this.addAll((Collection<?>)array, wrap); | ||
| } else if (array instanceof Iterable) { | ||
| this.addAll((Iterable<?>)array, wrap); | ||
| } else { | ||
| throw new JSONException( | ||
| "JSONArray initial value should be a string or collection or array."); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.