/** * Util function for putting a String-Object map value into a {@link JSONObject} if that * value is not null. This ignores any {@link JSONException} that may be thrown due to * insertion. * * @param jsonObject the {@link JSONObject} into which to put the field * @param fieldName the field name * @param value the potential field value */ static void putMapIfNotNull( @NonNull JSONObject jsonObject, @NonNull @Size(min = 1) String fieldName, @Nullable Map<String, Object> value ) { if (value == null) { return; } JSONObject mapObject = mapToJsonObject(value); if (mapObject == null) { return; } try { jsonObject.put(fieldName, mapObject); } catch (JSONException ignored) { } }
try { Map<String, Object> mapObject = (Map<String, Object>) object; jsonArray.put(mapToJsonObject(mapObject)); } catch (ClassCastException classCastException) {
try { Map<String, Object> mapValue = (Map<String, Object>) value; jsonObject.put(key, mapToJsonObject(mapValue)); } catch (ClassCastException classCastException) {
jsonObject.put(FIELD_LIVEMODE, mLiveMode); JSONObject metaDataObject = mapToJsonObject(mMetaData); if (metaDataObject != null) { jsonObject.put(FIELD_METADATA, metaDataObject); JSONObject sourceTypeJsonObject = mapToJsonObject(mSourceTypeData);
@Test public void mapToJsonObject_forSimpleObjects_returnsExpectedObject() { Map<String, Object> testMap = new HashMap<>(); testMap.put("akey", "avalue"); testMap.put("bkey", "bvalue"); testMap.put("boolkey", true); testMap.put("numkey", 123); try { JSONObject expectedJsonObject = new JSONObject(SIMPLE_JSON_TEST_OBJECT); JSONObject testObject = StripeJsonUtils.mapToJsonObject(testMap); JsonTestUtils.assertJsonEquals(expectedJsonObject, testObject); } catch (JSONException jsonException) { fail("Test data failure " + jsonException.getLocalizedMessage()); } }
@Test public void mapToJsonObject_forNull_returnsNull() { assertNull(StripeJsonUtils.mapToJsonObject(null)); }
@Test public void mapToJsonObject_withNestedMapAndLists_returnsExpectedObject() { Map<String, Object> testMap = new HashMap<>(); testMap.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"); }})); testMap.put("outer_key", new HashMap<String, Object>() {{ put("items", itemsList); put("another_key", "a simple value this time"); }}); try { JSONObject expectedJsonObject = new JSONObject(NESTED_MIXED_ARRAY_OBJECT); JSONObject testJsonObject = StripeJsonUtils.mapToJsonObject(testMap); JsonTestUtils.assertJsonEquals(expectedJsonObject, testJsonObject); } catch (JSONException jsonException) { fail("Test data failure " + jsonException.getLocalizedMessage()); } }
@Test public void mapToJsonObject_forNestedMaps_returnsExpectedObject() { Map<String, Object> testMap = new HashMap<>(); testMap.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"); }}); testMap.put("second_outer_key", new HashMap<String, Object>() {{ put("another_inner_key", false); }}); try { JSONObject expectedJsonObject = new JSONObject(NESTED_JSON_TEST_OBJECT); JSONObject testJsonObject = StripeJsonUtils.mapToJsonObject(testMap); JsonTestUtils.assertJsonEquals(expectedJsonObject, testJsonObject); } catch (JSONException jsonException) { fail("Test data failure " + jsonException.getLocalizedMessage()); } }
@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()); } }