@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()); } }
@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; }
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; } }
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; }  }
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; }   }
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; }    }