/** * Calls through to {@link JSONObject#optJSONObject(String)} and then * uses {@link #jsonObjectToStringMap(JSONObject)} on the result. * * @param jsonObject the input object * @param fieldName the required field name * @return the value stored in the requested field, or {@code null} if the key is not present */ @Nullable static Map<String, String> optHash( @NonNull JSONObject jsonObject, @NonNull @Size(min = 1) String fieldName) { JSONObject foundObject = jsonObject.optJSONObject(fieldName); if (foundObject == null) { return null; } return jsonObjectToStringMap(foundObject); }
@Test public void jsonObjectToStringMap_forSimpleObjects_returnsExpectedMap() { Map<String, String> 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, String> mappedObject = StripeJsonUtils.jsonObjectToStringMap(testJsonObject); JsonTestUtils.assertMapEquals(expectedMap, mappedObject); } catch (JSONException jsonException) { fail("Test data failure " + jsonException.getLocalizedMessage()); } }
@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()); } }
Boolean liveMode = jsonObject.optBoolean(FIELD_LIVEMODE); Map<String, String> metadata = StripeJsonUtils.jsonObjectToStringMap(jsonObject.optJSONObject(FIELD_METADATA)); SourceOwner owner = optStripeJsonModel(jsonObject, FIELD_OWNER, SourceOwner.class); SourceReceiver receiver = optStripeJsonModel(