diff --git a/JSONArray.java b/JSONArray.java index 421dd93..fbc1a0f 100644 --- a/JSONArray.java +++ b/JSONArray.java @@ -1352,7 +1352,7 @@ public class JSONArray implements Iterable { * If any of the names are null. */ public JSONObject toJSONObject(JSONArray names) throws JSONException { - if (names == null || names.length() == 0 || this.length() == 0) { + if (names == null || names.isEmpty() || this.isEmpty()) { return null; } JSONObject jo = new JSONObject(names.length()); @@ -1528,4 +1528,14 @@ public class JSONArray implements Iterable { } return results; } + + /** + * Check if JSONArray is empty. + * + * @return true if JSONArray is empty, otherwise false. + */ + public boolean isEmpty() { + return myArrayList.isEmpty(); + } + } diff --git a/JSONObject.java b/JSONObject.java index d18a556..8deb6ba 100644 --- a/JSONObject.java +++ b/JSONObject.java @@ -810,11 +810,10 @@ public class JSONObject { * @return An array of field names, or null if there are no names. */ public static String[] getNames(JSONObject jo) { - int length = jo.length(); - if (length == 0) { + if (jo.isEmpty()) { return null; } - return jo.keySet().toArray(new String[length]); + return jo.keySet().toArray(new String[jo.length()]); } /** @@ -963,6 +962,15 @@ public class JSONObject { return this.map.size(); } + /** + * Check if JSONObject is empty. + * + * @return true if JSONObject is empty, otherwise false. + */ + public boolean isEmpty() { + return map.isEmpty(); + } + /** * Produce a JSONArray containing the names of the elements of this * JSONObject. @@ -1948,7 +1956,7 @@ public class JSONObject { } public static Writer quote(String string, Writer w) throws IOException { - if (string == null || string.length() == 0) { + if (string == null || string.isEmpty()) { w.write("\"\""); return w; } @@ -2227,7 +2235,7 @@ public class JSONObject { * If any of the values are non-finite numbers. */ public JSONArray toJSONArray(JSONArray names) throws JSONException { - if (names == null || names.length() == 0) { + if (names == null || names.isEmpty()) { return null; } JSONArray ja = new JSONArray();