### Question:
StripeJsonUtils { @Nullable static Map<String, Object> jsonObjectToMap(@Nullable JSONObject jsonObject) { if (jsonObject == null) { return null; } Map<String, Object> map = new HashMap<>(); Iterator<String> keyIterator = jsonObject.keys(); while(keyIterator.hasNext()) { String key = keyIterator.next(); Object value = jsonObject.opt(key); if (NULL.equals(value) || value == null) { continue; } if (value instanceof JSONObject) { map.put(key, jsonObjectToMap((JSONObject) value)); } else if (value instanceof JSONArray) { map.put(key, jsonArrayToList((JSONArray) value)); } else { map.put(key, value); } } return map; }    }### Answer:
@Test public void jsonObjectToMap_forNull_returnsNull() { assertNull(StripeJsonUtils.jsonObjectToMap(null)); }
@Test public void jsonObjectToMap_forSimpleObjects_returnsExpectedMap() { Map<String, Object> expectedMap = new HashMap<>(); expectedMap.put("akey", "avalue"); expectedMap.put("bkey", "bvalue"); expectedMap.put("boolkey", true); expectedMap.put("numkey", 123); try { JSONObject testJsonObject = new JSONObject(SIMPLE_JSON_TEST_OBJECT); Map<String, Object> mappedObject = StripeJsonUtils.jsonObjectToMap(testJsonObject); JsonTestUtils.assertMapEquals(expectedMap, mappedObject); } catch (JSONException jsonException) { fail("Test data failure " + jsonException.getLocalizedMessage()); } }
@Test public void jsonObjectToMap_forNestedObjects_returnsExpectedMap() { Map<String, Object> expectedMap = new HashMap<>(); expectedMap.put("top_key", new HashMap<String, Object>() {{ put("first_inner_key", new HashMap<String, Object>() {{ put("innermost_key", 1000); put("second_innermost_key", "second_inner_value"); }}); put("second_inner_key", "just a value"); }}); expectedMap.put("second_outer_key", new HashMap<String, Object>() {{ put("another_inner_key", false); }}); try { JSONObject testJsonObject = new JSONObject(NESTED_JSON_TEST_OBJECT); Map<String, Object> mappedObject = StripeJsonUtils.jsonObjectToMap(testJsonObject); JsonTestUtils.assertMapEquals(expectedMap, mappedObject); } catch (JSONException jsonException) { fail("Test data failure " + jsonException.getLocalizedMessage()); } }
@Test public void jsonObjectToMap_withNestedObjectAndArrays_returnsExpectedMap() { Map<String, Object> expectedMap = new HashMap<>(); expectedMap.put("other_outer_key", false); final List<Object> itemsList = new ArrayList<>(); itemsList.add(new HashMap<String, Object>() {{ put("id", 123); }}); itemsList.add(new HashMap<String, Object>() {{ put("id", "this time with letters"); }}); itemsList.add("a string item"); itemsList.add(256); itemsList.add(Arrays.asList(1, 2, "C", 4)); itemsList.add(Arrays.asList(new HashMap<String, Object>() {{ put("deep", "deepValue"); }})); expectedMap.put("outer_key", new HashMap<String, Object>() {{ put("items", itemsList); put("another_key", "a simple value this time"); }}); try { JSONObject testJsonObject = new JSONObject(NESTED_MIXED_ARRAY_OBJECT); Map<String, Object> convertedMap = StripeJsonUtils.jsonObjectToMap(testJsonObject); JsonTestUtils.assertMapEquals(expectedMap, convertedMap); } catch (JSONException jsonException) { fail("Test data failure " + jsonException.getLocalizedMessage()); } }