Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unbounded wildcards in Collections and Maps #112

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions JSONArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,10 @@ public JSONArray(String source) throws JSONException {
* @param collection
* A Collection.
*/
public JSONArray(Collection<Object> collection) {
public JSONArray(Collection<?> collection) {
this.myArrayList = new ArrayList<Object>();
if (collection != null) {
Iterator<Object> iter = collection.iterator();
Iterator<?> iter = collection.iterator();
while (iter.hasNext()) {
this.myArrayList.add(JSONObject.wrap(iter.next()));
}
Expand Down Expand Up @@ -593,7 +593,7 @@ public JSONArray put(boolean value) {
* A Collection value.
* @return this.
*/
public JSONArray put(Collection<Object> value) {
public JSONArray put(Collection<?> value) {
this.put(new JSONArray(value));
return this;
}
Expand Down Expand Up @@ -643,10 +643,12 @@ public JSONArray put(long value) {
* is produced from a Map.
*
* @param value
* A Map value.
* A Map value. The keys of the parameter are usually strings;
* if not, then the keys toString() implementation will be
* used to derive a proper JSON key.
* @return this.
*/
public JSONArray put(Map<String, Object> value) {
public JSONArray put(Map<?, ?> value) {
this.put(new JSONObject(value));
return this;
}
Expand Down Expand Up @@ -695,7 +697,7 @@ public JSONArray put(int index, boolean value) throws JSONException {
* @throws JSONException
* If the index is negative or if the value is not finite.
*/
public JSONArray put(int index, Collection<Object> value) throws JSONException {
public JSONArray put(int index, Collection<?> value) throws JSONException {
this.put(index, new JSONArray(value));
return this;
}
Expand Down Expand Up @@ -761,13 +763,15 @@ public JSONArray put(int index, long value) throws JSONException {
* @param index
* The subscript.
* @param value
* The Map value.
* A Map value. The keys of the parameter are usually strings;
* if not, then the keys toString() implementation will be
* used to derive a proper JSON key.
* @return this.
* @throws JSONException
* If the index is negative or if the the value is an invalid
* number.
*/
public JSONArray put(int index, Map<String, Object> value) throws JSONException {
public JSONArray put(int index, Map<?, ?> value) throws JSONException {
this.put(index, new JSONObject(value));
return this;
}
Expand Down
32 changes: 17 additions & 15 deletions JSONObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -239,18 +239,18 @@ public JSONObject(JSONTokener x) throws JSONException {
*
* @param map
* A map object that can be used to initialize the contents of
* the JSONObject.
* the JSONObject. The keys of the map are usually strings;
* if not, then the keys toString() implementation will be
* used to derive a proper JSON key.
* @throws JSONException
*/
public JSONObject(Map<String, Object> map) {
public JSONObject(Map<?,?> map) {
this.map = new HashMap<String, Object>();
if (map != null) {
Iterator<Entry<String, Object>> i = map.entrySet().iterator();
while (i.hasNext()) {
Entry<String, Object> entry = i.next();
for (Entry<?,?> entry : map.entrySet()) {
Object value = entry.getValue();
if (value != null) {
this.map.put(entry.getKey(), wrap(value));
this.map.put(entry.getKey().toString(), wrap(value));
}
}
}
Expand Down Expand Up @@ -1053,7 +1053,7 @@ public JSONObject put(String key, boolean value) throws JSONException {
* @return this.
* @throws JSONException
*/
public JSONObject put(String key, Collection<Object> value) throws JSONException {
public JSONObject put(String key, Collection<?> value) throws JSONException {
this.put(key, new JSONArray(value));
return this;
}
Expand Down Expand Up @@ -1113,11 +1113,13 @@ public JSONObject put(String key, long value) throws JSONException {
* @param key
* A key string.
* @param value
* A Map value.
* A Map value. The keys of the parameter are usually strings;
* if not, then the keys toString() implementation will be
* used to derive a proper JSON key.
* @return this.
* @throws JSONException
*/
public JSONObject put(String key, Map<String, Object> value) throws JSONException {
public JSONObject put(String key, Map<?,?> value) throws JSONException {
this.put(key, new JSONObject(value));
return this;
}
Expand Down Expand Up @@ -1512,10 +1514,10 @@ public static String valueToString(Object value) throws JSONException {
return value.toString();
}
if (value instanceof Map) {
return new JSONObject((Map<String, Object>)value).toString();
return new JSONObject((Map<?,?>)value).toString();
}
if (value instanceof Collection) {
return new JSONArray((Collection<Object>) value).toString();
return new JSONArray((Collection<?>) value).toString();
}
if (value.getClass().isArray()) {
return new JSONArray(value).toString();
Expand Down Expand Up @@ -1551,13 +1553,13 @@ public static Object wrap(Object object) {
}

if (object instanceof Collection) {
return new JSONArray((Collection<Object>) object);
return new JSONArray((Collection<?>) object);
}
if (object.getClass().isArray()) {
return new JSONArray(object);
}
if (object instanceof Map) {
return new JSONObject((Map<String, Object>) object);
return new JSONObject((Map<?,?>) object);
}
Package objectPackage = object.getClass().getPackage();
String objectPackageName = objectPackage != null ? objectPackage
Expand Down Expand Up @@ -1595,9 +1597,9 @@ static final Writer writeValue(Writer writer, Object value,
} else if (value instanceof JSONArray) {
((JSONArray) value).write(writer, indentFactor, indent);
} else if (value instanceof Map) {
new JSONObject((Map<String, Object>) value).write(writer, indentFactor, indent);
new JSONObject((Map<?,?>) value).write(writer, indentFactor, indent);
} else if (value instanceof Collection) {
new JSONArray((Collection<Object>) value).write(writer, indentFactor,
new JSONArray((Collection<?>) value).write(writer, indentFactor,
indent);
} else if (value.getClass().isArray()) {
new JSONArray(value).write(writer, indentFactor, indent);
Expand Down