1
0
Fork 0
mirror of https://github.com/ethauvin/JSON-java.git synced 2025-06-17 07:50:52 -07:00

Merge pull request #421 from strkkk/add-emptiness-methods

add isEmpty and isNotEmpty methods
This commit is contained in:
Sean Leary 2018-05-26 09:56:23 -05:00 committed by GitHub
commit 7a17ae0b3e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 6 deletions

View file

@ -1352,7 +1352,7 @@ public class JSONArray implements Iterable<Object> {
* If any of the names are null. * If any of the names are null.
*/ */
public JSONObject toJSONObject(JSONArray names) throws JSONException { 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; return null;
} }
JSONObject jo = new JSONObject(names.length()); JSONObject jo = new JSONObject(names.length());
@ -1528,4 +1528,14 @@ public class JSONArray implements Iterable<Object> {
} }
return results; return results;
} }
/**
* Check if JSONArray is empty.
*
* @return true if JSONArray is empty, otherwise false.
*/
public boolean isEmpty() {
return myArrayList.isEmpty();
}
} }

View file

@ -810,11 +810,10 @@ public class JSONObject {
* @return An array of field names, or null if there are no names. * @return An array of field names, or null if there are no names.
*/ */
public static String[] getNames(JSONObject jo) { public static String[] getNames(JSONObject jo) {
int length = jo.length(); if (jo.isEmpty()) {
if (length == 0) {
return null; 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(); 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 * Produce a JSONArray containing the names of the elements of this
* JSONObject. * JSONObject.
@ -1948,7 +1956,7 @@ public class JSONObject {
} }
public static Writer quote(String string, Writer w) throws IOException { public static Writer quote(String string, Writer w) throws IOException {
if (string == null || string.length() == 0) { if (string == null || string.isEmpty()) {
w.write("\"\""); w.write("\"\"");
return w; return w;
} }
@ -2227,7 +2235,7 @@ public class JSONObject {
* If any of the values are non-finite numbers. * If any of the values are non-finite numbers.
*/ */
public JSONArray toJSONArray(JSONArray names) throws JSONException { public JSONArray toJSONArray(JSONArray names) throws JSONException {
if (names == null || names.length() == 0) { if (names == null || names.isEmpty()) {
return null; return null;
} }
JSONArray ja = new JSONArray(); JSONArray ja = new JSONArray();