@Test public void jsonObjectToMapWithoutKeys_whenHasKeyInput_returnsMapOmittingKeys() { JSONObject jsonObject = new JSONObject(); try { jsonObject.put("a_key", "a_value"); jsonObject.put("b_key", "b_value"); jsonObject.put("c_key", "c_value"); jsonObject.put("d_key", "d_value"); } catch (JSONException unexpected) { fail("Unexpected error: " + unexpected.getLocalizedMessage()); } Set<String> omitKeys = new HashSet<String>() {{ add("a_key"); add("d_key"); }}; Map<String, Object> resultMap = jsonObjectToMapWithoutKeys(jsonObject, omitKeys); assertNotNull(resultMap); assertEquals(2, resultMap.size()); assertEquals("b_value", resultMap.get("b_key")); assertEquals("c_value", resultMap.get("c_key")); assertFalse(resultMap.containsKey("a_key")); assertFalse(resultMap.containsKey("d_key")); }
@Nullable static Map<String, Object> jsonObjectToMapWithoutKeys( @Nullable JSONObject jsonObject, @Nullable Set<String> omitKeys) { if (jsonObject == null) { return null; } Set<String> keysToOmit = omitKeys == null ? new HashSet<String>() : omitKeys; 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 || keysToOmit.contains(key)) { continue; } map.put(key, value); } if (map.isEmpty()) { return null; } else { return map; } }
StripeSourceTypeModel extends StripeJsonModel { @Nullable static Map<String, Object> jsonObjectToMapWithoutKeys( @Nullable JSONObject jsonObject, @Nullable Set<String> omitKeys) { if (jsonObject == null) { return null; } Set<String> keysToOmit = omitKeys == null ? new HashSet<String>() : omitKeys; 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 || keysToOmit.contains(key)) { continue; } map.put(key, value); } if (map.isEmpty()) { return null; } else { return map; } } }
StripeSourceTypeModel extends StripeJsonModel { @Nullable static Map<String, Object> jsonObjectToMapWithoutKeys( @Nullable JSONObject jsonObject, @Nullable Set<String> omitKeys) { if (jsonObject == null) { return null; } Set<String> keysToOmit = omitKeys == null ? new HashSet<String>() : omitKeys; 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 || keysToOmit.contains(key)) { continue; } map.put(key, value); } if (map.isEmpty()) { return null; } else { return map; } } StripeSourceTypeModel(); }
StripeSourceTypeModel extends StripeJsonModel { @Nullable static Map<String, Object> jsonObjectToMapWithoutKeys( @Nullable JSONObject jsonObject, @Nullable Set<String> omitKeys) { if (jsonObject == null) { return null; } Set<String> keysToOmit = omitKeys == null ? new HashSet<String>() : omitKeys; 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 || keysToOmit.contains(key)) { continue; } map.put(key, value); } if (map.isEmpty()) { return null; } else { return map; } } StripeSourceTypeModel(); @NonNull Map<String, Object> getAdditionalFields(); }
StripeSourceTypeModel extends StripeJsonModel { @Nullable static Map<String, Object> jsonObjectToMapWithoutKeys( @Nullable JSONObject jsonObject, @Nullable Set<String> omitKeys) { if (jsonObject == null) { return null; } Set<String> keysToOmit = omitKeys == null ? new HashSet<String>() : omitKeys; 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 || keysToOmit.contains(key)) { continue; } map.put(key, value); } if (map.isEmpty()) { return null; } else { return map; } } StripeSourceTypeModel(); @NonNull Map<String, Object> getAdditionalFields();  }