Skip to content

Commit

Permalink
Merge pull request #1815 from Adyen/chore/json-opt-functions
Browse files Browse the repository at this point in the history
Replace JSONObject's opt functions with nullable extensions
  • Loading branch information
jreij authored Oct 28, 2024
2 parents c45a6ee + feeffe5 commit 578cb23
Show file tree
Hide file tree
Showing 37 changed files with 386 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ data class ErrorResponseBody(
override fun deserialize(jsonObject: JSONObject): ErrorResponseBody {
return try {
ErrorResponseBody(
status = jsonObject.optInt(STATUS),
errorCode = jsonObject.optString(ERROR_CODE),
message = jsonObject.optString(MESSAGE),
errorType = jsonObject.optString(ERROR_TYPE),
pspReference = jsonObject.optString(PSP_REFERENCE),
status = jsonObject.getIntOrNull(STATUS),
errorCode = jsonObject.getStringOrNull(ERROR_CODE),
message = jsonObject.getStringOrNull(MESSAGE),
errorType = jsonObject.getStringOrNull(ERROR_TYPE),
pspReference = jsonObject.getStringOrNull(PSP_REFERENCE),
)
} catch (e: JSONException) {
throw ModelSerializationException(ErrorResponseBody::class.java, e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ package com.adyen.checkout.components.core
import com.adyen.checkout.components.core.internal.util.EMPTY_VALUE
import com.adyen.checkout.core.exception.ModelSerializationException
import com.adyen.checkout.core.internal.data.model.ModelObject
import com.adyen.checkout.core.internal.data.model.getLongOrNull
import com.adyen.checkout.core.internal.data.model.getStringOrNull
import kotlinx.parcelize.Parcelize
import org.json.JSONException
Expand Down Expand Up @@ -41,7 +42,7 @@ data class Amount(
override fun deserialize(jsonObject: JSONObject): Amount {
return Amount(
currency = jsonObject.getStringOrNull(CURRENCY),
value = jsonObject.optLong(VALUE, EMPTY_VALUE),
value = jsonObject.getLongOrNull(VALUE) ?: EMPTY_VALUE,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ package com.adyen.checkout.components.core

import com.adyen.checkout.core.exception.ModelSerializationException
import com.adyen.checkout.core.internal.data.model.ModelObject
import com.adyen.checkout.core.internal.data.model.getIntOrNull
import kotlinx.parcelize.Parcelize
import org.json.JSONException
import org.json.JSONObject
Expand Down Expand Up @@ -41,7 +42,7 @@ data class Installments(
return try {
Installments(
plan = jsonObject.getString(PLAN),
value = jsonObject.optInt(VALUE, 1)
value = jsonObject.getIntOrNull(VALUE) ?: 1,
)
} catch (e: JSONException) {
throw ModelSerializationException(Installments::class.java, e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package com.adyen.checkout.components.core

import com.adyen.checkout.core.exception.ModelSerializationException
import com.adyen.checkout.core.internal.data.model.ModelObject
import com.adyen.checkout.core.internal.data.model.getBooleanOrNull
import com.adyen.checkout.core.internal.data.model.getStringOrNull
import kotlinx.parcelize.Parcelize
import org.json.JSONException
Expand Down Expand Up @@ -44,7 +45,7 @@ data class Issuer(
return Issuer(
id = jsonObject.getStringOrNull(ID),
name = jsonObject.getStringOrNull(NAME),
isDisabled = jsonObject.optBoolean(DISABLED, false),
isDisabled = jsonObject.getBooleanOrNull(DISABLED) ?: false,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package com.adyen.checkout.components.core

import com.adyen.checkout.core.exception.ModelSerializationException
import com.adyen.checkout.core.internal.data.model.ModelObject
import com.adyen.checkout.core.internal.data.model.getStringOrNull
import kotlinx.parcelize.Parcelize
import org.json.JSONException
import org.json.JSONObject
Expand Down Expand Up @@ -38,8 +39,8 @@ data class OrderRequest constructor(

override fun deserialize(jsonObject: JSONObject): OrderRequest {
return OrderRequest(
pspReference = jsonObject.optString(PSP_REFERENCE, ""),
orderData = jsonObject.optString(ORDER_DATA, "")
pspReference = jsonObject.getStringOrNull(PSP_REFERENCE).orEmpty(),
orderData = jsonObject.getStringOrNull(ORDER_DATA).orEmpty(),
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ package com.adyen.checkout.components.core
import com.adyen.checkout.core.exception.ModelSerializationException
import com.adyen.checkout.core.internal.data.model.ModelObject
import com.adyen.checkout.core.internal.data.model.ModelUtils
import com.adyen.checkout.core.internal.data.model.getStringOrNull
import kotlinx.parcelize.Parcelize
import org.json.JSONException
import org.json.JSONObject
Expand Down Expand Up @@ -38,7 +39,7 @@ data class OrderResponse(
putOpt(AMOUNT, ModelUtils.serializeOpt(modelObject.amount, Amount.SERIALIZER))
putOpt(
REMAINING_AMOUNT,
ModelUtils.serializeOpt(modelObject.remainingAmount, Amount.SERIALIZER)
ModelUtils.serializeOpt(modelObject.remainingAmount, Amount.SERIALIZER),
)
} catch (e: JSONException) {
throw ModelSerializationException(OrderResponse::class.java, e)
Expand All @@ -48,12 +49,12 @@ data class OrderResponse(

override fun deserialize(jsonObject: JSONObject): OrderResponse {
return OrderResponse(
pspReference = jsonObject.optString(PSP_REFERENCE, ""),
orderData = jsonObject.optString(ORDER_DATA, ""),
pspReference = jsonObject.getStringOrNull(PSP_REFERENCE).orEmpty(),
orderData = jsonObject.getStringOrNull(ORDER_DATA).orEmpty(),
amount = ModelUtils.deserializeOpt(jsonObject.optJSONObject(AMOUNT), Amount.SERIALIZER),
remainingAmount = ModelUtils.deserializeOpt(
jsonObject.optJSONObject(REMAINING_AMOUNT),
Amount.SERIALIZER
Amount.SERIALIZER,
),
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import com.adyen.checkout.core.exception.ModelSerializationException
import com.adyen.checkout.core.internal.data.model.ModelObject
import com.adyen.checkout.core.internal.data.model.ModelUtils.deserializeOpt
import com.adyen.checkout.core.internal.data.model.ModelUtils.serializeOpt
import com.adyen.checkout.core.internal.data.model.getBooleanOrNull
import com.adyen.checkout.core.internal.data.model.getStringOrNull
import kotlinx.parcelize.Parcelize
import org.json.JSONException
import org.json.JSONObject
Expand Down Expand Up @@ -89,17 +91,17 @@ data class PaymentComponentData<PaymentMethodDetailsT : PaymentMethodDetails>(
),
order = deserializeOpt(jsonObject.optJSONObject(ORDER), OrderRequest.SERIALIZER),
amount = deserializeOpt(jsonObject.optJSONObject(AMOUNT), Amount.SERIALIZER),
storePaymentMethod = jsonObject.optBoolean(STORE_PAYMENT_METHOD),
shopperReference = jsonObject.optString(SHOPPER_REFERENCE),
storePaymentMethod = jsonObject.getBooleanOrNull(STORE_PAYMENT_METHOD),
shopperReference = jsonObject.getStringOrNull(SHOPPER_REFERENCE),
billingAddress = deserializeOpt(jsonObject.optJSONObject(BILLING_ADDRESS), Address.SERIALIZER),
deliveryAddress = deserializeOpt(jsonObject.optJSONObject(DELIVERY_ADDRESS), Address.SERIALIZER),
shopperName = deserializeOpt(jsonObject.optJSONObject(SHOPPER_NAME), ShopperName.SERIALIZER),
telephoneNumber = jsonObject.optString(TELEPHONE_NUMBER),
shopperEmail = jsonObject.optString(SHOPPER_EMAIL),
dateOfBirth = jsonObject.optString(DATE_OF_BIRTH),
socialSecurityNumber = jsonObject.optString(SOCIAL_SECURITY_NUMBER),
telephoneNumber = jsonObject.getStringOrNull(TELEPHONE_NUMBER),
shopperEmail = jsonObject.getStringOrNull(SHOPPER_EMAIL),
dateOfBirth = jsonObject.getStringOrNull(DATE_OF_BIRTH),
socialSecurityNumber = jsonObject.getStringOrNull(SOCIAL_SECURITY_NUMBER),
installments = deserializeOpt(jsonObject.optJSONObject(INSTALLMENTS), Installments.SERIALIZER),
supportNativeRedirect = jsonObject.optBoolean(SUPPORT_NATIVE_REDIRECT),
supportNativeRedirect = jsonObject.getBooleanOrNull(SUPPORT_NATIVE_REDIRECT),
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package com.adyen.checkout.components.core.action

import com.adyen.checkout.core.exception.CheckoutException
import com.adyen.checkout.core.internal.data.model.ModelObject
import com.adyen.checkout.core.internal.data.model.getStringOrNull
import org.json.JSONObject

/**
Expand Down Expand Up @@ -41,10 +42,7 @@ abstract class Action : ModelObject() {
}

override fun deserialize(jsonObject: JSONObject): Action {
val actionType = jsonObject.optString(TYPE)
if (actionType.isEmpty()) {
throw CheckoutException("Action type not found")
}
val actionType = jsonObject.getStringOrNull(TYPE) ?: throw CheckoutException("Action type not found")
val serializer = getChildSerializer(actionType)
return serializer.deserialize(jsonObject)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
package com.adyen.checkout.components.core.action

import com.adyen.checkout.core.exception.ModelSerializationException
import com.adyen.checkout.core.internal.data.model.getBooleanOrNull
import kotlinx.parcelize.Parcelize
import org.json.JSONException
import org.json.JSONObject
Expand Down Expand Up @@ -41,7 +42,7 @@ data class TwintSdkData(
return try {
TwintSdkData(
token = jsonObject.getString(TOKEN),
isStored = jsonObject.optBoolean(IS_STORED),
isStored = jsonObject.getBooleanOrNull(IS_STORED) ?: false,
)
} catch (e: JSONException) {
throw ModelSerializationException(TwintSdkData::class.java, e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ package com.adyen.checkout.components.core.internal.data.model

import com.adyen.checkout.core.exception.ModelSerializationException
import com.adyen.checkout.core.internal.data.model.ModelObject
import com.adyen.checkout.core.internal.data.model.getStringOrNull
import kotlinx.parcelize.Parcelize
import org.json.JSONException
import org.json.JSONObject
Expand Down Expand Up @@ -38,7 +39,7 @@ internal data class PublicKeyResponse(
override fun deserialize(jsonObject: JSONObject): PublicKeyResponse {
return try {
PublicKeyResponse(
publicKey = jsonObject.optString(PUBLIC_KEY)
publicKey = jsonObject.getStringOrNull(PUBLIC_KEY).orEmpty(),
)
} catch (e: JSONException) {
throw ModelSerializationException(PublicKeyResponse::class.java, e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package com.adyen.checkout.googlepay

import com.adyen.checkout.core.exception.ModelSerializationException
import com.adyen.checkout.core.internal.data.model.ModelObject
import com.adyen.checkout.core.internal.data.model.getBooleanOrNull
import com.adyen.checkout.core.internal.data.model.getStringOrNull
import kotlinx.parcelize.Parcelize
import org.json.JSONException
Expand Down Expand Up @@ -49,7 +50,7 @@ data class BillingAddressParameters(

override fun deserialize(jsonObject: JSONObject) = BillingAddressParameters(
format = jsonObject.getStringOrNull(FORMAT),
isPhoneNumberRequired = jsonObject.optBoolean(PHONE_NUMBER_REQUIRED),
isPhoneNumberRequired = jsonObject.getBooleanOrNull(PHONE_NUMBER_REQUIRED) ?: false,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.adyen.checkout.core.exception.ModelSerializationException
import com.adyen.checkout.core.internal.data.model.JsonUtils.parseOptStringList
import com.adyen.checkout.core.internal.data.model.JsonUtils.serializeOptStringList
import com.adyen.checkout.core.internal.data.model.ModelObject
import com.adyen.checkout.core.internal.data.model.getBooleanOrNull
import kotlinx.parcelize.Parcelize
import org.json.JSONException
import org.json.JSONObject
Expand Down Expand Up @@ -50,7 +51,7 @@ data class ShippingAddressParameters(

override fun deserialize(jsonObject: JSONObject) = ShippingAddressParameters(
allowedCountryCodes = parseOptStringList(jsonObject.optJSONArray(ALLOWED_COUNTRY_CODES)),
isPhoneNumberRequired = jsonObject.optBoolean(PHONE_NUMBER_REQUIRED),
isPhoneNumberRequired = jsonObject.getBooleanOrNull(PHONE_NUMBER_REQUIRED) ?: false,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.adyen.checkout.core.internal.data.model.JsonUtils.serializeOptStringL
import com.adyen.checkout.core.internal.data.model.ModelObject
import com.adyen.checkout.core.internal.data.model.ModelUtils.deserializeOpt
import com.adyen.checkout.core.internal.data.model.ModelUtils.serializeOpt
import com.adyen.checkout.core.internal.data.model.getBooleanOrNull
import com.adyen.checkout.googlepay.BillingAddressParameters
import kotlinx.parcelize.Parcelize
import org.json.JSONException
Expand Down Expand Up @@ -51,7 +52,7 @@ internal data class CardParameters(
putOpt(BILLING_ADDRESS_REQUIRED, modelObject.isBillingAddressRequired)
putOpt(
BILLING_ADDRESS_PARAMETERS,
serializeOpt(modelObject.billingAddressParameters, BillingAddressParameters.SERIALIZER)
serializeOpt(modelObject.billingAddressParameters, BillingAddressParameters.SERIALIZER),
)
}
} catch (e: JSONException) {
Expand All @@ -62,13 +63,13 @@ internal data class CardParameters(
override fun deserialize(jsonObject: JSONObject) = CardParameters(
allowedAuthMethods = parseOptStringList(jsonObject.optJSONArray(ALLOWED_AUTH_METHODS)),
allowedCardNetworks = parseOptStringList(jsonObject.optJSONArray(ALLOWED_CARD_NETWORKS)),
isAllowPrepaidCards = jsonObject.optBoolean(ALLOW_PREPAID_CARDS),
isAllowCreditCards = jsonObject.optBoolean(ALLOW_CREDIT_CARDS),
isAssuranceDetailsRequired = jsonObject.optBoolean(ASSURANCE_DETAILS_REQUIRED),
isBillingAddressRequired = jsonObject.optBoolean(BILLING_ADDRESS_REQUIRED),
isAllowPrepaidCards = jsonObject.getBooleanOrNull(ALLOW_PREPAID_CARDS) ?: false,
isAllowCreditCards = jsonObject.getBooleanOrNull(ALLOW_CREDIT_CARDS),
isAssuranceDetailsRequired = jsonObject.getBooleanOrNull(ASSURANCE_DETAILS_REQUIRED),
isBillingAddressRequired = jsonObject.getBooleanOrNull(BILLING_ADDRESS_REQUIRED) ?: false,
billingAddressParameters = deserializeOpt(
jsonObject.optJSONObject(BILLING_ADDRESS_PARAMETERS),
BillingAddressParameters.SERIALIZER
BillingAddressParameters.SERIALIZER,
),
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import com.adyen.checkout.core.exception.ModelSerializationException
import com.adyen.checkout.core.internal.data.model.ModelObject
import com.adyen.checkout.core.internal.data.model.ModelUtils.deserializeOptList
import com.adyen.checkout.core.internal.data.model.ModelUtils.serializeOptList
import com.adyen.checkout.core.internal.data.model.getBooleanOrNull
import com.adyen.checkout.core.internal.data.model.getIntOrNull
import kotlinx.parcelize.Parcelize
import org.json.JSONException
import org.json.JSONObject
Expand Down Expand Up @@ -38,7 +40,7 @@ internal data class IsReadyToPayRequestModel(
putOpt(API_VERSION_MINOR, modelObject.apiVersionMinor)
putOpt(
ALLOWED_PAYMENT_METHODS,
serializeOptList(modelObject.allowedPaymentMethods, GooglePayPaymentMethodModel.SERIALIZER)
serializeOptList(modelObject.allowedPaymentMethods, GooglePayPaymentMethodModel.SERIALIZER),
)
putOpt(EXISTING_PAYMENT_METHOD_REQUIRED, modelObject.isExistingPaymentMethodRequired)
}
Expand All @@ -48,13 +50,14 @@ internal data class IsReadyToPayRequestModel(
}

override fun deserialize(jsonObject: JSONObject) = IsReadyToPayRequestModel(
apiVersion = jsonObject.optInt(API_VERSION),
apiVersionMinor = jsonObject.optInt(API_VERSION_MINOR),
apiVersion = jsonObject.getIntOrNull(API_VERSION) ?: 0,
apiVersionMinor = jsonObject.getIntOrNull(API_VERSION_MINOR) ?: 0,
allowedPaymentMethods = deserializeOptList(
jsonObject.optJSONArray(ALLOWED_PAYMENT_METHODS),
GooglePayPaymentMethodModel.SERIALIZER
GooglePayPaymentMethodModel.SERIALIZER,
),
isExistingPaymentMethodRequired = jsonObject.optBoolean(EXISTING_PAYMENT_METHOD_REQUIRED)
isExistingPaymentMethodRequired = jsonObject.getBooleanOrNull(EXISTING_PAYMENT_METHOD_REQUIRED)
?: false,
)
}
}
Expand Down
Loading

0 comments on commit 578cb23

Please sign in to comment.