Skip to content

Commit f37c2d6

Browse files
author
John J. Aylward
committed
Update for JSONArray.putAll methods
* Adds a copy constructor for JSONArray * Updates the JSONArray.addAll(Object) method to be more lenient * Adds support for JSONArray.putAll of generic Iterables * Adds support for JSONArray.putAll of another JSONArray
1 parent 6351fa6 commit f37c2d6

3 files changed

Lines changed: 158 additions & 5 deletions

File tree

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

Lines changed: 82 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,35 @@ public JSONArray(Collection<?> collection) {
177177
}
178178
}
179179

180+
/**
181+
* Construct a JSONArray from an Iterable. This is a shallow copy.
182+
*
183+
* @param iter
184+
* A Iterable collection.
185+
*/
186+
public JSONArray(Iterable<?> iter) {
187+
this();
188+
if (iter == null) {
189+
return;
190+
}
191+
this.addAll(iter);
192+
}
193+
194+
/**
195+
* Construct a JSONArray from another JSONArray. This is a shallow copy.
196+
*
197+
* @param collection
198+
* A Collection.
199+
*/
200+
public JSONArray(JSONArray array) {
201+
if (array == null) {
202+
this.myArrayList = new ArrayList<Object>();
203+
} else {
204+
this.myArrayList = new ArrayList<Object>(array.length());
205+
this.addAll(array.myArrayList);
206+
}
207+
}
208+
180209
/**
181210
* Construct a JSONArray from an array.
182211
*
@@ -191,6 +220,10 @@ public JSONArray(Collection<?> collection) {
191220
*/
192221
public JSONArray(Object array) throws JSONException {
193222
this();
223+
if (!array.getClass().isArray()) {
224+
throw new JSONException(
225+
"JSONArray initial value should be a string or collection or array.");
226+
}
194227
this.addAll(array);
195228
}
196229

@@ -210,6 +243,11 @@ public JSONArray(int initialCapacity) throws JSONException {
210243
this.myArrayList = new ArrayList<Object>(initialCapacity);
211244
}
212245

246+
@Override
247+
protected Object clone() {
248+
return new JSONArray(this.myArrayList);
249+
}
250+
213251
@Override
214252
public Iterator<Object> iterator() {
215253
return this.myArrayList.iterator();
@@ -1165,7 +1203,7 @@ public JSONArray put(int index, Object value) throws JSONException {
11651203
}
11661204

11671205
/**
1168-
* Put or replace a collection's elements in the JSONArray.
1206+
* Put a collection's elements in to the JSONArray.
11691207
*
11701208
* @param collection
11711209
* A Collection.
@@ -1175,9 +1213,33 @@ public JSONArray putAll(Collection<?> collection) {
11751213
this.addAll(collection);
11761214
return this;
11771215
}
1216+
1217+
/**
1218+
* Put an Iterable's elements in to the JSONArray.
1219+
*
1220+
* @param iter
1221+
* A Collection.
1222+
* @return this.
1223+
*/
1224+
public JSONArray putAll(Iterable<?> iter) {
1225+
this.addAll(iter);
1226+
return this;
1227+
}
11781228

11791229
/**
1180-
* Put or replace an array's elements in the JSONArray.
1230+
* Put a JSONArray's elements in to the JSONArray.
1231+
*
1232+
* @param array
1233+
* A JSONArray.
1234+
* @return this.
1235+
*/
1236+
public JSONArray putAll(JSONArray array) {
1237+
this.addAll(array.myArrayList);
1238+
return this;
1239+
}
1240+
1241+
/**
1242+
* Put an array's elements in to the JSONArray.
11811243
*
11821244
* @param array
11831245
* Array. If the parameter passed is null, or not an array, an
@@ -1520,7 +1582,6 @@ public boolean isEmpty() {
15201582
return this.myArrayList.isEmpty();
15211583
}
15221584

1523-
15241585
/**
15251586
* Add a collection's elements to the JSONArray.
15261587
*
@@ -1534,6 +1595,18 @@ private void addAll(Collection<?> collection) {
15341595
}
15351596
}
15361597

1598+
/**
1599+
* Add an Iterable's elements to the JSONArray.
1600+
*
1601+
* @param iter
1602+
* An Iterable.
1603+
*/
1604+
private void addAll(Iterable<?> iter) {
1605+
for (Object o: iter){
1606+
this.myArrayList.add(JSONObject.wrap(o));
1607+
}
1608+
}
1609+
15371610
/**
15381611
* Add an array's elements to the JSONArray.
15391612
*
@@ -1553,6 +1626,12 @@ private void addAll(Object array) throws JSONException {
15531626
for (int i = 0; i < length; i += 1) {
15541627
this.put(JSONObject.wrap(Array.get(array, i)));
15551628
}
1629+
} else if (array instanceof JSONArray) {
1630+
this.addAll(((JSONArray)array).myArrayList);
1631+
} else if (array instanceof Collection) {
1632+
this.addAll((Collection<?>)array);
1633+
} else if (array instanceof Iterable) {
1634+
this.addAll((Iterable<?>)array);
15561635
} else {
15571636
throw new JSONException(
15581637
"JSONArray initial value should be a string or collection or array.");

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

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -979,9 +979,9 @@ public void write() throws IOException {
979979
JSONArray jsonArray = new JSONArray(str);
980980
String expectedStr = str;
981981
StringWriter stringWriter = new StringWriter();
982-
jsonArray.write(stringWriter);
983-
String actualStr = stringWriter.toString();
984982
try {
983+
jsonArray.write(stringWriter);
984+
String actualStr = stringWriter.toString();
985985
JSONArray finalArray = new JSONArray(actualStr);
986986
Util.compareActualVsExpectedJsonArrays(jsonArray, finalArray);
987987
assertTrue("write() expected " + expectedStr +
@@ -1187,4 +1187,71 @@ public void testJSONArrayInt() {
11871187
e.getMessage());
11881188
}
11891189
}
1190+
1191+
/**
1192+
* Verifies that the object constructor can properly handle any supported collection object.
1193+
*/
1194+
@Test
1195+
@SuppressWarnings({ "unchecked", "boxing" })
1196+
public void testObjectConstructor() {
1197+
// should copy the array
1198+
Object o = new Object[] {2, "test2", true};
1199+
JSONArray a = new JSONArray(o);
1200+
assertNotNull("Should not error", a);
1201+
assertEquals("length", 3, a.length());
1202+
1203+
// should NOT copy the collection
1204+
// this is required for backwards compatibility
1205+
o = new ArrayList<Object>();
1206+
((Collection<Object>)o).add(1);
1207+
((Collection<Object>)o).add("test");
1208+
((Collection<Object>)o).add(false);
1209+
try {
1210+
a = new JSONArray(o);
1211+
assertNull("Should error", a);
1212+
} catch (JSONException ex) {
1213+
}
1214+
1215+
// should NOT copy the JSONArray
1216+
// this is required for backwards compatibility
1217+
o = a;
1218+
try {
1219+
a = new JSONArray(o);
1220+
assertNull("Should error", a);
1221+
} catch (JSONException ex) {
1222+
}
1223+
}
1224+
1225+
/**
1226+
* Verifies that the JSONArray constructor properly copies the original.
1227+
*/
1228+
@Test
1229+
public void testJSONArrayConstructor() {
1230+
// should copy the array
1231+
JSONArray a1 = new JSONArray("[2, \"test2\", true]");
1232+
JSONArray a2 = new JSONArray(a1);
1233+
assertNotNull("Should not error", a2);
1234+
assertEquals("length", a1.length(), a2.length());
1235+
1236+
for(int i = 0; i < a1.length(); i++) {
1237+
assertEquals("index " + i + " are equal", a1.get(i), a2.get(i));
1238+
}
1239+
}
1240+
1241+
/**
1242+
* Verifies that the object constructor can properly handle any supported collection object.
1243+
*/
1244+
@Test
1245+
public void testJSONArrayPutAll() {
1246+
// should copy the array
1247+
JSONArray a1 = new JSONArray("[2, \"test2\", true]");
1248+
JSONArray a2 = new JSONArray();
1249+
a2.putAll(a1);
1250+
assertNotNull("Should not error", a2);
1251+
assertEquals("length", a1.length(), a2.length());
1252+
1253+
for(int i = 0; i < a1.length(); i++) {
1254+
assertEquals("index " + i + " are equal", a1.get(i), a2.get(i));
1255+
}
1256+
}
11901257
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3201,4 +3201,11 @@ public void testPutNullObject() {
32013201
fail("Expected an exception");
32023202
}
32033203

3204+
@Test
3205+
public void testIssue548ObjectWithEmptyJsonArray() {
3206+
JSONObject jsonObject = new JSONObject("{\"empty_json_array\": []}");
3207+
assertTrue("missing expected key 'empty_json_array'", jsonObject.has("empty_json_array"));
3208+
assertNotNull("'empty_json_array' should be an array", jsonObject.getJSONArray("empty_json_array"));
3209+
assertEquals("'empty_json_array' should have a length of 0", 0, jsonObject.getJSONArray("empty_json_array").length());
3210+
}
32043211
}

0 commit comments

Comments
 (0)