@Nullable @ThreeDSecureStatus static String asThreeDSecureStatus(@Nullable String threeDSecureStatus) { String nullChecked = StripeJsonUtils.nullIfNullOrEmpty(threeDSecureStatus); if (nullChecked == null) { return null; } if (REQUIRED.equalsIgnoreCase(threeDSecureStatus)) { return REQUIRED; } else if (OPTIONAL.equalsIgnoreCase(threeDSecureStatus)) { return OPTIONAL; } else if (NOT_SUPPORTED.equalsIgnoreCase(threeDSecureStatus)) { return NOT_SUPPORTED; } else { return UNKNOWN; } } }
/** * Calls through to {@link JSONObject#optString(String)} while safely * converting the raw string "null" and the empty string to {@code null}. Will not throw * an exception if the field isn't found. * * @param jsonObject the input object * @param fieldName the optional field name * @return the value stored in the field, or {@code null} if the field isn't present */ @Nullable static String optString( @NonNull JSONObject jsonObject, @NonNull @Size(min = 1) String fieldName) { return nullIfNullOrEmpty(jsonObject.optString(fieldName)); }
/** * Calls through to {@link JSONObject#getString(String)} while safely * converting the raw string "null" and the empty string to {@code null}. * * @param jsonObject the input object * @param fieldName the required field name * @return the value stored in the requested field * @throws JSONException if the field does not exist */ @Nullable static String getString( @NonNull JSONObject jsonObject, @NonNull @Size(min = 1) String fieldName) throws JSONException { return nullIfNullOrEmpty(jsonObject.getString(fieldName)); }
/** * Calls through to {@link JSONObject#optString(String)} while safely converting * the raw string "null" and the empty string to {@code null}, along with any value that isn't * a two-character string. * @param jsonObject the object from which to retrieve the country code * @param fieldName the name of the field in which the country code is stored * @return a two-letter country code if one is found, or {@code null} */ @Nullable @Size(2) static String optCountryCode( @NonNull JSONObject jsonObject, @NonNull @Size(min = 1) String fieldName) { String value = nullIfNullOrEmpty(jsonObject.optString(fieldName)); if (value != null && value.length() == 2) { return value; } return null; }
/** * Calls through to {@link JSONObject#optString(String)} while safely converting * the raw string "null" and the empty string to {@code null}, along with any value that isn't * a three-character string. * @param jsonObject the object from which to retrieve the currency code * @param fieldName the name of the field in which the currency code is stored * @return a three-letter currency code if one is found, or {@code null} */ @Nullable @Size(3) static String optCurrency( @NonNull JSONObject jsonObject, @NonNull @Size(min = 1) String fieldName) { String value = nullIfNullOrEmpty(jsonObject.optString(fieldName)); if (value != null && value.length() == 3) { return value; } return null; }
@Test public void nullIfNullOrEmpty_returnsNullForNull() { assertNull(StripeJsonUtils.nullIfNullOrEmpty("null")); }
@Test public void nullIfNullOrEmpty_returnsInputIfNotNull() { final String notANull = "notANull"; assertEquals(notANull, StripeJsonUtils.nullIfNullOrEmpty(notANull)); }
@Test public void nullIfNullOrEmpty_returnsNullForEmpty() { assertNull(StripeJsonUtils.nullIfNullOrEmpty("")); }