From 25b5aa7ef265c7a6335f9b6de32056d3a7d63447 Mon Sep 17 00:00:00 2001 From: Lukas Treyer Date: Sun, 11 Oct 2015 12:21:12 +0200 Subject: [PATCH] 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")