From 5ca334bcc6b09be9c30992e0ff2a018c48ac7b64 Mon Sep 17 00:00:00 2001 From: Frank Hempel Date: Tue, 29 May 2012 19:09:32 +0200 Subject: [PATCH 1/5] added support for Java null #Please enter the commit message for your changes. Lines starting --- JSONObject.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/JSONObject.java b/JSONObject.java index 5b05255dc..57cc2d32c 100755 --- a/JSONObject.java +++ b/JSONObject.java @@ -1497,6 +1497,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) { From b36431785623c47109da65d8c129219c145c375d Mon Sep 17 00:00:00 2001 From: Frank Hempel Date: Mon, 4 Jun 2012 14:41:31 +0200 Subject: [PATCH 2/5] JSONObject.keys() now returns an Iterable (from which one can draw an Iterator) --- JSONObject.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/JSONObject.java b/JSONObject.java index 57cc2d32c..ccf228c49 100755 --- a/JSONObject.java +++ b/JSONObject.java @@ -595,7 +595,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 +701,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 +724,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()); } @@ -1551,7 +1551,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) { From 4e32adca18ca1fdfc8aff8598a38fbb8caf086df Mon Sep 17 00:00:00 2001 From: Frank Hempel Date: Mon, 4 Jun 2012 14:50:48 +0200 Subject: [PATCH 3/5] JSONObject.toString(): print error message before returning null in case of an error --- JSONObject.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/JSONObject.java b/JSONObject.java index ccf228c49..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 @@ -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; } } From 4b54b98e3bd7a8154c5fdf836f836ae97fa9dd4e Mon Sep 17 00:00:00 2001 From: Frank Hempel Date: Mon, 4 Jun 2012 15:34:58 +0200 Subject: [PATCH 4/5] added JSONArray.shallowClonedList() --- JSONArray.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/JSONArray.java b/JSONArray.java index 71c277aea..4e84eaecf 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; /** @@ -903,4 +904,8 @@ Writer write(Writer writer, int indentFactor, int indent) throw new JSONException(e); } } + + public List shallowClonedList() { + return new ArrayList(this.myArrayList); + } } From f761abe4b69ea5f1d2822d16c6d1299c20d95ca6 Mon Sep 17 00:00:00 2001 From: Frank Hempel Date: Mon, 4 Jun 2012 20:25:08 +0200 Subject: [PATCH 5/5] more Java-null support --- JSONArray.java | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/JSONArray.java b/JSONArray.java index 4e84eaecf..20ad476b1 100644 --- a/JSONArray.java +++ b/JSONArray.java @@ -238,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."); + } + } /** @@ -258,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."); + } + } /** @@ -269,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; } @@ -286,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; } @@ -313,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."); + } + } /** @@ -323,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; }