Skip to content

Commit 26f4848

Browse files
authored
Merge pull request #577 from stranck/master
Added clear() methods to JSONObject and JSONArray
2 parents a57eff2 + efad1d7 commit 26f4848

4 files changed

Lines changed: 46 additions & 0 deletions

File tree

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,14 @@ public int length() {
567567
return this.myArrayList.size();
568568
}
569569

570+
/**
571+
* Removes all of the elements from this JSONArray.
572+
* The JSONArray will be empty after this call returns.
573+
*/
574+
public void clear() {
575+
this.myArrayList.clear();
576+
}
577+
570578
/**
571579
* Get the optional object value associated with an index.
572580
*

src/main/java/org/json/JSONObject.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -973,6 +973,14 @@ public int length() {
973973
return this.map.size();
974974
}
975975

976+
/**
977+
* Removes all of the elements from this JSONObject.
978+
* The JSONObject will be empty after this call returns.
979+
*/
980+
public void clear() {
981+
this.map.clear();
982+
}
983+
976984
/**
977985
* Check if JSONObject is empty.
978986
*

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,4 +1254,19 @@ public void testJSONArrayPutAll() {
12541254
assertEquals("index " + i + " are equal", a1.get(i), a2.get(i));
12551255
}
12561256
}
1257+
1258+
/**
1259+
* Tests if calling JSONArray clear() method actually makes the JSONArray empty
1260+
*/
1261+
@Test(expected = JSONException.class)
1262+
public void jsonArrayClearMethodTest() {
1263+
//Adds random stuff to the JSONArray
1264+
JSONArray jsonArray = new JSONArray();
1265+
jsonArray.put(123);
1266+
jsonArray.put("456");
1267+
jsonArray.put(new JSONArray());
1268+
jsonArray.clear(); //Clears the JSONArray
1269+
assertTrue("expected jsonArray.length() == 0", jsonArray.length() == 0); //Check if its length is 0
1270+
jsonArray.getInt(0); //Should throws org.json.JSONException: JSONArray[0] not found
1271+
}
12571272
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3215,4 +3215,19 @@ public void testIssue548ObjectWithEmptyJsonArray() {
32153215
assertNotNull("'empty_json_array' should be an array", jsonObject.getJSONArray("empty_json_array"));
32163216
assertEquals("'empty_json_array' should have a length of 0", 0, jsonObject.getJSONArray("empty_json_array").length());
32173217
}
3218+
3219+
/**
3220+
* Tests if calling JSONObject clear() method actually makes the JSONObject empty
3221+
*/
3222+
@Test(expected = JSONException.class)
3223+
public void jsonObjectClearMethodTest() {
3224+
//Adds random stuff to the JSONObject
3225+
JSONObject jsonObject = new JSONObject();
3226+
jsonObject.put("key1", 123);
3227+
jsonObject.put("key2", "456");
3228+
jsonObject.put("key3", new JSONObject());
3229+
jsonObject.clear(); //Clears the JSONObject
3230+
assertTrue("expected jsonObject.length() == 0", jsonObject.length() == 0); //Check if its length is 0
3231+
jsonObject.getInt("key1"); //Should throws org.json.JSONException: JSONObject["asd"] not found
3232+
}
32183233
}

0 commit comments

Comments
 (0)