From 3364174b651abd8b11c368c68c52c9e7f3da6a24 Mon Sep 17 00:00:00 2001 From: Michael Dobrovnik Date: Mon, 5 Jan 2015 18:47:20 +0100 Subject: [PATCH 1/3] Use wildcards (?) in generic signatures. Avoid wrong generic method resolution (c.f. http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.5.1 ; example 4.5.1.1.) --- JSONArray.java | 10 +++++----- JSONObject.java | 22 ++++++++++------------ 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/JSONArray.java b/JSONArray.java index 3f05548d5..91a868f10 100644 --- a/JSONArray.java +++ b/JSONArray.java @@ -150,10 +150,10 @@ public JSONArray(String source) throws JSONException { * @param collection * A Collection. */ - public JSONArray(Collection collection) { + public JSONArray(Collection collection) { this.myArrayList = new ArrayList(); if (collection != null) { - Iterator iter = collection.iterator(); + Iterator iter = collection.iterator(); while (iter.hasNext()) { this.myArrayList.add(JSONObject.wrap(iter.next())); } @@ -593,7 +593,7 @@ public JSONArray put(boolean value) { * A Collection value. * @return this. */ - public JSONArray put(Collection value) { + public JSONArray put(Collection value) { this.put(new JSONArray(value)); return this; } @@ -646,7 +646,7 @@ public JSONArray put(long value) { * A Map value. * @return this. */ - public JSONArray put(Map value) { + public JSONArray put(Map value) { this.put(new JSONObject(value)); return this; } @@ -695,7 +695,7 @@ public JSONArray put(int index, boolean value) throws JSONException { * @throws JSONException * If the index is negative or if the value is not finite. */ - public JSONArray put(int index, Collection value) throws JSONException { + public JSONArray put(int index, Collection value) throws JSONException { this.put(index, new JSONArray(value)); return this; } diff --git a/JSONObject.java b/JSONObject.java index d66623110..a1dbec4f5 100755 --- a/JSONObject.java +++ b/JSONObject.java @@ -242,15 +242,13 @@ public JSONObject(JSONTokener x) throws JSONException { * the JSONObject. * @throws JSONException */ - public JSONObject(Map map) { + public JSONObject(Map map) { this.map = new HashMap(); if (map != null) { - Iterator> i = map.entrySet().iterator(); - while (i.hasNext()) { - Entry entry = i.next(); + for (Entry entry : map.entrySet()) { Object value = entry.getValue(); if (value != null) { - this.map.put(entry.getKey(), wrap(value)); + this.map.put(entry.getKey().toString(), wrap(value)); } } } @@ -1053,7 +1051,7 @@ public JSONObject put(String key, boolean value) throws JSONException { * @return this. * @throws JSONException */ - public JSONObject put(String key, Collection value) throws JSONException { + public JSONObject put(String key, Collection value) throws JSONException { this.put(key, new JSONArray(value)); return this; } @@ -1117,7 +1115,7 @@ public JSONObject put(String key, long value) throws JSONException { * @return this. * @throws JSONException */ - public JSONObject put(String key, Map value) throws JSONException { + public JSONObject put(String key, Map value) throws JSONException { this.put(key, new JSONObject(value)); return this; } @@ -1512,10 +1510,10 @@ public static String valueToString(Object value) throws JSONException { return value.toString(); } if (value instanceof Map) { - return new JSONObject((Map)value).toString(); + return new JSONObject((Map)value).toString(); } if (value instanceof Collection) { - return new JSONArray((Collection) value).toString(); + return new JSONArray((Collection) value).toString(); } if (value.getClass().isArray()) { return new JSONArray(value).toString(); @@ -1557,7 +1555,7 @@ public static Object wrap(Object object) { return new JSONArray(object); } if (object instanceof Map) { - return new JSONObject((Map) object); + return new JSONObject((Map) object); } Package objectPackage = object.getClass().getPackage(); String objectPackageName = objectPackage != null ? objectPackage @@ -1595,9 +1593,9 @@ static final Writer writeValue(Writer writer, Object value, } else if (value instanceof JSONArray) { ((JSONArray) value).write(writer, indentFactor, indent); } else if (value instanceof Map) { - new JSONObject((Map) value).write(writer, indentFactor, indent); + new JSONObject((Map) value).write(writer, indentFactor, indent); } else if (value instanceof Collection) { - new JSONArray((Collection) value).write(writer, indentFactor, + new JSONArray((Collection) value).write(writer, indentFactor, indent); } else if (value.getClass().isArray()) { new JSONArray(value).write(writer, indentFactor, indent); From b15485cd0b817c23a58cd86a32c822fa9b38946b Mon Sep 17 00:00:00 2001 From: Michael Dobrovnik Date: Mon, 5 Jan 2015 19:10:12 +0100 Subject: [PATCH 2/3] Use wildcards (?) in generic signatures - additional wildcards --- JSONArray.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/JSONArray.java b/JSONArray.java index 91a868f10..48f9e8b6c 100644 --- a/JSONArray.java +++ b/JSONArray.java @@ -767,7 +767,7 @@ public JSONArray put(int index, long value) throws JSONException { * If the index is negative or if the the value is an invalid * number. */ - public JSONArray put(int index, Map value) throws JSONException { + public JSONArray put(int index, Map value) throws JSONException { this.put(index, new JSONObject(value)); return this; } From f61aa33ddd6228de14bd44aaba0657d51ea8e281 Mon Sep 17 00:00:00 2001 From: Michael Dobrovnik Date: Wed, 7 Jan 2015 17:50:13 +0100 Subject: [PATCH 3/3] Use wildcards in generic signatures. Clarifying comments. Additional Collection occurence. --- JSONArray.java | 8 ++++++-- JSONObject.java | 10 +++++++--- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/JSONArray.java b/JSONArray.java index 48f9e8b6c..ef4706b13 100644 --- a/JSONArray.java +++ b/JSONArray.java @@ -643,7 +643,9 @@ public JSONArray put(long value) { * is produced from a Map. * * @param value - * A Map value. + * A Map value. The keys of the parameter are usually strings; + * if not, then the keys toString() implementation will be + * used to derive a proper JSON key. * @return this. */ public JSONArray put(Map value) { @@ -761,7 +763,9 @@ public JSONArray put(int index, long value) throws JSONException { * @param index * The subscript. * @param value - * The Map value. + * A Map value. The keys of the parameter are usually strings; + * if not, then the keys toString() implementation will be + * used to derive a proper JSON key. * @return this. * @throws JSONException * If the index is negative or if the the value is an invalid diff --git a/JSONObject.java b/JSONObject.java index a1dbec4f5..f93c9f0af 100755 --- a/JSONObject.java +++ b/JSONObject.java @@ -239,7 +239,9 @@ public JSONObject(JSONTokener x) throws JSONException { * * @param map * A map object that can be used to initialize the contents of - * the JSONObject. + * the JSONObject. The keys of the map are usually strings; + * if not, then the keys toString() implementation will be + * used to derive a proper JSON key. * @throws JSONException */ public JSONObject(Map map) { @@ -1111,7 +1113,9 @@ public JSONObject put(String key, long value) throws JSONException { * @param key * A key string. * @param value - * A Map value. + * A Map value. The keys of the parameter are usually strings; + * if not, then the keys toString() implementation will be + * used to derive a proper JSON key. * @return this. * @throws JSONException */ @@ -1549,7 +1553,7 @@ public static Object wrap(Object object) { } if (object instanceof Collection) { - return new JSONArray((Collection) object); + return new JSONArray((Collection) object); } if (object.getClass().isArray()) { return new JSONArray(object);