Skip to content

Commit ec8f649

Browse files
author
John J. Aylward
committed
2 parents 105426b + 39e3ccc commit ec8f649

File tree

3 files changed

+33
-38
lines changed

3 files changed

+33
-38
lines changed

JSONArray.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ of this software and associated documentation files (the "Software"), to deal
7676
* </ul>
7777
*
7878
* @author JSON.org
79-
* @version 2015-07-22
79+
* @version 2015-10-29
8080
*/
8181
public class JSONArray implements Iterable<Object> {
8282

@@ -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: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ of this software and associated documentation files (the "Software"), to deal
9292
* </ul>
9393
*
9494
* @author JSON.org
95-
* @version 2015-07-22
95+
* @version 2015-12-05
9696
*/
9797
public class JSONObject {
9898
/**
@@ -165,10 +165,6 @@ public JSONObject() {
165165
* A JSONObject.
166166
* @param names
167167
* An array of strings.
168-
* @throws JSONException
169-
* @exception JSONException
170-
* If a value is a non-finite number or if a name is
171-
* duplicated.
172168
*/
173169
public JSONObject(JSONObject jo, String[] names) {
174170
this();
@@ -241,17 +237,14 @@ public JSONObject(JSONTokener x) throws JSONException {
241237
* @param map
242238
* A map object that can be used to initialize the contents of
243239
* the JSONObject.
244-
* @throws JSONException
245240
*/
246-
public JSONObject(Map<String, Object> map) {
241+
public JSONObject(Map<?, ?> map) {
247242
this.map = new HashMap<String, Object>();
248243
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();
244+
for (final Entry<?, ?> e : map.entrySet()) {
245+
final Object value = e.getValue();
253246
if (value != null) {
254-
this.map.put(entry.getKey(), wrap(value));
247+
this.map.put(String.valueOf(e.getKey()), wrap(value));
255248
}
256249
}
257250
}
@@ -1204,7 +1197,7 @@ public JSONObject put(String key, boolean value) throws JSONException {
12041197
* @return this.
12051198
* @throws JSONException
12061199
*/
1207-
public JSONObject put(String key, Collection<Object> value) throws JSONException {
1200+
public JSONObject put(String key, Collection<?> value) throws JSONException {
12081201
this.put(key, new JSONArray(value));
12091202
return this;
12101203
}
@@ -1268,7 +1261,7 @@ public JSONObject put(String key, long value) throws JSONException {
12681261
* @return this.
12691262
* @throws JSONException
12701263
*/
1271-
public JSONObject put(String key, Map<String, Object> value) throws JSONException {
1264+
public JSONObject put(String key, Map<?, ?> value) throws JSONException {
12721265
this.put(key, new JSONObject(value));
12731266
return this;
12741267
}
@@ -1663,13 +1656,11 @@ public static String valueToString(Object value) throws JSONException {
16631656
return value.toString();
16641657
}
16651658
if (value instanceof Map) {
1666-
@SuppressWarnings("unchecked")
1667-
Map<String, Object> map = (Map<String, Object>) value;
1659+
Map<?, ?> map = (Map<?, ?>) value;
16681660
return new JSONObject(map).toString();
16691661
}
16701662
if (value instanceof Collection) {
1671-
@SuppressWarnings("unchecked")
1672-
Collection<Object> coll = (Collection<Object>) value;
1663+
Collection<?> coll = (Collection<?>) value;
16731664
return new JSONArray(coll).toString();
16741665
}
16751666
if (value.getClass().isArray()) {
@@ -1707,16 +1698,14 @@ public static Object wrap(Object object) {
17071698
}
17081699

17091700
if (object instanceof Collection) {
1710-
@SuppressWarnings("unchecked")
1711-
Collection<Object> coll = (Collection<Object>) object;
1701+
Collection<?> coll = (Collection<?>) object;
17121702
return new JSONArray(coll);
17131703
}
17141704
if (object.getClass().isArray()) {
17151705
return new JSONArray(object);
17161706
}
17171707
if (object instanceof Map) {
1718-
@SuppressWarnings("unchecked")
1719-
Map<String, Object> map = (Map<String, Object>) object;
1708+
Map<?, ?> map = (Map<?, ?>) object;
17201709
return new JSONObject(map);
17211710
}
17221711
Package objectPackage = object.getClass().getPackage();
@@ -1755,14 +1744,11 @@ static final Writer writeValue(Writer writer, Object value,
17551744
} else if (value instanceof JSONArray) {
17561745
((JSONArray) value).write(writer, indentFactor, indent);
17571746
} else if (value instanceof Map) {
1758-
@SuppressWarnings("unchecked")
1759-
Map<String, Object> map = (Map<String, Object>) value;
1747+
Map<?, ?> map = (Map<?, ?>) value;
17601748
new JSONObject(map).write(writer, indentFactor, indent);
17611749
} else if (value instanceof Collection) {
1762-
@SuppressWarnings("unchecked")
1763-
Collection<Object> coll = (Collection<Object>) value;
1764-
new JSONArray(coll).write(writer, indentFactor,
1765-
indent);
1750+
Collection<?> coll = (Collection<?>) value;
1751+
new JSONArray(coll).write(writer, indentFactor, indent);
17661752
} else if (value.getClass().isArray()) {
17671753
new JSONArray(value).write(writer, indentFactor, indent);
17681754
} else if (value instanceof Number) {

README

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,13 @@ JSONML.java: JSONML provides support for converting between JSONML and XML.
6262
XMLTokener.java: XMLTokener extends JSONTokener for parsing XML text.
6363

6464
Unit tests are maintained in a separate project. Contributing developers can test JSON-java pull requests with the code in this project: https://github.com/stleary/JSON-Java-unit-test
65+
66+
Release history:
67+
68+
20151123 JSONObject and JSONArray initialization with generics. Contains the
69+
latest code as of 23 Nov, 2015.
70+
71+
20150729 Checkpoint for Maven central repository release. Contains the latest code as of 29 July, 2015.
72+
73+
JSON-java releases can be found by searching the Maven repository for groupId "org.json" and artifactId "json". For example:
74+
https://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22org.json%22%20AND%20a%3A%22json%22

0 commit comments

Comments
 (0)