Skip to content

Commit 25b5aa7

Browse files
author
Lukas Treyer
committed
changed Map<String, ?> method parameters to Map<?,?>
changed Iterator to foreach loop in JSONArray ctor JSONArray(Collection<?> collection) and JSONObject ctor JSONObject(Map<?,?> map)
1 parent 409eb9f commit 25b5aa7

File tree

2 files changed

+13
-18
lines changed

2 files changed

+13
-18
lines changed

JSONArray.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,9 @@ public JSONArray(String source) throws JSONException {
154154
public JSONArray(Collection<?> collection) {
155155
this.myArrayList = new ArrayList<Object>();
156156
if (collection != null) {
157-
Iterator<?> iter = collection.iterator();
158-
while (iter.hasNext()) {
159-
this.myArrayList.add(JSONObject.wrap(iter.next()));
160-
}
157+
for (Object o: collection){
158+
this.myArrayList.add(JSONObject.wrap(o));
159+
}
161160
}
162161
}
163162

@@ -799,7 +798,7 @@ public JSONArray put(long value) {
799798
* A Map value.
800799
* @return this.
801800
*/
802-
public JSONArray put(Map<String, ?> value) {
801+
public JSONArray put(Map<?, ?> value) {
803802
this.put(new JSONObject(value));
804803
return this;
805804
}
@@ -920,7 +919,7 @@ public JSONArray put(int index, long value) throws JSONException {
920919
* If the index is negative or if the the value is an invalid
921920
* number.
922921
*/
923-
public JSONArray put(int index, Map<String, ?> value) throws JSONException {
922+
public JSONArray put(int index, Map<?, ?> value) throws JSONException {
924923
this.put(index, new JSONObject(value));
925924
return this;
926925
}

JSONObject.java

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -243,17 +243,13 @@ public JSONObject(JSONTokener x) throws JSONException {
243243
* the JSONObject.
244244
* @throws JSONException
245245
*/
246-
public JSONObject(Map<String, ?> map) {
246+
public JSONObject(Map<?, ?> map) {
247247
this.map = new HashMap<String, Object>();
248248
if (map != null) {
249-
Set<?> eSet = map.entrySet();
250-
@SuppressWarnings("unchecked")
251-
Iterator<Entry<String, ?>> i = (Iterator<Entry<String, ?>>) eSet.iterator();
252-
while (i.hasNext()) {
253-
Entry<String, ?> entry = i.next();
254-
Object value = entry.getValue();
249+
for (final Entry<?, ?> e : map.entrySet()) {
250+
final Object value = e.getValue();
255251
if (value != null) {
256-
this.map.put(entry.getKey(), wrap(value));
252+
this.map.put(String.valueOf(e.getKey()), wrap(value));
257253
}
258254
}
259255
}
@@ -1270,7 +1266,7 @@ public JSONObject put(String key, long value) throws JSONException {
12701266
* @return this.
12711267
* @throws JSONException
12721268
*/
1273-
public JSONObject put(String key, Map<String, ?> value) throws JSONException {
1269+
public JSONObject put(String key, Map<?, ?> value) throws JSONException {
12741270
this.put(key, new JSONObject(value));
12751271
return this;
12761272
}
@@ -1666,7 +1662,7 @@ public static String valueToString(Object value) throws JSONException {
16661662
}
16671663
if (value instanceof Map) {
16681664
@SuppressWarnings("unchecked")
1669-
Map<String, ?> map = (Map<String, ?>) value;
1665+
Map<?, ?> map = (Map<?, ?>) value;
16701666
return new JSONObject(map).toString();
16711667
}
16721668
if (value instanceof Collection) {
@@ -1718,7 +1714,7 @@ public static Object wrap(Object object) {
17181714
}
17191715
if (object instanceof Map) {
17201716
@SuppressWarnings("unchecked")
1721-
Map<String, ?> map = (Map<String, ?>) object;
1717+
Map<?, ?> map = (Map<?, ?>) object;
17221718
return new JSONObject(map);
17231719
}
17241720
Package objectPackage = object.getClass().getPackage();
@@ -1758,7 +1754,7 @@ static final Writer writeValue(Writer writer, Object value,
17581754
((JSONArray) value).write(writer, indentFactor, indent);
17591755
} else if (value instanceof Map) {
17601756
@SuppressWarnings("unchecked")
1761-
Map<String, ?> map = (Map<String, ?>) value;
1757+
Map<?, ?> map = (Map<?, ?>) value;
17621758
new JSONObject(map).write(writer, indentFactor, indent);
17631759
} else if (value instanceof Collection) {
17641760
@SuppressWarnings("unchecked")

0 commit comments

Comments
 (0)