Object ob = jsonArray.get(i); if (ob instanceof JSONArray) { objectList.add(jsonArrayToList((JSONArray) ob)); } else if (ob instanceof JSONObject) { Map<String, Object> objectMap = jsonObjectToMap((JSONObject) ob); if (objectMap != null) { objectList.add(objectMap);
/** * Calls through to {@link JSONObject#optJSONObject(String)} and then * uses {@link #jsonObjectToStringMap(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, String> optHash( @NonNull JSONObject jsonObject, @NonNull @Size(min = 1) String fieldName) { JSONObject foundObject = jsonObject.optJSONObject(fieldName); if (foundObject == null) { return null; } return jsonObjectToStringMap(foundObject); }
@NonNull @Override public JSONObject toJson() { JSONObject jsonObject = new JSONObject(); putStringIfNotNull(jsonObject, FIELD_ID, mId); putStringIfNotNull(jsonObject, FIELD_OBJECT, mObjectType); putArrayIfNotNull(jsonObject, FIELD_ALLOWED_SOURCE_TYPES, listToJsonArray(mAllowedSourceTypes)); putLongIfNotNull(jsonObject, FIELD_AMOUNT, mAmount); putLongIfNotNull(jsonObject, FIELD_CANCELED, mCanceledAt); putStringIfNotNull(jsonObject, FIELD_CAPTURE_METHOD, mCaptureMethod); putStringIfNotNull(jsonObject, FIELD_CLIENT_SECRET, mClientSecret); putStringIfNotNull(jsonObject, FIELD_CONFIRMATION_METHOD, mConfirmationMethod); putLongIfNotNull(jsonObject, FIELD_CREATED, mCreated); putStringIfNotNull(jsonObject, FIELD_CURRENCY, mCurrency); putStringIfNotNull(jsonObject, FIELD_DESCRIPTION, mDescription); putBooleanIfNotNull(jsonObject, FIELD_LIVEMODE, mLiveMode); putMapIfNotNull(jsonObject, FIELD_NEXT_SOURCE_ACTION, mNextSourceAction); putStringIfNotNull(jsonObject, FIELD_RECEIPT_EMAIL, mReceiptEmail); putStringIfNotNull(jsonObject, FIELD_SOURCE, mSource); putStringIfNotNull(jsonObject, FIELD_STATUS, mStatus); return jsonObject; }
@Nullable public static BankAccount fromJson(@Nullable JSONObject jsonObject) { if (jsonObject == null) { return null; } return new BankAccount( StripeJsonUtils.optString(jsonObject, FIELD_ACCOUNT_HOLDER_NAME), asBankAccountType( StripeJsonUtils.optString(jsonObject, FIELD_ACCOUNT_HOLDER_TYPE)), StripeJsonUtils.optString(jsonObject, FIELD_BANK_NAME), StripeJsonUtils.optCountryCode(jsonObject, FIELD_COUNTRY), StripeJsonUtils.optCurrency(jsonObject, FIELD_CURRENCY), StripeJsonUtils.optString(jsonObject, FIELD_FINGERPRINT), StripeJsonUtils.optString(jsonObject, FIELD_LAST4), StripeJsonUtils.optString(jsonObject, FIELD_ROUTING_NUMBER)); } }
return null; String id = optString(jsonObject, FIELD_ID); String objectType = optString(jsonObject, FIELD_OBJECT); JSONArray allowedSourceTypesJSONArray = jsonObject.optJSONArray(FIELD_ALLOWED_SOURCE_TYPES); List<String> allowedSourceTypes = new ArrayList<>(); Long amount = optLong(jsonObject, FIELD_AMOUNT); Long canceledAt = optLong(jsonObject, FIELD_CANCELED); String captureMethod = optString(jsonObject, FIELD_CAPTURE_METHOD); String clientSecret = optString(jsonObject, FIELD_CLIENT_SECRET); String confirmationMethod = optString(jsonObject, FIELD_CONFIRMATION_METHOD); Long created = optLong(jsonObject, FIELD_CREATED); String currency = optCurrency(jsonObject, FIELD_CURRENCY); String description = optString(jsonObject, FIELD_DESCRIPTION); Boolean livemode = optBoolean(jsonObject, FIELD_LIVEMODE); String receiptEmail = optString(jsonObject, FIELD_RECEIPT_EMAIL); String status = optString(jsonObject, FIELD_STATUS); Map<String, Object> nextSourceAction = optMap(jsonObject, FIELD_NEXT_SOURCE_ACTION); String source = optString(jsonObject, FIELD_SOURCE);
String id = optString(jsonObject, FIELD_ID); Long amount = optLong(jsonObject, FIELD_AMOUNT); String clientSecret = optString(jsonObject, FIELD_CLIENT_SECRET); SourceCodeVerification codeVerification = optStripeJsonModel( jsonObject, FIELD_CODE_VERIFICATION, SourceCodeVerification.class); Long created = optLong(jsonObject, FIELD_CREATED); String currency = optString(jsonObject, FIELD_CURRENCY); @SourceFlow String flow = asSourceFlow(optString(jsonObject, FIELD_FLOW)); Boolean liveMode = jsonObject.optBoolean(FIELD_LIVEMODE); Map<String, String> metadata = StripeJsonUtils.jsonObjectToStringMap(jsonObject.optJSONObject(FIELD_METADATA)); SourceOwner owner = optStripeJsonModel(jsonObject, FIELD_OWNER, SourceOwner.class); SourceReceiver receiver = optStripeJsonModel( FIELD_REDIRECT, SourceRedirect.class); @SourceStatus String status = asSourceStatus(optString(jsonObject, FIELD_STATUS)); String typeRaw = optString(jsonObject, FIELD_TYPE); if (typeRaw == null) { StripeJsonUtils.jsonObjectToMap(jsonObject.optJSONObject(typeRaw)); StripeSourceTypeModel sourceTypeModel = MODELED_TYPES.contains(typeRaw) @Usage String usage = asUsage(optString(jsonObject, FIELD_USAGE));
Integer expMonth = optInteger(jsonObject, FIELD_EXP_MONTH); Integer expYear = optInteger(jsonObject, FIELD_EXP_YEAR); builder.addressCity(optString(jsonObject, FIELD_ADDRESS_CITY)); builder.addressLine1(optString(jsonObject, FIELD_ADDRESS_LINE1)); builder.addressLine1Check(optString(jsonObject, FIELD_ADDRESS_LINE1_CHECK)); builder.addressLine2(optString(jsonObject, FIELD_ADDRESS_LINE2)); builder.addressCountry(optString(jsonObject, FIELD_ADDRESS_COUNTRY)); builder.addressState(optString(jsonObject, FIELD_ADDRESS_STATE)); builder.addressZip(optString(jsonObject, FIELD_ADDRESS_ZIP)); builder.addressZipCheck(optString(jsonObject, FIELD_ADDRESS_ZIP_CHECK)); builder.brand(asCardBrand(optString(jsonObject, FIELD_BRAND))); builder.country(optCountryCode(jsonObject, FIELD_COUNTRY)); builder.customer(optString(jsonObject, FIELD_CUSTOMER)); builder.currency(optCurrency(jsonObject, FIELD_CURRENCY)); builder.cvcCheck(optString(jsonObject, FIELD_CVC_CHECK)); builder.funding(asFundingType(optString(jsonObject, FIELD_FUNDING))); builder.fingerprint(optString(jsonObject, FIELD_FINGERPRINT)); builder.id(optString(jsonObject, FIELD_ID)); builder.last4(optString(jsonObject, FIELD_LAST4)); builder.name(optString(jsonObject, FIELD_NAME)); builder.tokenizationMethod(optString(jsonObject, FIELD_TOKENIZATION_METHOD)); builder.metadata(optHash(jsonObject, FIELD_METADATA));
@Nullable public static Address fromJson(@Nullable JSONObject jsonObject) { if (jsonObject == null) { return null; } String city = optString(jsonObject, FIELD_CITY); String country = optString(jsonObject, FIELD_COUNTRY); String line1 = optString(jsonObject, FIELD_LINE_1); String line2 = optString(jsonObject, FIELD_LINE_2); String postalCode = optString(jsonObject, FIELD_POSTAL_CODE); String state = optString(jsonObject, FIELD_STATE); return new Address(city, country, line1, line2, postalCode, state); }
@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; } } }
try { Map<String, Object> mapObject = (Map<String, Object>) object; jsonArray.put(mapToJsonObject(mapObject)); } catch (ClassCastException classCastException) { jsonArray.put(listToJsonArray((List) object)); } else if (object instanceof Number || object instanceof Boolean) { jsonArray.put(object);
@Nullable public static Customer fromJson(JSONObject jsonObject) { String objectType = optString(jsonObject, FIELD_OBJECT); if (!VALUE_CUSTOMER.equals(objectType)) { return null; customer.mId = optString(jsonObject, FIELD_ID); customer.mDefaultSource = optString(jsonObject, FIELD_DEFAULT_SOURCE); customer.mShippingInformation = ShippingInformation.fromJson(jsonObject.optJSONObject(FIELD_SHIPPING)); JSONObject sources = jsonObject.optJSONObject(FIELD_SOURCES); if (sources != null && VALUE_LIST.equals(optString(sources, FIELD_OBJECT))) { customer.mHasMore = optBoolean(sources, FIELD_HAS_MORE); customer.mTotalCount = optInteger(sources, FIELD_TOTAL_COUNT); customer.mUrl = optString(sources, FIELD_URL); List<CustomerSource> sourceDataList = new ArrayList<>(); JSONArray dataArray = sources.optJSONArray(FIELD_DATA);
/** * 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); }
return null; final String tokenId = StripeJsonUtils.optString(jsonObject, FIELD_ID); final Long createdTimeStamp = StripeJsonUtils.optLong(jsonObject, FIELD_CREATED); final Boolean liveMode = StripeJsonUtils.optBoolean(jsonObject, FIELD_LIVEMODE); @TokenType final String tokenType = asTokenType(StripeJsonUtils.optString(jsonObject, FIELD_TYPE)); final Boolean used = StripeJsonUtils.optBoolean(jsonObject, FIELD_USED);
/** * 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) { } }
@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()); } }
JSONObject jsonObject = new JSONObject(); try { putStringIfNotNull(jsonObject, FIELD_ID, mId); jsonObject.put(FIELD_OBJECT, VALUE_SOURCE); jsonObject.put(FIELD_AMOUNT, mAmount); putStringIfNotNull(jsonObject, FIELD_CLIENT_SECRET, mClientSecret); putStripeJsonModelIfNotNull(jsonObject, FIELD_CODE_VERIFICATION, mCodeVerification); jsonObject.put(FIELD_CREATED, mCreated); putStringIfNotNull(jsonObject, FIELD_CURRENCY, mCurrency); putStringIfNotNull(jsonObject, FIELD_FLOW, mFlow); jsonObject.put(FIELD_LIVEMODE, mLiveMode); JSONObject metaDataObject = mapToJsonObject(mMetaData); if (metaDataObject != null) { jsonObject.put(FIELD_METADATA, metaDataObject); JSONObject sourceTypeJsonObject = mapToJsonObject(mSourceTypeData); putStripeJsonModelIfNotNull(jsonObject, FIELD_RECEIVER, mReceiver); putStripeJsonModelIfNotNull(jsonObject, FIELD_REDIRECT, mRedirect); putStringIfNotNull(jsonObject, FIELD_STATUS, mStatus); putStringIfNotNull(jsonObject, FIELD_TYPE, mTypeRaw); putStringIfNotNull(jsonObject, FIELD_USAGE, mUsage);
@Nullable static SourceCardData fromJson(@Nullable JSONObject jsonObject) { if (jsonObject == null) { return null; } SourceCardData cardData = new SourceCardData(); cardData.setAddressLine1Check(optString(jsonObject, FIELD_ADDRESS_LINE1_CHECK)) .setAddressZipCheck(optString(jsonObject, FIELD_ADDRESS_ZIP_CHECK)) .setBrand(Card.asCardBrand(optString(jsonObject, FIELD_BRAND))) .setCountry(optString(jsonObject, FIELD_COUNTRY)) .setCvcCheck(optString(jsonObject, FIELD_CVC_CHECK)) .setDynamicLast4(optString(jsonObject, FIELD_DYNAMIC_LAST4)) .setExpiryMonth(optInteger(jsonObject, FIELD_EXP_MONTH)) .setExpiryYear(optInteger(jsonObject, FIELD_EXP_YEAR)) .setFunding(Card.asFundingType(optString(jsonObject, FIELD_FUNDING))) .setLast4(optString(jsonObject, FIELD_LAST4)) .setThreeDSecureStatus(asThreeDSecureStatus(optString(jsonObject, FIELD_THREE_D_SECURE))) .setTokenizationMethod(optString(jsonObject, FIELD_TOKENIZATION_METHOD)); Map<String, Object> nonStandardFields = jsonObjectToMapWithoutKeys(jsonObject, cardData.mStandardFields); if (nonStandardFields != null) { cardData.setAdditionalFields(nonStandardFields); } return cardData; }
@Test public void listToJsonArray_forNull_returnsNull() { assertNull(StripeJsonUtils.listToJsonArray(null)); }
@Test public void jsonArrayToList_forSimpleList_returnsExpectedList() { List<Object> expectedList = new ArrayList<>(); expectedList.add(1); expectedList.add(2); expectedList.add(3); expectedList.add("a"); expectedList.add(true); expectedList.add("cde"); try { JSONArray testJsonArray = new JSONArray(SIMPLE_JSON_TEST_ARRAY); List<Object> convertedJsonArray = StripeJsonUtils.jsonArrayToList(testJsonArray); JsonTestUtils.assertListEquals(expectedList, convertedJsonArray); } catch (JSONException jsonException) { fail("Test data failure " + jsonException.getLocalizedMessage()); } }
/** * Util function for putting a String-String 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 putStringHashIfNotNull( @NonNull JSONObject jsonObject, @NonNull @Size(min = 1) String fieldName, @Nullable Map<String, String> value) { if (value == null) { return; } JSONObject jsonHash = stringHashToJsonObject(value); if (jsonHash == null) { return; } try { jsonObject.put(fieldName, jsonHash); } catch (JSONException ignored) { } }