### Question:
StripeJsonUtils { @Nullable @SuppressWarnings("unchecked") static JSONObject mapToJsonObject(@Nullable Map<String, ? extends Object> mapObject) { if (mapObject == null) { return null; } JSONObject jsonObject = new JSONObject(); for (String key : mapObject.keySet()) { Object value = mapObject.get(key); if (value == null) { continue; } try { if (value instanceof Map<?, ?>) { try { Map<String, Object> mapValue = (Map<String, Object>) value; jsonObject.put(key, mapToJsonObject(mapValue)); } catch (ClassCastException classCastException) { } } else if (value instanceof List<?>) { jsonObject.put(key, listToJsonArray((List<Object>) value)); } else if (value instanceof Number || value instanceof Boolean) { jsonObject.put(key, value); } else { jsonObject.put(key, value.toString()); } } catch (JSONException jsonException) { } } return jsonObject; }    }### Answer:
@Test public void mapToJsonObject_forNull_returnsNull() { assertNull(StripeJsonUtils.mapToJsonObject(null)); }
@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_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 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()); } }