/** * Calls through to {@link JSONObject#optJSONObject(String)} and then * uses {@link #jsonObjectToMap(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, Object> optMap( @NonNull JSONObject jsonObject, @NonNull @Size(min = 1) String fieldName) { JSONObject foundObject = jsonObject.optJSONObject(fieldName); if (foundObject == null) { return null; } return jsonObjectToMap(foundObject); }
objectList.add(jsonArrayToList((JSONArray) ob)); } else if (ob instanceof JSONObject) { Map<String, Object> objectMap = jsonObjectToMap((JSONObject) ob); if (objectMap != null) { objectList.add(objectMap);
/** * Convert a {@link JSONObject} to a {@link Map}. * * @param jsonObject a {@link JSONObject} to be converted * @return a {@link Map} representing the input, or {@code null} if the input is {@code null} */ @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; }
@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_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()); } }
@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 toMap_catchesAllFields_fromRawJson() { try { final JSONObject rawJsonObject = new JSONObject(JSON_CARD); final Map<String, Object> rawMap = StripeJsonUtils.jsonObjectToMap(rawJsonObject); final Card expectedCard = buildEquivalentJsonCard(); JsonTestUtils.assertMapEquals(rawMap, expectedCard.toMap()); } catch (JSONException unexpected) { fail(); } }
@Test public void mapToJsonObjectAndBackToMap_forComplicatedObject_isNoOp() { try { JSONObject testJsonObject = new JSONObject(NESTED_MIXED_ARRAY_OBJECT); Map<String, Object> convertedMap = StripeJsonUtils.jsonObjectToMap(testJsonObject); JSONObject cycledObject = StripeJsonUtils.mapToJsonObject(convertedMap); JsonTestUtils.assertJsonEquals(cycledObject, testJsonObject); } catch (JSONException jsonException) { fail("Test data failure " + jsonException.getLocalizedMessage()); } }
StripeJsonUtils.jsonObjectToMap(jsonObject.optJSONObject(typeRaw)); StripeSourceTypeModel sourceTypeModel = MODELED_TYPES.contains(typeRaw)