Skip to content

Commit 409eb9f

Browse files
author
Lukas Treyer
committed
changed all method signatures containing collections and maps to accept
wildcard generic types, e.g. Collection<?> instead of Collection<Object>. This was proposed by other pull requests (stleary#111, stleary#112) already. Consider this commit as merge with stleary#111 and stleary#112. JSONArray: - put(Collection<?> value) {...} - put(Map<String, ?> value) {...} - put(int index, Collection<?> value) throws JSONException {...} - put(int index, Map<String, ?> value) throws JSONException {...} JSONObject: - put(String key, Collection<?> value) throws JSONException {...} - put(String key, Map<String, ?> 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 }
1 parent 0afd266 commit 409eb9f

File tree

2 files changed

+13
-14
lines changed

2 files changed

+13
-14
lines changed

JSONArray.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,7 @@ public JSONArray put(boolean value) {
746746
* A Collection value.
747747
* @return this.
748748
*/
749-
public JSONArray put(Collection<Object> value) {
749+
public JSONArray put(Collection<?> value) {
750750
this.put(new JSONArray(value));
751751
return this;
752752
}
@@ -799,7 +799,7 @@ public JSONArray put(long value) {
799799
* A Map value.
800800
* @return this.
801801
*/
802-
public JSONArray put(Map<String, Object> value) {
802+
public JSONArray put(Map<String, ?> value) {
803803
this.put(new JSONObject(value));
804804
return this;
805805
}
@@ -848,7 +848,7 @@ public JSONArray put(int index, boolean value) throws JSONException {
848848
* @throws JSONException
849849
* If the index is negative or if the value is not finite.
850850
*/
851-
public JSONArray put(int index, Collection<Object> value) throws JSONException {
851+
public JSONArray put(int index, Collection<?> value) throws JSONException {
852852
this.put(index, new JSONArray(value));
853853
return this;
854854
}
@@ -920,7 +920,7 @@ public JSONArray put(int index, long value) throws JSONException {
920920
* If the index is negative or if the the value is an invalid
921921
* number.
922922
*/
923-
public JSONArray put(int index, Map<String, Object> value) throws JSONException {
923+
public JSONArray put(int index, Map<String, ?> value) throws JSONException {
924924
this.put(index, new JSONObject(value));
925925
return this;
926926
}

JSONObject.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,7 +1206,7 @@ public JSONObject put(String key, boolean value) throws JSONException {
12061206
* @return this.
12071207
* @throws JSONException
12081208
*/
1209-
public JSONObject put(String key, Collection<Object> value) throws JSONException {
1209+
public JSONObject put(String key, Collection<?> value) throws JSONException {
12101210
this.put(key, new JSONArray(value));
12111211
return this;
12121212
}
@@ -1270,7 +1270,7 @@ public JSONObject put(String key, long value) throws JSONException {
12701270
* @return this.
12711271
* @throws JSONException
12721272
*/
1273-
public JSONObject put(String key, Map<String, Object> value) throws JSONException {
1273+
public JSONObject put(String key, Map<String, ?> value) throws JSONException {
12741274
this.put(key, new JSONObject(value));
12751275
return this;
12761276
}
@@ -1666,12 +1666,12 @@ public static String valueToString(Object value) throws JSONException {
16661666
}
16671667
if (value instanceof Map) {
16681668
@SuppressWarnings("unchecked")
1669-
Map<String, Object> map = (Map<String, Object>) value;
1669+
Map<String, ?> map = (Map<String, ?>) value;
16701670
return new JSONObject(map).toString();
16711671
}
16721672
if (value instanceof Collection) {
16731673
@SuppressWarnings("unchecked")
1674-
Collection<Object> coll = (Collection<Object>) value;
1674+
Collection<?> coll = (Collection<?>) value;
16751675
return new JSONArray(coll).toString();
16761676
}
16771677
if (value.getClass().isArray()) {
@@ -1710,15 +1710,15 @@ public static Object wrap(Object object) {
17101710

17111711
if (object instanceof Collection) {
17121712
@SuppressWarnings("unchecked")
1713-
Collection<Object> coll = (Collection<Object>) object;
1713+
Collection<?> coll = (Collection<?>) object;
17141714
return new JSONArray(coll);
17151715
}
17161716
if (object.getClass().isArray()) {
17171717
return new JSONArray(object);
17181718
}
17191719
if (object instanceof Map) {
17201720
@SuppressWarnings("unchecked")
1721-
Map<String, Object> map = (Map<String, Object>) object;
1721+
Map<String, ?> map = (Map<String, ?>) object;
17221722
return new JSONObject(map);
17231723
}
17241724
Package objectPackage = object.getClass().getPackage();
@@ -1758,13 +1758,12 @@ static final Writer writeValue(Writer writer, Object value,
17581758
((JSONArray) value).write(writer, indentFactor, indent);
17591759
} else if (value instanceof Map) {
17601760
@SuppressWarnings("unchecked")
1761-
Map<String, Object> map = (Map<String, Object>) value;
1761+
Map<String, ?> map = (Map<String, ?>) value;
17621762
new JSONObject(map).write(writer, indentFactor, indent);
17631763
} else if (value instanceof Collection) {
17641764
@SuppressWarnings("unchecked")
1765-
Collection<Object> coll = (Collection<Object>) value;
1766-
new JSONArray(coll).write(writer, indentFactor,
1767-
indent);
1765+
Collection<?> coll = (Collection<?>) value;
1766+
new JSONArray(coll).write(writer, indentFactor, indent);
17681767
} else if (value.getClass().isArray()) {
17691768
new JSONArray(value).write(writer, indentFactor, indent);
17701769
} else if (value instanceof Number) {

0 commit comments

Comments
 (0)