From 0afd26623c0fa44c3c36d3b70323fa97c248106a Mon Sep 17 00:00:00 2001 From: Lukas Treyer Date: Sun, 4 Oct 2015 23:17:30 +0200 Subject: [PATCH 01/10] JSONObject and JSONArray initialization: JSONObject(Map map) allows to initialize the JSONObject with a Map JSONArray(Collection collection) allows to initialize a JSONArray with a Collection --- JSONArray.java | 4 ++-- JSONObject.java | 8 +++++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/JSONArray.java b/JSONArray.java index b2717d9..3605411 100644 --- a/JSONArray.java +++ b/JSONArray.java @@ -151,10 +151,10 @@ public class JSONArray implements Iterable { * @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())); } diff --git a/JSONObject.java b/JSONObject.java index ed1159e..5d17cc7 100755 --- a/JSONObject.java +++ b/JSONObject.java @@ -243,12 +243,14 @@ public class JSONObject { * the JSONObject. * @throws JSONException */ - public JSONObject(Map map) { + public JSONObject(Map map) { this.map = new HashMap(); if (map != null) { - Iterator> i = map.entrySet().iterator(); + Set eSet = map.entrySet(); + @SuppressWarnings("unchecked") + Iterator> i = (Iterator>) eSet.iterator(); while (i.hasNext()) { - Entry entry = i.next(); + Entry entry = i.next(); Object value = entry.getValue(); if (value != null) { this.map.put(entry.getKey(), wrap(value)); From 409eb9f2922dcdc35013ebbbbfd3e3b05ea79259 Mon Sep 17 00:00:00 2001 From: Lukas Treyer Date: Sun, 11 Oct 2015 11:20:08 +0200 Subject: [PATCH 02/10] 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) { From 25b5aa7ef265c7a6335f9b6de32056d3a7d63447 Mon Sep 17 00:00:00 2001 From: Lukas Treyer Date: Sun, 11 Oct 2015 12:21:12 +0200 Subject: [PATCH 03/10] changed Map method parameters to Map changed Iterator to foreach loop in JSONArray ctor JSONArray(Collection collection) and JSONObject ctor JSONObject(Map map) --- JSONArray.java | 11 +++++------ JSONObject.java | 20 ++++++++------------ 2 files changed, 13 insertions(+), 18 deletions(-) diff --git a/JSONArray.java b/JSONArray.java index 077eded..d853b1a 100644 --- a/JSONArray.java +++ b/JSONArray.java @@ -154,10 +154,9 @@ public class JSONArray implements Iterable { public JSONArray(Collection collection) { this.myArrayList = new ArrayList(); if (collection != null) { - Iterator iter = collection.iterator(); - while (iter.hasNext()) { - this.myArrayList.add(JSONObject.wrap(iter.next())); - } + for (Object o: collection){ + this.myArrayList.add(JSONObject.wrap(o)); + } } } @@ -799,7 +798,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; } @@ -920,7 +919,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 73c51f7..6d0d37b 100755 --- a/JSONObject.java +++ b/JSONObject.java @@ -243,17 +243,13 @@ public class JSONObject { * the JSONObject. * @throws JSONException */ - public JSONObject(Map map) { + public JSONObject(Map map) { this.map = new HashMap(); if (map != null) { - Set eSet = map.entrySet(); - @SuppressWarnings("unchecked") - Iterator> i = (Iterator>) eSet.iterator(); - while (i.hasNext()) { - Entry entry = i.next(); - Object value = entry.getValue(); + for (final Entry e : map.entrySet()) { + final Object value = e.getValue(); if (value != null) { - this.map.put(entry.getKey(), wrap(value)); + this.map.put(String.valueOf(e.getKey()), wrap(value)); } } } @@ -1270,7 +1266,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,7 +1662,7 @@ 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) { @@ -1718,7 +1714,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,7 +1754,7 @@ 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") From 5ddc515679feb438e67c6fff8540f2e96a6843dc Mon Sep 17 00:00:00 2001 From: Lukas Treyer Date: Mon, 12 Oct 2015 23:52:14 +0200 Subject: [PATCH 04/10] removed 6 unnecessary @SuppressWarnings("unchecked") annotations. --- JSONObject.java | 6 ------ 1 file changed, 6 deletions(-) diff --git a/JSONObject.java b/JSONObject.java index 6d0d37b..0e8ff09 100755 --- a/JSONObject.java +++ b/JSONObject.java @@ -1661,12 +1661,10 @@ public class JSONObject { return value.toString(); } if (value instanceof Map) { - @SuppressWarnings("unchecked") Map map = (Map) value; return new JSONObject(map).toString(); } if (value instanceof Collection) { - @SuppressWarnings("unchecked") Collection coll = (Collection) value; return new JSONArray(coll).toString(); } @@ -1705,7 +1703,6 @@ public class JSONObject { } if (object instanceof Collection) { - @SuppressWarnings("unchecked") Collection coll = (Collection) object; return new JSONArray(coll); } @@ -1713,7 +1710,6 @@ public class JSONObject { return new JSONArray(object); } if (object instanceof Map) { - @SuppressWarnings("unchecked") Map map = (Map) object; return new JSONObject(map); } @@ -1753,11 +1749,9 @@ public class JSONObject { } else if (value instanceof JSONArray) { ((JSONArray) value).write(writer, indentFactor, indent); } else if (value instanceof Map) { - @SuppressWarnings("unchecked") 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); } else if (value.getClass().isArray()) { From a07d391eaeaa974c0f47754b3e5eec5bbe921cf5 Mon Sep 17 00:00:00 2001 From: stleary Date: Sun, 25 Oct 2015 11:05:16 -0500 Subject: [PATCH 05/10] Include latest Maven release information --- README | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README b/README index dd5d1e6..bf7050e 100755 --- a/README +++ b/README @@ -62,3 +62,10 @@ JSONML.java: JSONML provides support for converting between JSONML and XML. XMLTokener.java: XMLTokener extends JSONTokener for parsing XML text. Unit tests are maintained in a separate project. Contributing developers can test JSON-java pull requests with the code in this project: https://github.com/stleary/JSON-Java-unit-test + +Release history: + +20150729 Checkpoint for Maven central repository release. Contains the latest code as of 29 July, 2015. + +JSON-java releases can be found by searching the Maven repository for groupId "org.json" and artifactId "json". For example: +https://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22org.json%22%20AND%20a%3A%22json%22 From 33ae025e7864d8392c76f4332821d0a2b1f9f4dd Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Thu, 29 Oct 2015 18:24:46 -0500 Subject: [PATCH 06/10] Update JSONObject.java Update version for https://github.com/douglascrockford/JSON-java/pull/153 --- JSONObject.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/JSONObject.java b/JSONObject.java index 0e8ff09..a23daab 100755 --- a/JSONObject.java +++ b/JSONObject.java @@ -92,7 +92,7 @@ import java.util.Set; * * * @author JSON.org - * @version 2015-07-22 + * @version 2015-10-29 */ public class JSONObject { /** From a109f581c885b24d19d8e8e659a1d68af61605ce Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Thu, 29 Oct 2015 18:25:27 -0500 Subject: [PATCH 07/10] Update JSONArray.java Update version for https://github.com/douglascrockford/JSON-java/pull/153 --- JSONArray.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/JSONArray.java b/JSONArray.java index d853b1a..5ccb65b 100644 --- a/JSONArray.java +++ b/JSONArray.java @@ -76,7 +76,7 @@ import java.util.Map; * * * @author JSON.org - * @version 2015-07-22 + * @version 2015-10-29 */ public class JSONArray implements Iterable { From 5f2e77f9ddf1c6a9f67ea239be1802719029ec15 Mon Sep 17 00:00:00 2001 From: stleary Date: Sat, 21 Nov 2015 11:50:31 -0600 Subject: [PATCH 08/10] Update readme with proposed release information --- README | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README b/README index bf7050e..02d63ad 100755 --- a/README +++ b/README @@ -65,6 +65,9 @@ Unit tests are maintained in a separate project. Contributing developers can tes Release history: +20151123 JSONObject and JSONArray initialization with generics. Contains the +latest code as of 23 Nov, 2015. + 20150729 Checkpoint for Maven central repository release. Contains the latest code as of 29 July, 2015. JSON-java releases can be found by searching the Maven repository for groupId "org.json" and artifactId "json". For example: From cadba9400c56848527c298f3c9f5cfad9c69ff4c Mon Sep 17 00:00:00 2001 From: Andrew Fletcher Date: Wed, 2 Dec 2015 10:49:54 -0800 Subject: [PATCH 09/10] Update JavaDoc for JSONObject Constructors Two JSONObject constructors incorrectly specify a @throws JSONException tag in the JavaDoc for those constructors. Remove the relevant JavaDoc. --- JSONObject.java | 5 ----- 1 file changed, 5 deletions(-) diff --git a/JSONObject.java b/JSONObject.java index a23daab..2ac21c9 100755 --- a/JSONObject.java +++ b/JSONObject.java @@ -165,10 +165,6 @@ public class JSONObject { * A JSONObject. * @param names * An array of strings. - * @throws JSONException - * @exception JSONException - * If a value is a non-finite number or if a name is - * duplicated. */ public JSONObject(JSONObject jo, String[] names) { this(); @@ -241,7 +237,6 @@ public class JSONObject { * @param map * A map object that can be used to initialize the contents of * the JSONObject. - * @throws JSONException */ public JSONObject(Map map) { this.map = new HashMap(); From 39e3ccc6715833488755c9938d1a1be8af1e2c8c Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Sat, 5 Dec 2015 20:14:15 -0600 Subject: [PATCH 10/10] Update version Update version after merge of https://github.com/douglascrockford/JSON-java/pull/179 --- JSONObject.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/JSONObject.java b/JSONObject.java index 2ac21c9..53cdeec 100755 --- a/JSONObject.java +++ b/JSONObject.java @@ -92,7 +92,7 @@ import java.util.Set; * * * @author JSON.org - * @version 2015-10-29 + * @version 2015-12-05 */ public class JSONObject { /**