diff options
author | Torsten Grote <t@grobox.de> | 2020-07-22 16:47:05 -0300 |
---|---|---|
committer | Torsten Grote <t@grobox.de> | 2020-07-22 16:47:05 -0300 |
commit | b9d7d6edb7cc12e5238168c4446a28c0ae98c729 (patch) | |
tree | d462a3c4487a017956e684028ecc5ae5b69919a1 /taler-kotlin-common/src | |
parent | 4bd0b7a75344b08cf5f84c360a28fc3f575b8501 (diff) | |
download | taler-android-b9d7d6edb7cc12e5238168c4446a28c0ae98c729.tar.gz taler-android-b9d7d6edb7cc12e5238168c4446a28c0ae98c729.tar.bz2 taler-android-b9d7d6edb7cc12e5238168c4446a28c0ae98c729.zip |
[common] also support multi-platform serialization/deserialization
Diffstat (limited to 'taler-kotlin-common/src')
-rw-r--r-- | taler-kotlin-common/src/main/java/net/taler/common/Amount.kt | 17 | ||||
-rw-r--r-- | taler-kotlin-common/src/main/java/net/taler/common/ContractTerms.kt | 23 |
2 files changed, 34 insertions, 6 deletions
diff --git a/taler-kotlin-common/src/main/java/net/taler/common/Amount.kt b/taler-kotlin-common/src/main/java/net/taler/common/Amount.kt index 76cd294..992f93b 100644 --- a/taler-kotlin-common/src/main/java/net/taler/common/Amount.kt +++ b/taler-kotlin-common/src/main/java/net/taler/common/Amount.kt @@ -26,6 +26,11 @@ import com.fasterxml.jackson.databind.annotation.JsonDeserialize import com.fasterxml.jackson.databind.annotation.JsonSerialize import com.fasterxml.jackson.databind.deser.std.StdDeserializer import com.fasterxml.jackson.databind.ser.std.StdSerializer +import kotlinx.serialization.Decoder +import kotlinx.serialization.Encoder +import kotlinx.serialization.KSerializer +import kotlinx.serialization.Serializable +import kotlinx.serialization.Serializer import org.json.JSONObject import java.lang.Math.floorDiv import kotlin.math.pow @@ -34,6 +39,7 @@ import kotlin.math.roundToInt class AmountParserException(msg: String? = null, cause: Throwable? = null) : Exception(msg, cause) class AmountOverflowException(msg: String? = null, cause: Throwable? = null) : Exception(msg, cause) +@Serializable(with = KotlinXAmountSerializer::class) @JsonSerialize(using = AmountSerializer::class) @JsonDeserialize(using = AmountDeserializer::class) data class Amount( @@ -211,6 +217,17 @@ data class Amount( } +@Serializer(forClass = Amount::class) +object KotlinXAmountSerializer: KSerializer<Amount> { + override fun serialize(encoder: Encoder, value: Amount) { + encoder.encodeString(value.toJSONString()) + } + + override fun deserialize(decoder: Decoder): Amount { + return Amount.fromJSONString(decoder.decodeString()) + } +} + class AmountSerializer : StdSerializer<Amount>(Amount::class.java) { override fun serialize(value: Amount, gen: JsonGenerator, provider: SerializerProvider) { gen.writeString(value.toJSONString()) diff --git a/taler-kotlin-common/src/main/java/net/taler/common/ContractTerms.kt b/taler-kotlin-common/src/main/java/net/taler/common/ContractTerms.kt index 8b8e02d..c07127a 100644 --- a/taler-kotlin-common/src/main/java/net/taler/common/ContractTerms.kt +++ b/taler-kotlin-common/src/main/java/net/taler/common/ContractTerms.kt @@ -23,13 +23,20 @@ import com.fasterxml.jackson.annotation.JsonInclude import com.fasterxml.jackson.annotation.JsonInclude.Include.NON_EMPTY import com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL import com.fasterxml.jackson.annotation.JsonProperty +import kotlinx.serialization.SerialName +import kotlinx.serialization.Serializable import net.taler.common.TalerUtils.getLocalizedString +@Serializable @JsonIgnoreProperties(ignoreUnknown = true) data class ContractTerms( val summary: String, - val products: List<ContractProduct>, - val amount: Amount + @SerialName("summary_i18n") + val summaryI18n: Map<String, String>? = null, + val amount: Amount, + @SerialName("fulfillment_url") + val fulfillmentUrl: String, + val products: List<ContractProduct> ) @JsonInclude(NON_NULL) @@ -52,13 +59,17 @@ abstract class Product { get() = getLocalizedString(descriptionI18n, description) } +@Serializable data class ContractProduct( - override val productId: String?, + @SerialName("product_id") + override val productId: String? = null, override val description: String, - override val descriptionI18n: Map<String, String>?, + @SerialName("description_i18n") + override val descriptionI18n: Map<String, String>? = null, override val price: Amount, - override val location: String?, - override val image: String?, + @SerialName("delivery_location") + override val location: String? = null, + override val image: String? = null, val quantity: Int ) : Product() { @get:JsonIgnore |