Skip to content

Commit 7a17ae0

Browse files
authored
Merge pull request #421 from strkkk/add-emptiness-methods
add isEmpty and isNotEmpty methods
2 parents 3c1535d + 7cad4c3 commit 7a17ae0

2 files changed

Lines changed: 24 additions & 6 deletions

File tree

JSONArray.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1352,7 +1352,7 @@ public boolean similar(Object other) {
13521352
* If any of the names are null.
13531353
*/
13541354
public JSONObject toJSONObject(JSONArray names) throws JSONException {
1355-
if (names == null || names.length() == 0 || this.length() == 0) {
1355+
if (names == null || names.isEmpty() || this.isEmpty()) {
13561356
return null;
13571357
}
13581358
JSONObject jo = new JSONObject(names.length());
@@ -1528,4 +1528,14 @@ public List<Object> toList() {
15281528
}
15291529
return results;
15301530
}
1531+
1532+
/**
1533+
* Check if JSONArray is empty.
1534+
*
1535+
* @return true if JSONArray is empty, otherwise false.
1536+
*/
1537+
public boolean isEmpty() {
1538+
return myArrayList.isEmpty();
1539+
}
1540+
15311541
}

JSONObject.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -810,11 +810,10 @@ public long getLong(String key) throws JSONException {
810810
* @return An array of field names, or null if there are no names.
811811
*/
812812
public static String[] getNames(JSONObject jo) {
813-
int length = jo.length();
814-
if (length == 0) {
813+
if (jo.isEmpty()) {
815814
return null;
816815
}
817-
return jo.keySet().toArray(new String[length]);
816+
return jo.keySet().toArray(new String[jo.length()]);
818817
}
819818

820819
/**
@@ -963,6 +962,15 @@ public int length() {
963962
return this.map.size();
964963
}
965964

965+
/**
966+
* Check if JSONObject is empty.
967+
*
968+
* @return true if JSONObject is empty, otherwise false.
969+
*/
970+
public boolean isEmpty() {
971+
return map.isEmpty();
972+
}
973+
966974
/**
967975
* Produce a JSONArray containing the names of the elements of this
968976
* JSONObject.
@@ -1948,7 +1956,7 @@ public static String quote(String string) {
19481956
}
19491957

19501958
public static Writer quote(String string, Writer w) throws IOException {
1951-
if (string == null || string.length() == 0) {
1959+
if (string == null || string.isEmpty()) {
19521960
w.write("\"\"");
19531961
return w;
19541962
}
@@ -2227,7 +2235,7 @@ public static void testValidity(Object o) throws JSONException {
22272235
* If any of the values are non-finite numbers.
22282236
*/
22292237
public JSONArray toJSONArray(JSONArray names) throws JSONException {
2230-
if (names == null || names.length() == 0) {
2238+
if (names == null || names.isEmpty()) {
22312239
return null;
22322240
}
22332241
JSONArray ja = new JSONArray();

0 commit comments

Comments
 (0)