@@ -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