diff --git a/JSONArray.java b/JSONArray.java index 776a2bd05..8ac58441b 100644 --- a/JSONArray.java +++ b/JSONArray.java @@ -78,7 +78,7 @@ of this software and associated documentation files (the "Software"), to deal * * * @author JSON.org - * @version 2016-05-20 + * @version 2016-07-19 */ public class JSONArray implements Iterable { @@ -156,9 +156,9 @@ public JSONArray(String source) throws JSONException { public JSONArray(Collection collection) { this.myArrayList = new ArrayList(); if (collection != null) { - for (Object o: collection){ - this.myArrayList.add(JSONObject.wrap(o)); - } + for (Object o : collection) { + this.myArrayList.add(JSONObject.wrap(o)); + } } } @@ -241,11 +241,15 @@ public boolean getBoolean(int index) throws JSONException { public double getDouble(int index) throws JSONException { Object object = this.get(index); try { - return object instanceof Number ? ((Number) object).doubleValue() - : Double.parseDouble((String) object); + if (object instanceof Number) { + return ((Number) object).doubleValue(); + } else if (object instanceof String) { + return Double.parseDouble((String) object); + } } catch (Exception e) { - throw new JSONException("JSONArray[" + index + "] is not a number."); + } + throw new JSONException("JSONArray[" + index + "] is not a number."); } /** @@ -325,11 +329,15 @@ public BigInteger getBigInteger (int index) throws JSONException { public int getInt(int index) throws JSONException { Object object = this.get(index); try { - return object instanceof Number ? ((Number) object).intValue() - : Integer.parseInt((String) object); + if (object instanceof Number) { + return ((Number) object).intValue(); + } else if (object instanceof String) { + return Integer.parseInt((String) object); + } } catch (Exception e) { - throw new JSONException("JSONArray[" + index + "] is not a number."); + } + throw new JSONException("JSONArray[" + index + "] is not a number."); } /** @@ -381,11 +389,15 @@ public JSONObject getJSONObject(int index) throws JSONException { public long getLong(int index) throws JSONException { Object object = this.get(index); try { - return object instanceof Number ? ((Number) object).longValue() - : Long.parseLong((String) object); + if (object instanceof Number) { + return ((Number) object).longValue(); + } else if (object instanceof String) { + return Long.parseLong((String) object); + } } catch (Exception e) { - throw new JSONException("JSONArray[" + index + "] is not a number."); + } + throw new JSONException("JSONArray[" + index + "] is not a number."); } /** @@ -486,11 +498,20 @@ public boolean optBoolean(int index) { * @return The truth. */ public boolean optBoolean(int index, boolean defaultValue) { - try { - return this.getBoolean(index); - } catch (Exception e) { + Object object = this.opt(index); + if (JSONObject.NULL.equals(object)) { return defaultValue; } + if (object.equals(Boolean.FALSE) + || (object instanceof String && ((String) object) + .equalsIgnoreCase("false"))) { + return false; + } else if (object.equals(Boolean.TRUE) + || (object instanceof String && ((String) object) + .equalsIgnoreCase("true"))) { + return true; + } + return defaultValue; } /** @@ -518,11 +539,20 @@ public double optDouble(int index) { * @return The value. */ public double optDouble(int index, double defaultValue) { + Object object = this.opt(index); + if (JSONObject.NULL.equals(object)) { + return defaultValue; + } try { - return this.getDouble(index); + if (object instanceof Number) { + return ((Number) object).doubleValue(); + } else if (object instanceof String) { + return Double.parseDouble((String) object); + } } catch (Exception e) { - return defaultValue; + } + return defaultValue; } /** @@ -550,11 +580,20 @@ public int optInt(int index) { * @return The value. */ public int optInt(int index, int defaultValue) { + Object object = this.opt(index); + if (JSONObject.NULL.equals(object)) { + return defaultValue; + } try { - return this.getInt(index); + if (object instanceof Number) { + return ((Number) object).intValue(); + } else if (object instanceof String) { + return Integer.parseInt((String) object); + } } catch (Exception e) { - return defaultValue; + } + return defaultValue; } /** @@ -615,8 +654,12 @@ public > E optEnum(Class clazz, int index, E defaultValue) * @return The value. */ public BigInteger optBigInteger(int index, BigInteger defaultValue) { + Object object = this.opt(index); + if (JSONObject.NULL.equals(object)) { + return defaultValue; + } try { - return this.getBigInteger(index); + return new BigInteger(object.toString()); } catch (Exception e) { return defaultValue; } @@ -634,8 +677,12 @@ public BigInteger optBigInteger(int index, BigInteger defaultValue) { * @return The value. */ public BigDecimal optBigDecimal(int index, BigDecimal defaultValue) { + Object object = this.opt(index); + if (JSONObject.NULL.equals(object)) { + return defaultValue; + } try { - return this.getBigDecimal(index); + return new BigDecimal(object.toString()); } catch (Exception e) { return defaultValue; } @@ -693,11 +740,20 @@ public long optLong(int index) { * @return The value. */ public long optLong(int index, long defaultValue) { + Object object = this.opt(index); + if (JSONObject.NULL.equals(object)) { + return defaultValue; + } try { - return this.getLong(index); + if (object instanceof Number) { + return ((Number) object).longValue(); + } else if (object instanceof String) { + return Long.parseLong((String) object); + } } catch (Exception e) { - return defaultValue; + } + return defaultValue; } /** @@ -961,7 +1017,7 @@ public JSONArray put(int index, Object value) throws JSONException { } /** - * Creates a JSONPointer using an intialization string and tries to + * Creates a JSONPointer using an initialization string and tries to * match it to an item within this JSONArray. For example, given a * JSONArray initialized with this document: *
@@ -1081,6 +1137,7 @@ public JSONObject toJSONObject(JSONArray names) throws JSONException {
      * @return a printable, displayable, transmittable representation of the
      *         array.
      */
+    @Override
     public String toString() {
         try {
             return this.toString(0);
diff --git a/JSONObject.java b/JSONObject.java
index aa227ff09..8fb0a2569 100644
--- a/JSONObject.java
+++ b/JSONObject.java
@@ -93,7 +93,7 @@ of this software and associated documentation files (the "Software"), to deal
  * 
  *
  * @author JSON.org
- * @version 2016-05-20
+ * @version 2016-07-19
  */
 public class JSONObject {
     /**
@@ -132,6 +132,7 @@ public boolean equals(Object object) {
          *
          * @return The string "null".
          */
+        @Override
         public String toString() {
             return "null";
         }
@@ -242,7 +243,7 @@ public JSONObject(JSONTokener x) throws JSONException {
     public JSONObject(Map map) {
         this.map = new HashMap();
         if (map != null) {
-        	for (final Entry e : map.entrySet()) {
+            for (final Entry e : map.entrySet()) {
                 final Object value = e.getValue();
                 if (value != null) {
                     this.map.put(String.valueOf(e.getKey()), wrap(value));
@@ -576,12 +577,15 @@ public BigDecimal getBigDecimal(String key) throws JSONException {
     public double getDouble(String key) throws JSONException {
         Object object = this.get(key);
         try {
-            return object instanceof Number ? ((Number) object).doubleValue()
-                    : Double.parseDouble((String) object);
+            if (object instanceof Number) {
+                return ((Number) object).doubleValue();
+            } else if (object instanceof String) {
+                return Double.parseDouble((String) object);
+            }
         } catch (Exception e) {
-            throw new JSONException("JSONObject[" + quote(key)
-                    + "] is not a number.");
         }
+        throw new JSONException("JSONObject[" + quote(key)
+                + "] is not a number.");
     }
 
     /**
@@ -597,12 +601,15 @@ public double getDouble(String key) throws JSONException {
     public int getInt(String key) throws JSONException {
         Object object = this.get(key);
         try {
-            return object instanceof Number ? ((Number) object).intValue()
-                    : Integer.parseInt((String) object);
+            if (object instanceof Number) {
+                return ((Number) object).intValue();
+            } else if (object instanceof String) {
+                return Integer.parseInt((String) object);
+            }
         } catch (Exception e) {
-            throw new JSONException("JSONObject[" + quote(key)
-                    + "] is not an int.");
+
         }
+        throw new JSONException("JSONObject[" + quote(key) + "] is not an int.");
     }
 
     /**
@@ -654,12 +661,15 @@ public JSONObject getJSONObject(String key) throws JSONException {
     public long getLong(String key) throws JSONException {
         Object object = this.get(key);
         try {
-            return object instanceof Number ? ((Number) object).longValue()
-                    : Long.parseLong((String) object);
+            if (object instanceof Number) {
+                return ((Number) object).longValue();
+            } else if (object instanceof String) {
+                return Long.parseLong((String) object);
+            }
         } catch (Exception e) {
-            throw new JSONException("JSONObject[" + quote(key)
-                    + "] is not a long.");
+
         }
+        throw new JSONException("JSONObject[" + quote(key) + "] is not a long.");
     }
 
     /**
@@ -932,11 +942,20 @@ public boolean optBoolean(String key) {
      * @return The truth.
      */
     public boolean optBoolean(String key, boolean defaultValue) {
-        try {
-            return this.getBoolean(key);
-        } catch (Exception e) {
+        Object object = this.get(key);
+        if (NULL.equals(object)) {
             return defaultValue;
         }
+        if (object.equals(Boolean.FALSE)
+                || (object instanceof String && ((String) object)
+                        .equalsIgnoreCase("false"))) {
+            return false;
+        } else if (object.equals(Boolean.TRUE)
+                || (object instanceof String && ((String) object)
+                        .equalsIgnoreCase("true"))) {
+            return true;
+        }
+        return defaultValue;
     }
 
     /**
@@ -964,8 +983,12 @@ public double optDouble(String key) {
      * @return An object which is the value.
      */
     public BigInteger optBigInteger(String key, BigInteger defaultValue) {
+        Object object = this.get(key);
+        if (NULL.equals(object)) {
+            return defaultValue;
+        }
         try {
-            return this.getBigInteger(key);
+            return new BigInteger(object.toString());
         } catch (Exception e) {
             return defaultValue;
         }
@@ -983,8 +1006,12 @@ public BigInteger optBigInteger(String key, BigInteger defaultValue) {
      * @return An object which is the value.
      */
     public BigDecimal optBigDecimal(String key, BigDecimal defaultValue) {
+        Object object = this.opt(key);
+        if (NULL.equals(object)) {
+            return defaultValue;
+        }
         try {
-            return this.getBigDecimal(key);
+            return new BigDecimal(object.toString());
         } catch (Exception e) {
             return defaultValue;
         }
@@ -1002,11 +1029,20 @@ public BigDecimal optBigDecimal(String key, BigDecimal defaultValue) {
      * @return An object which is the value.
      */
     public double optDouble(String key, double defaultValue) {
+        Object object = this.get(key);
+        if (NULL.equals(object)) {
+            return defaultValue;
+        }
         try {
-            return this.getDouble(key);
+            if (object instanceof Number) {
+                return ((Number) object).doubleValue();
+            } else if (object instanceof String) {
+                return Double.parseDouble((String) object);
+            }
         } catch (Exception e) {
-            return defaultValue;
+
         }
+        return defaultValue;
     }
 
     /**
@@ -1034,11 +1070,20 @@ public int optInt(String key) {
      * @return An object which is the value.
      */
     public int optInt(String key, int defaultValue) {
+        Object object = this.get(key);
+        if (NULL.equals(object)) {
+            return defaultValue;
+        }
         try {
-            return this.getInt(key);
+            if (object instanceof Number) {
+                return ((Number) object).intValue();
+            } else if (object instanceof String) {
+                return Integer.parseInt((String) object);
+            }
         } catch (Exception e) {
-            return defaultValue;
+
         }
+        return defaultValue;
     }
 
     /**
@@ -1092,11 +1137,20 @@ public long optLong(String key) {
      * @return An object which is the value.
      */
     public long optLong(String key, long defaultValue) {
+        Object object = this.get(key);
+        if (NULL.equals(object)) {
+            return defaultValue;
+        }
         try {
-            return this.getLong(key);
+            if (object instanceof Number) {
+                return ((Number) object).longValue();
+            } else if (object instanceof String) {
+                return Long.parseLong((String) object);
+            }
         } catch (Exception e) {
-            return defaultValue;
+
         }
+        return defaultValue;
     }
 
     /**
@@ -1621,6 +1675,7 @@ public JSONArray toJSONArray(JSONArray names) throws JSONException {
      *         brace) and ending with } (right
      *         brace).
      */
+    @Override
     public String toString() {
         try {
             return this.toString(0);
@@ -1880,13 +1935,13 @@ public Writer write(Writer writer, int indentFactor, int indent)
     }
 
     /**
-     * Returns a java.util.Map containing all of the entrys in this object.
+     * Returns a java.util.Map containing all of the entries in this object.
      * If an entry in the object is a JSONArray or JSONObject it will also
      * be converted.
      * 

* Warning: This method assumes that the data structure is acyclical. * - * @return a java.util.Map containing the entrys of this object + * @return a java.util.Map containing the entries of this object */ public Map toMap() { Map results = new HashMap();