diff --git a/JSONArray.java b/JSONArray.java index 71c277aea..20ad476b1 100644 --- a/JSONArray.java +++ b/JSONArray.java @@ -31,6 +31,7 @@ of this software and associated documentation files (the "Software"), to deal import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; +import java.util.List; import java.util.Map; /** @@ -237,6 +238,29 @@ public double getDouble(int index) throws JSONException { "] is not a number."); } } + + + /** + * Get the double value associated with an index as boxed Double object making it possible to express null. + * + * @param index The index must be between 0 and length() - 1. + * @return The value as Double or null. + * @throws JSONException If the key is not found or if the value cannot + * be converted to a number. + */ + public Double getDoubleObject(int index) throws JSONException { + Object object = this.get(index); + if (object.equals(JSONObject.NULL)) + return null; + try { + return object instanceof Number + ? ((Number)object).doubleValue() + : Double.parseDouble((String)object); + } catch (Exception e) { + throw new JSONException("JSONArray[" + index + + "] is not a number."); + } + } /** @@ -257,6 +281,28 @@ public int getInt(int index) throws JSONException { "] is not a number."); } } + + + /** + * Get the int value associated with an index as boxed Integer making it possible to express null. + * + * @param index The index must be between 0 and length() - 1. + * @return The value as Integer or null. + * @throws JSONException If the key is not found or if the value is not a number. + */ + public Integer getIntObject(int index) throws JSONException { + Object object = this.get(index); + if (object.equals(JSONObject.NULL)) + return null; + try { + return object instanceof Number + ? ((Number)object).intValue() + : Integer.parseInt((String)object); + } catch (Exception e) { + throw new JSONException("JSONArray[" + index + + "] is not a number."); + } + } /** @@ -268,6 +314,8 @@ public int getInt(int index) throws JSONException { */ public JSONArray getJSONArray(int index) throws JSONException { Object object = this.get(index); + if (object.equals(JSONObject.NULL)) + return null; if (object instanceof JSONArray) { return (JSONArray)object; } @@ -285,6 +333,8 @@ public JSONArray getJSONArray(int index) throws JSONException { */ public JSONObject getJSONObject(int index) throws JSONException { Object object = this.get(index); + if (object.equals(JSONObject.NULL)) + return null; if (object instanceof JSONObject) { return (JSONObject)object; } @@ -312,6 +362,29 @@ public long getLong(int index) throws JSONException { "] is not a number."); } } + + + /** + * Get the long value associated with an index as boxed Long object maing it possible to express null values. + * + * @param index The index must be between 0 and length() - 1. + * @return The value as Long or null. + * @throws JSONException If the key is not found or if the value cannot + * be converted to a number. + */ + public Long getLongObject(int index) throws JSONException { + Object object = this.get(index); + if (object.equals(JSONObject.NULL)) + return null; + try { + return object instanceof Number + ? ((Number)object).longValue() + : Long.parseLong((String)object); + } catch (Exception e) { + throw new JSONException("JSONArray[" + index + + "] is not a number."); + } + } /** @@ -322,6 +395,8 @@ public long getLong(int index) throws JSONException { */ public String getString(int index) throws JSONException { Object object = this.get(index); + if (object.equals(JSONObject.NULL)) + return null; if (object instanceof String) { return (String)object; } @@ -903,4 +978,8 @@ Writer write(Writer writer, int indentFactor, int indent) throw new JSONException(e); } } + + public List shallowClonedList() { + return new ArrayList(this.myArrayList); + } } diff --git a/JSONObject.java b/JSONObject.java index 5b05255dc..98d70f267 100755 --- a/JSONObject.java +++ b/JSONObject.java @@ -37,6 +37,9 @@ of this software and associated documentation files (the "Software"), to deal import java.util.Locale; import java.util.Map; import java.util.ResourceBundle; +import java.util.Set; + +import org.apache.log4j.Logger; /** * A JSONObject is an unordered collection of name/value pairs. Its external @@ -595,7 +598,7 @@ public static String[] getNames(JSONObject jo) { if (length == 0) { return null; } - Iterator iterator = jo.keys(); + Iterator iterator = jo.keys().iterator(); String[] names = new String[length]; int i = 0; while (iterator.hasNext()) { @@ -701,8 +704,8 @@ public boolean isNull(String key) { * * @return An iterator of the keys. */ - public Iterator keys() { - return this.map.keySet().iterator(); + public Set keys() { + return this.map.keySet(); } @@ -724,7 +727,7 @@ public int length() { */ public JSONArray names() { JSONArray ja = new JSONArray(); - Iterator keys = this.keys(); + Iterator keys = this.keys().iterator(); while (keys.hasNext()) { ja.put(keys.next()); } @@ -1347,6 +1350,7 @@ public String toString() { try { return this.toString(0); } catch (Exception e) { + Logger.getLogger("JSON").error("toString() caused the following error:", e); return null; } } @@ -1497,6 +1501,9 @@ public Writer write(Writer writer) throws JSONException { static final Writer writeValue(Writer writer, Object value, int indentFactor, int indent) throws JSONException, IOException { + // support Java null as well + if (value == null) + value = JSONObject.NULL; if (value instanceof JSONObject) { ((JSONObject) value).write(writer, indentFactor, indent); } else if (value instanceof JSONArray) { @@ -1548,7 +1555,7 @@ Writer write(Writer writer, int indentFactor, int indent) try { boolean commanate = false; final int length = this.length(); - Iterator keys = this.keys(); + Iterator keys = this.keys().iterator(); writer.write('{'); if (length == 1) {