aboutsummaryrefslogtreecommitdiff
path: root/taler-kotlin-common
diff options
context:
space:
mode:
authorTorsten Grote <t@grobox.de>2020-03-30 14:17:09 -0300
committerTorsten Grote <t@grobox.de>2020-03-30 14:17:09 -0300
commitdb0cf385fe707091f219cc61fb4e12b5b1fe64d0 (patch)
treec50aca48f996e0dec20315cde0ae4d278bdf0640 /taler-kotlin-common
parent1dbc22f1faba07331953d761097d5ad91ffff5ce (diff)
downloadtaler-android-db0cf385fe707091f219cc61fb4e12b5b1fe64d0.tar.gz
taler-android-db0cf385fe707091f219cc61fb4e12b5b1fe64d0.tar.bz2
taler-android-db0cf385fe707091f219cc61fb4e12b5b1fe64d0.zip
Fix amount serialization in PoS app
Diffstat (limited to 'taler-kotlin-common')
-rw-r--r--taler-kotlin-common/src/main/java/net/taler/common/Amount.kt33
1 files changed, 22 insertions, 11 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 49b699f..bd12a40 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
@@ -17,30 +17,24 @@
package net.taler.common
import android.annotation.SuppressLint
+import com.fasterxml.jackson.core.JsonGenerator
import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind.DeserializationContext
import com.fasterxml.jackson.databind.JsonMappingException
+import com.fasterxml.jackson.databind.SerializerProvider
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 org.json.JSONObject
import java.lang.Math.floorDiv
import kotlin.math.pow
import kotlin.math.roundToInt
-class AmountDeserializer : StdDeserializer<Amount>(Amount::class.java) {
- override fun deserialize(p: JsonParser, ctxt: DeserializationContext): Amount {
- val node = p.codec.readValue(p, String::class.java)
- try {
- return Amount.fromJSONString(node)
- } catch (e: AmountParserException) {
- throw JsonMappingException(p, "Error parsing Amount", e)
- }
- }
-}
-
class AmountParserException(msg: String? = null, cause: Throwable? = null) : Exception(msg, cause)
class AmountOverflowException(msg: String? = null, cause: Throwable? = null) : Exception(msg, cause)
+@JsonSerialize(using = AmountSerializer::class)
@JsonDeserialize(using = AmountDeserializer::class)
data class Amount(
/**
@@ -215,3 +209,20 @@ data class Amount(
}
}
+
+class AmountSerializer : StdSerializer<Amount>(Amount::class.java) {
+ override fun serialize(value: Amount, gen: JsonGenerator, provider: SerializerProvider) {
+ gen.writeString(value.toJSONString())
+ }
+}
+
+class AmountDeserializer : StdDeserializer<Amount>(Amount::class.java) {
+ override fun deserialize(p: JsonParser, ctxt: DeserializationContext): Amount {
+ val node = p.codec.readValue(p, String::class.java)
+ try {
+ return Amount.fromJSONString(node)
+ } catch (e: AmountParserException) {
+ throw JsonMappingException(p, "Error parsing Amount", e)
+ }
+ }
+}