@Test public void jsonObjectToStringMap_forNestedObjects_returnsExpectedFlatMap() { Map<String, String> expectedMap = new HashMap<>(); expectedMap.put("top_key", "{\"first_inner_key\":{\"innermost_key\":1000," + "\"second_innermost_key\":\"second_inner_value\"}," + "\"second_inner_key\":\"just a value\"}"); expectedMap.put("second_outer_key", "{\"another_inner_key\":false}"); try { JSONObject testJsonObject = new JSONObject(NESTED_JSON_TEST_OBJECT); Map<String, String> mappedObject = StripeJsonUtils.jsonObjectToStringMap(testJsonObject); JsonTestUtils.assertMapEquals(expectedMap, mappedObject); } catch (JSONException jsonException) { fail("Test data failure " + jsonException.getLocalizedMessage()); } }
@Nullable static Map<String, String> jsonObjectToStringMap(@Nullable JSONObject jsonObject) { if (jsonObject == null) { return null; } Map<String, String> 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; } map.put(key, value.toString()); } return map; }
StripeJsonUtils { @Nullable static Map<String, String> jsonObjectToStringMap(@Nullable JSONObject jsonObject) { if (jsonObject == null) { return null; } Map<String, String> 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; } map.put(key, value.toString()); } return map; } }
StripeJsonUtils { @Nullable static Map<String, String> jsonObjectToStringMap(@Nullable JSONObject jsonObject) { if (jsonObject == null) { return null; } Map<String, String> 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; } map.put(key, value.toString()); } return map; }  }
StripeJsonUtils { @Nullable static Map<String, String> jsonObjectToStringMap(@Nullable JSONObject jsonObject) { if (jsonObject == null) { return null; } Map<String, String> 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; } map.put(key, value.toString()); } return map; }   }
StripeJsonUtils { @Nullable static Map<String, String> jsonObjectToStringMap(@Nullable JSONObject jsonObject) { if (jsonObject == null) { return null; } Map<String, String> 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; } map.put(key, value.toString()); } return map; }    }