Skip to content

Commit

Permalink
stleary#528, Introduced a method to copy Map content into JSONObject
Browse files Browse the repository at this point in the history
- Method in JSONObject is put(Map)
- Also written unit-tests in JSONObjectTest.java for this new function
  • Loading branch information
Vivek committed Jun 6, 2020
1 parent 70fcc78 commit 64f6aa8
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/main/java/org/json/JSONObject.java
Expand Up @@ -2598,4 +2598,29 @@ private static JSONException wrongValueFormatException(
"JSONObject[" + quote(key) + "] is not a " + valueType + " (" + value + ")."
, cause);
}

/**
* Copies all the content of the given {@link java.util.Map} object and
* puts them into this JSONObject.
*
* @param map
* A map object whose content is copied to this JSONObject.
* @return this.
*/

public JSONObject put(Map<?,?> map) {
if (map != null && !map.isEmpty()) {
for (final Entry<?, ?> e : map.entrySet()) {
final Object key = e.getKey();
if (key == null) {
continue;
}
final Object value = e.getValue();
if (value != null) {
this.map.put(String.valueOf(key), wrap(value));
}
}
}
return this;
}
}
72 changes: 72 additions & 0 deletions src/test/java/org/json/junit/JSONObjectTest.java
Expand Up @@ -3149,4 +3149,76 @@ public void testPutNullObject() {
fail("Expected an exception");
}

@SuppressWarnings("serial")
@Test
public void testJSONObjectPutMap() {
JSONObject jsonObject = new JSONObject("{\"one\":1}");
assertEquals("Value mapped with KEY['one'] must be 1.", 1, jsonObject.get("one"));
assertEquals("Length of JSONObject must be 1.", 1, jsonObject.length());
// Map Inserted
assertNotNull(jsonObject.put(new HashMap<String, Object>(){{ put("two", 2); }}));
assertTrue("JSONObject contains KEY['two'].", jsonObject.has("two"));
assertEquals("Value mapped with KEY['two'] must be 2.", 2, jsonObject.get("two"));
assertEquals("Length of JSONObject must be 2.", 2, jsonObject.length());
}
@Test
public void testJSONObjectPutMapNull() {
JSONObject jsonObject = new JSONObject("{\"one\":1}");
assertEquals("Length of JSONObject must be 1", 1, jsonObject.length());
jsonObject.put(null);
assertEquals("Length of JSONObject must not change and remain 1 like before.",
1, jsonObject.length());
}
@Test
public void testJSONObjectPutMapEmpty() {
JSONObject jsonObject = new JSONObject("{\"one\":1}");
assertEquals("Length of JSONObject must be 1", 1, jsonObject.length());
jsonObject.put(new HashMap<>());
assertEquals("Length of JSONObject must not change and remain 1 like before.",
1, jsonObject.length());
}
@SuppressWarnings("serial")
@Test
public void testJSONObjectPutMapReassignedValue() {
JSONObject jsonObject = new JSONObject("{\"one\":1}");
// Map Inserted
jsonObject.put(new HashMap<String, Object>(){{ put("two", 2); }});
assertEquals("Value mapped with KEY['two'] must be 2.", 2, jsonObject.get("two"));
assertEquals("Length of JSONObject must be 2.", 2, jsonObject.length());
// Map that Reassigns Value Inserted
jsonObject.put(new HashMap<String, Object>(){{ put("two", "two"); }});
assertEquals("Value mapped with KEY['two'] must be 'two'.", "two", jsonObject.get("two"));
assertEquals("Length of JSONObject must not change and remain 2 like before.",
2, jsonObject.length());
}
@Test
public void testJSONObjectPutMapWithNullKey() {
JSONObject jsonObject = new JSONObject("{\"one\":1}");
// Map to be inserted
Map<String, Object> map = new HashMap<>();
map.put(null, 3); // Not Inserted
map.put("three", 3); // Inserted
jsonObject.put(map);
assertFalse("JSONObject should not have KEY[null]", jsonObject.has(null));
assertTrue("JSONObject should not have KEY['two']", jsonObject.has("three"));
assertEquals("Value mapped with KEY['three'] must be 3.", 3, jsonObject.get("three"));
assertEquals("Length of JSONObject increases by 1 (now 2), because only 1 key-value pair is added from map",
2, jsonObject.length());
}
@Test
public void testJSONObjectPutMapWithNullValue() {
JSONObject jsonObject = new JSONObject("{\"one\":1}");
// Map to be inserted
Map<String, Object> map = new HashMap<>();
map.put("two", null); // Not Inserted
jsonObject.put(map);
assertFalse("KEY['two'] must not exist", jsonObject.has("two"));
assertEquals("Length of JSONObject remains constant must be 1", 1, jsonObject.length());
// Modify Map, by assigning a valid value to the key which had null value
map.put("two", 2); // Inserted
jsonObject.put(map);
assertEquals("Value mapped with KEY['two'] must be 2.", 2, jsonObject.get("two"));
assertEquals("Length of JSONObject increments by 1, now 2", 2, jsonObject.length());
}

}

0 comments on commit 64f6aa8

Please sign in to comment.