From 64f6aa82168beaa2f3fee9bbd9ae16640e04ee76 Mon Sep 17 00:00:00 2001 From: Vivek Date: Sat, 6 Jun 2020 14:25:20 +0200 Subject: [PATCH] #528, Introduced a method to copy Map content into JSONObject - Method in JSONObject is put(Map) - Also written unit-tests in JSONObjectTest.java for this new function --- src/main/java/org/json/JSONObject.java | 25 +++++++ .../java/org/json/junit/JSONObjectTest.java | 72 +++++++++++++++++++ 2 files changed, 97 insertions(+) diff --git a/src/main/java/org/json/JSONObject.java b/src/main/java/org/json/JSONObject.java index f718c0618..58bf23500 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -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; + } } diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index c24d138c6..473a69852 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -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(){{ 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(){{ 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(){{ 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 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 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()); + } + }