X Tutup
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion JSONArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -1352,7 +1352,7 @@ public boolean similar(Object other) {
* 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());
Expand Down Expand Up @@ -1528,4 +1528,14 @@ public List<Object> toList() {
}
return results;
}

/**
* Check if JSONArray is empty.
*
* @return true if JSONArray is empty, otherwise false.
*/
public boolean isEmpty() {
return myArrayList.isEmpty();
}

}
18 changes: 13 additions & 5 deletions JSONObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -810,11 +810,10 @@ public long getLong(String key) throws JSONException {
* @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()]);
}

/**
Expand Down Expand Up @@ -963,6 +962,15 @@ public int length() {
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.
Expand Down Expand Up @@ -1948,7 +1956,7 @@ public static String quote(String string) {
}

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;
}
Expand Down Expand Up @@ -2227,7 +2235,7 @@ public static void testValidity(Object o) throws JSONException {
* 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();
Expand Down
X Tutup