Skip to content

Commit 564ad2c

Browse files
committed
Merge pull request stleary#153 from treyerl/master
JSONObject and JSONArray initialization for Map<?,?> and Collection<?>
2 parents dfd1911 + 5ddc515 commit 564ad2c

File tree

2 files changed

+21
-31
lines changed

2 files changed

+21
-31
lines changed

JSONArray.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -151,13 +151,12 @@ public JSONArray(String source) throws JSONException {
151151
* @param collection
152152
* A Collection.
153153
*/
154-
public JSONArray(Collection<Object> collection) {
154+
public JSONArray(Collection<?> collection) {
155155
this.myArrayList = new ArrayList<Object>();
156156
if (collection != null) {
157-
Iterator<Object> 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

@@ -746,7 +745,7 @@ public JSONArray put(boolean value) {
746745
* A Collection value.
747746
* @return this.
748747
*/
749-
public JSONArray put(Collection<Object> value) {
748+
public JSONArray put(Collection<?> value) {
750749
this.put(new JSONArray(value));
751750
return this;
752751
}
@@ -799,7 +798,7 @@ public JSONArray put(long value) {
799798
* A Map value.
800799
* @return this.
801800
*/
802-
public JSONArray put(Map<String, Object> value) {
801+
public JSONArray put(Map<?, ?> value) {
803802
this.put(new JSONObject(value));
804803
return this;
805804
}
@@ -848,7 +847,7 @@ public JSONArray put(int index, boolean value) throws JSONException {
848847
* @throws JSONException
849848
* If the index is negative or if the value is not finite.
850849
*/
851-
public JSONArray put(int index, Collection<Object> value) throws JSONException {
850+
public JSONArray put(int index, Collection<?> value) throws JSONException {
852851
this.put(index, new JSONArray(value));
853852
return this;
854853
}
@@ -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, Object> 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: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -243,15 +243,13 @@ public JSONObject(JSONTokener x) throws JSONException {
243243
* the JSONObject.
244244
* @throws JSONException
245245
*/
246-
public JSONObject(Map<String, Object> map) {
246+
public JSONObject(Map<?, ?> map) {
247247
this.map = new HashMap<String, Object>();
248248
if (map != null) {
249-
Iterator<Entry<String, Object>> i = map.entrySet().iterator();
250-
while (i.hasNext()) {
251-
Entry<String, Object> entry = i.next();
252-
Object value = entry.getValue();
249+
for (final Entry<?, ?> e : map.entrySet()) {
250+
final Object value = e.getValue();
253251
if (value != null) {
254-
this.map.put(entry.getKey(), wrap(value));
252+
this.map.put(String.valueOf(e.getKey()), wrap(value));
255253
}
256254
}
257255
}
@@ -1204,7 +1202,7 @@ public JSONObject put(String key, boolean value) throws JSONException {
12041202
* @return this.
12051203
* @throws JSONException
12061204
*/
1207-
public JSONObject put(String key, Collection<Object> value) throws JSONException {
1205+
public JSONObject put(String key, Collection<?> value) throws JSONException {
12081206
this.put(key, new JSONArray(value));
12091207
return this;
12101208
}
@@ -1268,7 +1266,7 @@ public JSONObject put(String key, long value) throws JSONException {
12681266
* @return this.
12691267
* @throws JSONException
12701268
*/
1271-
public JSONObject put(String key, Map<String, Object> value) throws JSONException {
1269+
public JSONObject put(String key, Map<?, ?> value) throws JSONException {
12721270
this.put(key, new JSONObject(value));
12731271
return this;
12741272
}
@@ -1663,13 +1661,11 @@ public static String valueToString(Object value) throws JSONException {
16631661
return value.toString();
16641662
}
16651663
if (value instanceof Map) {
1666-
@SuppressWarnings("unchecked")
1667-
Map<String, Object> map = (Map<String, Object>) value;
1664+
Map<?, ?> map = (Map<?, ?>) value;
16681665
return new JSONObject(map).toString();
16691666
}
16701667
if (value instanceof Collection) {
1671-
@SuppressWarnings("unchecked")
1672-
Collection<Object> coll = (Collection<Object>) value;
1668+
Collection<?> coll = (Collection<?>) value;
16731669
return new JSONArray(coll).toString();
16741670
}
16751671
if (value.getClass().isArray()) {
@@ -1707,16 +1703,14 @@ public static Object wrap(Object object) {
17071703
}
17081704

17091705
if (object instanceof Collection) {
1710-
@SuppressWarnings("unchecked")
1711-
Collection<Object> coll = (Collection<Object>) object;
1706+
Collection<?> coll = (Collection<?>) object;
17121707
return new JSONArray(coll);
17131708
}
17141709
if (object.getClass().isArray()) {
17151710
return new JSONArray(object);
17161711
}
17171712
if (object instanceof Map) {
1718-
@SuppressWarnings("unchecked")
1719-
Map<String, Object> map = (Map<String, Object>) object;
1713+
Map<?, ?> map = (Map<?, ?>) object;
17201714
return new JSONObject(map);
17211715
}
17221716
Package objectPackage = object.getClass().getPackage();
@@ -1755,14 +1749,11 @@ static final Writer writeValue(Writer writer, Object value,
17551749
} else if (value instanceof JSONArray) {
17561750
((JSONArray) value).write(writer, indentFactor, indent);
17571751
} else if (value instanceof Map) {
1758-
@SuppressWarnings("unchecked")
1759-
Map<String, Object> map = (Map<String, Object>) value;
1752+
Map<?, ?> map = (Map<?, ?>) value;
17601753
new JSONObject(map).write(writer, indentFactor, indent);
17611754
} else if (value instanceof Collection) {
1762-
@SuppressWarnings("unchecked")
1763-
Collection<Object> coll = (Collection<Object>) value;
1764-
new JSONArray(coll).write(writer, indentFactor,
1765-
indent);
1755+
Collection<?> coll = (Collection<?>) value;
1756+
new JSONArray(coll).write(writer, indentFactor, indent);
17661757
} else if (value.getClass().isArray()) {
17671758
new JSONArray(value).write(writer, indentFactor, indent);
17681759
} else if (value instanceof Number) {

0 commit comments

Comments
 (0)