Skip to content

Commit 870fa03

Browse files
authored
Merge pull request #529 from ethauvin/master
Added putAll(Collection) and putAll(Array) methods.
2 parents 98cd8ef + 0d13e56 commit 870fa03

2 files changed

Lines changed: 109 additions & 13 deletions

File tree

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

Lines changed: 71 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,7 @@ public JSONArray(Collection<?> collection) {
173173
this.myArrayList = new ArrayList<Object>();
174174
} else {
175175
this.myArrayList = new ArrayList<Object>(collection.size());
176-
for (Object o: collection){
177-
this.myArrayList.add(JSONObject.wrap(o));
178-
}
176+
this.addAll(collection);
179177
}
180178
}
181179

@@ -193,16 +191,7 @@ public JSONArray(Collection<?> collection) {
193191
*/
194192
public JSONArray(Object array) throws JSONException {
195193
this();
196-
if (array.getClass().isArray()) {
197-
int length = Array.getLength(array);
198-
this.myArrayList.ensureCapacity(length);
199-
for (int i = 0; i < length; i += 1) {
200-
this.put(JSONObject.wrap(Array.get(array, i)));
201-
}
202-
} else {
203-
throw new JSONException(
204-
"JSONArray initial value should be a string or collection or array.");
205-
}
194+
this.addAll(array);
206195
}
207196

208197
/**
@@ -1174,6 +1163,36 @@ public JSONArray put(int index, Object value) throws JSONException {
11741163
}
11751164
return this.put(value);
11761165
}
1166+
1167+
/**
1168+
* Put or replace a collection's elements in the JSONArray.
1169+
*
1170+
* @param collection
1171+
* A Collection.
1172+
* @return this.
1173+
*/
1174+
public JSONArray putAll(Collection<?> collection) {
1175+
this.addAll(collection);
1176+
return this;
1177+
}
1178+
1179+
/**
1180+
* Put or replace an array's elements in the JSONArray.
1181+
*
1182+
* @param array
1183+
* Array. If the parameter passed is null, or not an array, an
1184+
* exception will be thrown.
1185+
* @return this.
1186+
*
1187+
* @throws JSONException
1188+
* If not an array or if an array value is non-finite number.
1189+
* @throws NullPointerException
1190+
* Thrown if the array parameter is null.
1191+
*/
1192+
public JSONArray putAll(Object array) throws JSONException {
1193+
this.addAll(array);
1194+
return this;
1195+
}
11771196

11781197
/**
11791198
* Creates a JSONPointer using an initialization string and tries to
@@ -1500,6 +1519,45 @@ public List<Object> toList() {
15001519
public boolean isEmpty() {
15011520
return this.myArrayList.isEmpty();
15021521
}
1522+
1523+
1524+
/**
1525+
* Add a collection's elements to the JSONArray.
1526+
*
1527+
* @param collection
1528+
* A Collection.
1529+
*/
1530+
private void addAll(Collection<?> collection) {
1531+
this.myArrayList.ensureCapacity(this.myArrayList.size() + collection.size());
1532+
for (Object o: collection){
1533+
this.myArrayList.add(JSONObject.wrap(o));
1534+
}
1535+
}
1536+
1537+
/**
1538+
* Add an array's elements to the JSONArray.
1539+
*
1540+
* @param array
1541+
* Array. If the parameter passed is null, or not an array, an
1542+
* exception will be thrown.
1543+
*
1544+
* @throws JSONException
1545+
* If not an array or if an array value is non-finite number.
1546+
* @throws NullPointerException
1547+
* Thrown if the array parameter is null.
1548+
*/
1549+
private void addAll(Object array) throws JSONException {
1550+
if (array.getClass().isArray()) {
1551+
int length = Array.getLength(array);
1552+
this.myArrayList.ensureCapacity(this.myArrayList.size() + length);
1553+
for (int i = 0; i < length; i += 1) {
1554+
this.put(JSONObject.wrap(Array.get(array, i)));
1555+
}
1556+
} else {
1557+
throw new JSONException(
1558+
"JSONArray initial value should be a string or collection or array.");
1559+
}
1560+
}
15031561

15041562
/**
15051563
* Create a new JSONException in a common format for incorrect conversions.

src/test/java/org/json/junit/JSONArrayTest.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,44 @@ public void verifyConstructor() {
225225
expected.similar(jaObj));
226226
}
227227

228+
/**
229+
* Tests consecutive calls to putAll with array and collection.
230+
*/
231+
@Test
232+
public void verifyPutAll() {
233+
final JSONArray jsonArray = new JSONArray();
234+
235+
// array
236+
int[] myInts = { 1, 2, 3, 4, 5 };
237+
jsonArray.putAll(myInts);
238+
239+
assertEquals("int arrays lengths should be equal",
240+
jsonArray.length(),
241+
myInts.length);
242+
243+
for (int i = 0; i < myInts.length; i++) {
244+
assertEquals("int arrays elements should be equal",
245+
myInts[i],
246+
jsonArray.getInt(i));
247+
}
248+
249+
// collection
250+
List<String> myList = Arrays.asList("one", "two", "three", "four", "five");
251+
jsonArray.putAll(myList);
252+
253+
int len = myInts.length + myList.size();
254+
255+
assertEquals("arrays lengths should be equal",
256+
jsonArray.length(),
257+
len);
258+
259+
for (int i = 0; i < myList.size(); i++) {
260+
assertEquals("collection elements should be equal",
261+
myList.get(i),
262+
jsonArray.getString(myInts.length + i));
263+
}
264+
}
265+
228266
/**
229267
* Verifies that the put Collection has backwards compatibility with RAW types pre-java5.
230268
*/

0 commit comments

Comments
 (0)