1
0
Fork 0
mirror of https://github.com/ethauvin/JSON-java.git synced 2025-06-17 07:50:52 -07:00

Merge pull request #153 from treyerl/master

JSONObject and JSONArray initialization for Map<?,?> and Collection<?>
This commit is contained in:
Sean Leary 2015-10-29 18:07:43 -05:00
commit 564ad2c2fb
2 changed files with 21 additions and 31 deletions

View file

@ -151,13 +151,12 @@ public class JSONArray implements Iterable<Object> {
* @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();
while (iter.hasNext()) {
this.myArrayList.add(JSONObject.wrap(iter.next()));
}
for (Object o: collection){
this.myArrayList.add(JSONObject.wrap(o));
}
}
}
@ -746,7 +745,7 @@ public class JSONArray implements Iterable<Object> {
* A Collection value.
* @return this.
*/
public JSONArray put(Collection<Object> value) {
public JSONArray put(Collection<?> value) {
this.put(new JSONArray(value));
return this;
}
@ -799,7 +798,7 @@ public class JSONArray implements Iterable<Object> {
* A Map value.
* @return this.
*/
public JSONArray put(Map<String, Object> value) {
public JSONArray put(Map<?, ?> value) {
this.put(new JSONObject(value));
return this;
}
@ -848,7 +847,7 @@ public class JSONArray implements Iterable<Object> {
* @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;
}
@ -920,7 +919,7 @@ public class JSONArray implements Iterable<Object> {
* 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;
}

View file

@ -243,15 +243,13 @@ public class JSONObject {
* the JSONObject.
* @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();
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));
}
}
}
@ -1204,7 +1202,7 @@ public class JSONObject {
* @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;
}
@ -1268,7 +1266,7 @@ public class JSONObject {
* @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;
}
@ -1663,13 +1661,11 @@ public class JSONObject {
return value.toString();
}
if (value instanceof Map) {
@SuppressWarnings("unchecked")
Map<String, Object> map = (Map<String, Object>) value;
Map<?, ?> map = (Map<?, ?>) value;
return new JSONObject(map).toString();
}
if (value instanceof Collection) {
@SuppressWarnings("unchecked")
Collection<Object> coll = (Collection<Object>) value;
Collection<?> coll = (Collection<?>) value;
return new JSONArray(coll).toString();
}
if (value.getClass().isArray()) {
@ -1707,16 +1703,14 @@ public class JSONObject {
}
if (object instanceof Collection) {
@SuppressWarnings("unchecked")
Collection<Object> coll = (Collection<Object>) object;
Collection<?> coll = (Collection<?>) object;
return new JSONArray(coll);
}
if (object.getClass().isArray()) {
return new JSONArray(object);
}
if (object instanceof Map) {
@SuppressWarnings("unchecked")
Map<String, Object> map = (Map<String, Object>) object;
Map<?, ?> map = (Map<?, ?>) object;
return new JSONObject(map);
}
Package objectPackage = object.getClass().getPackage();
@ -1755,14 +1749,11 @@ public class JSONObject {
} else if (value instanceof JSONArray) {
((JSONArray) value).write(writer, indentFactor, indent);
} else if (value instanceof Map) {
@SuppressWarnings("unchecked")
Map<String, Object> map = (Map<String, Object>) value;
Map<?, ?> map = (Map<?, ?>) value;
new JSONObject(map).write(writer, indentFactor, indent);
} else if (value instanceof Collection) {
@SuppressWarnings("unchecked")
Collection<Object> coll = (Collection<Object>) value;
new JSONArray(coll).write(writer, indentFactor,
indent);
Collection<?> coll = (Collection<?>) value;
new JSONArray(coll).write(writer, indentFactor, indent);
} else if (value.getClass().isArray()) {
new JSONArray(value).write(writer, indentFactor, indent);
} else if (value instanceof Number) {