diff --git a/JSONArray.java b/JSONArray.java index b2717d9..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 { @@ -151,13 +151,12 @@ 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(); - while (iter.hasNext()) { - this.myArrayList.add(JSONObject.wrap(iter.next())); - } + for (Object o: collection){ + this.myArrayList.add(JSONObject.wrap(o)); + } } } @@ -746,7 +745,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 +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; } @@ -848,7 +847,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 +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 ed1159e..53cdeec 100755 --- a/JSONObject.java +++ b/JSONObject.java @@ -92,7 +92,7 @@ import java.util.Set; * * * @author JSON.org - * @version 2015-07-22 + * @version 2015-12-05 */ public class JSONObject { /** @@ -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,17 +237,14 @@ 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) { + public JSONObject(Map map) { this.map = new HashMap(); if (map != null) { - Iterator> i = map.entrySet().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)); } } } @@ -1204,7 +1197,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; } @@ -1268,7 +1261,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; } @@ -1663,13 +1656,11 @@ public class JSONObject { return value.toString(); } 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()) { @@ -1707,16 +1698,14 @@ 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()) { return new JSONArray(object); } if (object instanceof Map) { - @SuppressWarnings("unchecked") - Map map = (Map) object; + Map map = (Map) object; return new JSONObject(map); } Package objectPackage = object.getClass().getPackage(); @@ -1755,14 +1744,11 @@ 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; + 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) { diff --git a/README b/README index dd5d1e6..02d63ad 100755 --- a/README +++ b/README @@ -62,3 +62,13 @@ 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: + +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: +https://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22org.json%22%20AND%20a%3A%22json%22