From a490ebdb7852768364a9e577f89c3871c4d3392c Mon Sep 17 00:00:00 2001 From: Andrei Paikin Date: Sat, 19 May 2018 09:42:21 +0300 Subject: [PATCH 1/3] add isEmpty and isNotEmpty methods --- JSONObject.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/JSONObject.java b/JSONObject.java index d18a556..a6ece64 100644 --- a/JSONObject.java +++ b/JSONObject.java @@ -963,6 +963,24 @@ 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(); + } + + /** + * Check if JSONObject is not empty. + * + * @return true if JSONObject is not empty, otherwise false. + */ + public boolean isNotEmpty() { + return !map.isEmpty(); + } + /** * Produce a JSONArray containing the names of the elements of this * JSONObject. From 05074386d3582e9ea98148eaf6fd44eeaca964da Mon Sep 17 00:00:00 2001 From: Andrei_Paikin Date: Mon, 21 May 2018 16:58:13 +0300 Subject: [PATCH 2/3] change length comparison to isEmpty method --- CDL.java | 6 +++--- JSONArray.java | 12 +++++++++++- JSONML.java | 4 ++-- JSONObject.java | 18 ++++-------------- XML.java | 10 +++++----- 5 files changed, 25 insertions(+), 25 deletions(-) diff --git a/CDL.java b/CDL.java index 1c7df32..5f907bf 100644 --- a/CDL.java +++ b/CDL.java @@ -224,7 +224,7 @@ public class CDL { */ public static JSONArray toJSONArray(JSONArray names, JSONTokener x) throws JSONException { - if (names == null || names.length() == 0) { + if (names == null || names.isEmpty()) { return null; } JSONArray ja = new JSONArray(); @@ -235,7 +235,7 @@ public class CDL { } ja.put(jo); } - if (ja.length() == 0) { + if (ja.isEmpty()) { return null; } return ja; @@ -272,7 +272,7 @@ public class CDL { */ public static String toString(JSONArray names, JSONArray ja) throws JSONException { - if (names == null || names.length() == 0) { + if (names == null || names.isEmpty()) { return null; } StringBuffer sb = new StringBuffer(); 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/JSONML.java b/JSONML.java index acec7b8..f639050 100644 --- a/JSONML.java +++ b/JSONML.java @@ -178,7 +178,7 @@ public class JSONML { newjo.accumulate(attribute, ""); } } - if (arrayForm && newjo.length() > 0) { + if (arrayForm && !newjo.isEmpty()) { newja.put(newjo); } @@ -208,7 +208,7 @@ public class JSONML { "' and '" + closeTag + "'"); } tagName = null; - if (!arrayForm && newja.length() > 0) { + if (!arrayForm && !newja.isEmpty()) { newjo.put("childNodes", newja); } if (ja == null) { diff --git a/JSONObject.java b/JSONObject.java index a6ece64..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()]); } /** @@ -972,15 +971,6 @@ public class JSONObject { return map.isEmpty(); } - /** - * Check if JSONObject is not empty. - * - * @return true if JSONObject is not empty, otherwise false. - */ - public boolean isNotEmpty() { - return !map.isEmpty(); - } - /** * Produce a JSONArray containing the names of the elements of this * JSONObject. @@ -1966,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; } @@ -2245,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(); diff --git a/XML.java b/XML.java index 55362b2..f3eaa15 100644 --- a/XML.java +++ b/XML.java @@ -277,7 +277,7 @@ public class XML { if ("CDATA".equals(token)) { if (x.next() == '[') { string = x.nextCDATA(); - if (string.length() > 0) { + if (!string.isEmpty()) { context.accumulate("content", string); } return false; @@ -353,7 +353,7 @@ public class XML { if (x.nextToken() != GT) { throw x.syntaxError("Misshaped tag"); } - if (jsonobject.length() > 0) { + if (!jsonobject.isEmpty()) { context.accumulate(tagName, jsonobject); } else { context.accumulate(tagName, ""); @@ -371,7 +371,7 @@ public class XML { return false; } else if (token instanceof String) { string = (String) token; - if (string.length() > 0) { + if (!string.isEmpty()) { jsonobject.accumulate("content", keepStrings ? string : stringToValue(string)); } @@ -379,7 +379,7 @@ public class XML { } else if (token == LT) { // Nested element if (parse(x, jsonobject, tagName,keepStrings)) { - if (jsonobject.length() == 0) { + if (jsonobject.isEmpty()) { context.accumulate(tagName, ""); } else if (jsonobject.length() == 1 && jsonobject.opt("content") != null) { @@ -676,7 +676,7 @@ public class XML { string = (object == null) ? "null" : escape(object.toString()); return (tagName == null) ? "\"" + string + "\"" - : (string.length() == 0) ? "<" + tagName + "/>" : "<" + tagName + : (string.isEmpty()) ? "<" + tagName + "/>" : "<" + tagName + ">" + string + ""; } From 7cad4c3b262f2bb052f6c813f725b74285dce871 Mon Sep 17 00:00:00 2001 From: Andrei Paikin Date: Fri, 25 May 2018 22:17:03 +0300 Subject: [PATCH 3/3] partially revert changes --- CDL.java | 6 +++--- JSONML.java | 4 ++-- XML.java | 10 +++++----- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/CDL.java b/CDL.java index 5f907bf..1c7df32 100644 --- a/CDL.java +++ b/CDL.java @@ -224,7 +224,7 @@ public class CDL { */ public static JSONArray toJSONArray(JSONArray names, JSONTokener x) throws JSONException { - if (names == null || names.isEmpty()) { + if (names == null || names.length() == 0) { return null; } JSONArray ja = new JSONArray(); @@ -235,7 +235,7 @@ public class CDL { } ja.put(jo); } - if (ja.isEmpty()) { + if (ja.length() == 0) { return null; } return ja; @@ -272,7 +272,7 @@ public class CDL { */ public static String toString(JSONArray names, JSONArray ja) throws JSONException { - if (names == null || names.isEmpty()) { + if (names == null || names.length() == 0) { return null; } StringBuffer sb = new StringBuffer(); diff --git a/JSONML.java b/JSONML.java index f639050..acec7b8 100644 --- a/JSONML.java +++ b/JSONML.java @@ -178,7 +178,7 @@ public class JSONML { newjo.accumulate(attribute, ""); } } - if (arrayForm && !newjo.isEmpty()) { + if (arrayForm && newjo.length() > 0) { newja.put(newjo); } @@ -208,7 +208,7 @@ public class JSONML { "' and '" + closeTag + "'"); } tagName = null; - if (!arrayForm && !newja.isEmpty()) { + if (!arrayForm && newja.length() > 0) { newjo.put("childNodes", newja); } if (ja == null) { diff --git a/XML.java b/XML.java index f3eaa15..55362b2 100644 --- a/XML.java +++ b/XML.java @@ -277,7 +277,7 @@ public class XML { if ("CDATA".equals(token)) { if (x.next() == '[') { string = x.nextCDATA(); - if (!string.isEmpty()) { + if (string.length() > 0) { context.accumulate("content", string); } return false; @@ -353,7 +353,7 @@ public class XML { if (x.nextToken() != GT) { throw x.syntaxError("Misshaped tag"); } - if (!jsonobject.isEmpty()) { + if (jsonobject.length() > 0) { context.accumulate(tagName, jsonobject); } else { context.accumulate(tagName, ""); @@ -371,7 +371,7 @@ public class XML { return false; } else if (token instanceof String) { string = (String) token; - if (!string.isEmpty()) { + if (string.length() > 0) { jsonobject.accumulate("content", keepStrings ? string : stringToValue(string)); } @@ -379,7 +379,7 @@ public class XML { } else if (token == LT) { // Nested element if (parse(x, jsonobject, tagName,keepStrings)) { - if (jsonobject.isEmpty()) { + if (jsonobject.length() == 0) { context.accumulate(tagName, ""); } else if (jsonobject.length() == 1 && jsonobject.opt("content") != null) { @@ -676,7 +676,7 @@ public class XML { string = (object == null) ? "null" : escape(object.toString()); return (tagName == null) ? "\"" + string + "\"" - : (string.isEmpty()) ? "<" + tagName + "/>" : "<" + tagName + : (string.length() == 0) ? "<" + tagName + "/>" : "<" + tagName + ">" + string + ""; }