Skip to content

Commit 813aee7

Browse files
author
Leonardo Lopes
committed
new methods putCollection and putMap
1 parent c6f3de9 commit 813aee7

File tree

2 files changed

+65
-7
lines changed

2 files changed

+65
-7
lines changed

src/main/java/org/json/JSONObject.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1709,7 +1709,7 @@ public JSONObject put(String key, boolean value) throws JSONException {
17091709
* @throws NullPointerException
17101710
* If the key is <code>null</code>.
17111711
*/
1712-
public JSONObject put(String key, Collection<?> value) throws JSONException {
1712+
public JSONObject putCollection(String key, Collection<?> value) throws JSONException {
17131713
return this.put(key, new JSONArray(value));
17141714
}
17151715

@@ -1795,7 +1795,7 @@ public JSONObject put(String key, long value) throws JSONException {
17951795
* @throws NullPointerException
17961796
* If the key is <code>null</code>.
17971797
*/
1798-
public JSONObject put(String key, Map<?, ?> value) throws JSONException {
1798+
public JSONObject putMap(String key, Map<?, ?> value) throws JSONException {
17991799
return this.put(key, new JSONObject(value));
18001800
}
18011801

@@ -1821,14 +1821,22 @@ public JSONObject put(String key, Object value) throws JSONException {
18211821
if (key == null) {
18221822
throw new NullPointerException("Null key.");
18231823
}
1824-
if (value != null) {
1825-
testValidity(value);
1826-
this.map.put(key, value);
1827-
} else {
1824+
1825+
if (value == null) {
18281826
// just...
18291827
// works
18301828
// btw idc
18311829
this.map.put(key, JSONObject.NULL);
1830+
} else {
1831+
testValidity(value);
1832+
1833+
if (value instanceof Collection) {
1834+
this.putCollection(key, (Collection<?>) value);
1835+
} else if (value instanceof Map) {
1836+
this.putMap(key, (Map<?, ?>) value);
1837+
} else {
1838+
this.map.put(key, value);
1839+
}
18321840
}
18331841
return this;
18341842
}

src/test/java/org/json/junit/JSONTest.java

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,56 @@
1111
import org.junit.Test;
1212

1313
public class JSONTest {
14+
@Test
15+
public void testConstructor() {
16+
final JSONObject expected = new JSONObject("{\"myKey\":10}");
17+
18+
@SuppressWarnings("rawtypes")
19+
Map myRawC = Collections.singletonMap("myKey", Integer.valueOf(10));
20+
JSONObject jaRaw = new JSONObject(myRawC);
21+
22+
Map<String, Object> myCStrObj = Collections.singletonMap("myKey", (Object) Integer.valueOf(10));
23+
JSONObject jaStrObj = new JSONObject(myCStrObj);
24+
25+
Map<String, Integer> myCStrInt = Collections.singletonMap("myKey", Integer.valueOf(10));
26+
JSONObject jaStrInt = new JSONObject(myCStrInt);
27+
28+
Map<?, ?> myCObjObj = Collections.singletonMap((Object) "myKey", (Object) Integer.valueOf(10));
29+
JSONObject jaObjObj = new JSONObject(myCObjObj);
30+
31+
assertTrue("The RAW Collection should give me the same as the Typed Collection", expected.similar(jaRaw));
32+
assertTrue("The RAW Collection should give me the same as the Typed Collection", expected.similar(jaStrObj));
33+
assertTrue("The RAW Collection should give me the same as the Typed Collection", expected.similar(jaStrInt));
34+
assertTrue("The RAW Collection should give me the same as the Typed Collection", expected.similar(jaObjObj));
35+
}
36+
37+
@Test
38+
public void testPutMap() {
39+
final JSONObject expected = new JSONObject("{\"myMap\":{\"myKey\":10}}");
40+
41+
@SuppressWarnings("rawtypes")
42+
Map myRawC = Collections.singletonMap("myKey", Integer.valueOf(10));
43+
JSONObject jaRaw = new JSONObject();
44+
jaRaw.put("myMap", myRawC);
45+
46+
Map<String, Object> myCStrObj = Collections.singletonMap("myKey", (Object) Integer.valueOf(10));
47+
JSONObject jaStrObj = new JSONObject();
48+
jaStrObj.put("myMap", myCStrObj);
49+
50+
Map<String, Integer> myCStrInt = Collections.singletonMap("myKey", Integer.valueOf(10));
51+
JSONObject jaStrInt = new JSONObject();
52+
jaStrInt.put("myMap", myCStrInt);
53+
54+
Map<?, ?> myCObjObj = Collections.singletonMap((Object) "myKey", (Object) Integer.valueOf(10));
55+
JSONObject jaObjObj = new JSONObject();
56+
jaObjObj.put("myMap", myCObjObj);
57+
58+
assertTrue("The RAW Collection should give me the same as the Typed Collection", expected.similar(jaRaw));
59+
assertTrue("The RAW Collection should give me the same as the Typed Collection", expected.similar(jaStrObj));
60+
assertTrue("The RAW Collection should give me the same as the Typed Collection", expected.similar(jaStrInt));
61+
assertTrue("The RAW Collection should give me the same as the Typed Collection", expected.similar(jaObjObj));
62+
}
63+
1464
@Test
1565
public void testMapWithNullValue() {
1666
Map<String, Object> map = new HashMap<String, Object>();
@@ -50,7 +100,7 @@ public void testPutNull() {
50100
assertTrue("jsonObject should be empty", jsonObjectRemove.isEmpty());
51101

52102
JSONObject jsonObjectPutNull = new JSONObject(str);
53-
jsonObjectPutNull.put("myKey", (Object) null);
103+
jsonObjectPutNull.put("myKey", null);
54104
assertTrue("jsonObject should NOT be empty", !jsonObjectPutNull.isEmpty());
55105
}
56106

0 commit comments

Comments
 (0)