From 409eb9f2922dcdc35013ebbbbfd3e3b05ea79259 Mon Sep 17 00:00:00 2001 From: Lukas Treyer Date: Sun, 11 Oct 2015 11:20:08 +0200 Subject: [PATCH] changed all method signatures containing collections and maps to accept wildcard generic types, e.g. Collection instead of Collection. This was proposed by other pull requests (#111, #112) already. Consider this commit as merge with #111 and #112. JSONArray: - put(Collection value) {...} - put(Map value) {...} - put(int index, Collection value) throws JSONException {...} - put(int index, Map value) throws JSONException {...} JSONObject: - put(String key, Collection value) throws JSONException {...} - put(String key, Map value) throws JSONException {...} Changed all code affected by new JSONObject and JSONArray constructors: JSONObject: - valueToString(Object value) throws JSONException { - value instanceof Map - value instanceof Collection } - wrap(Object object) { - value instanceof Map - value instanceof Collection } - writeValue(Writer writer, Object value, int indentFactor, int indent){ - value instanceof Map - value instanceof Collection } --- JSONArray.java | 8 ++++---- JSONObject.java | 19 +++++++++---------- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/JSONArray.java b/JSONArray.java index 3605411..077eded 100644 --- a/JSONArray.java +++ b/JSONArray.java @@ -746,7 +746,7 @@ public class JSONArray implements Iterable { * A Collection value. * @return this. */ - public JSONArray put(Collection value) { + public JSONArray put(Collection value) { this.put(new JSONArray(value)); return this; } @@ -799,7 +799,7 @@ public class JSONArray implements Iterable { * A Map value. * @return this. */ - public JSONArray put(Map value) { + public JSONArray put(Map value) { this.put(new JSONObject(value)); return this; } @@ -848,7 +848,7 @@ public class JSONArray implements Iterable { * @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; } @@ -920,7 +920,7 @@ public class JSONArray implements Iterable { * 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; } diff --git a/JSONObject.java b/JSONObject.java index 5d17cc7..73c51f7 100755 --- a/JSONObject.java +++ b/JSONObject.java @@ -1206,7 +1206,7 @@ public class JSONObject { * @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; } @@ -1270,7 +1270,7 @@ public class JSONObject { * @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; } @@ -1666,12 +1666,12 @@ public class JSONObject { } if (value instanceof Map) { @SuppressWarnings("unchecked") - Map map = (Map) value; + Map map = (Map) value; return new JSONObject(map).toString(); } if (value instanceof Collection) { @SuppressWarnings("unchecked") - Collection coll = (Collection) value; + Collection coll = (Collection) value; return new JSONArray(coll).toString(); } if (value.getClass().isArray()) { @@ -1710,7 +1710,7 @@ public class JSONObject { if (object instanceof Collection) { @SuppressWarnings("unchecked") - Collection coll = (Collection) object; + Collection coll = (Collection) object; return new JSONArray(coll); } if (object.getClass().isArray()) { @@ -1718,7 +1718,7 @@ public class JSONObject { } if (object instanceof Map) { @SuppressWarnings("unchecked") - Map map = (Map) object; + Map map = (Map) object; return new JSONObject(map); } Package objectPackage = object.getClass().getPackage(); @@ -1758,13 +1758,12 @@ public class JSONObject { ((JSONArray) value).write(writer, indentFactor, indent); } else if (value instanceof Map) { @SuppressWarnings("unchecked") - Map map = (Map) value; + Map map = (Map) value; new JSONObject(map).write(writer, indentFactor, indent); } else if (value instanceof Collection) { @SuppressWarnings("unchecked") - Collection coll = (Collection) value; - new JSONArray(coll).write(writer, indentFactor, - indent); + Collection coll = (Collection) value; + new JSONArray(coll).write(writer, indentFactor, indent); } else if (value.getClass().isArray()) { new JSONArray(value).write(writer, indentFactor, indent); } else if (value instanceof Number) {