aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--taler-kotlin-android/src/main/java/net/taler/common/TimestampMixin.kt12
-rw-r--r--taler-kotlin-android/src/test/java/net/taler/common/ContractTermsTest.kt5
-rw-r--r--taler-kotlin-common/src/commonMain/kotlin/net/taler/common/Time.kt29
-rw-r--r--taler-kotlin-common/src/commonTest/kotlin/net/taler/common/TimeTest.kt49
4 files changed, 92 insertions, 3 deletions
diff --git a/taler-kotlin-android/src/main/java/net/taler/common/TimestampMixin.kt b/taler-kotlin-android/src/main/java/net/taler/common/TimestampMixin.kt
index 28dbe7f..6c1bebf 100644
--- a/taler-kotlin-android/src/main/java/net/taler/common/TimestampMixin.kt
+++ b/taler-kotlin-android/src/main/java/net/taler/common/TimestampMixin.kt
@@ -17,11 +17,23 @@
package net.taler.common
import com.fasterxml.jackson.annotation.JsonProperty
+import com.fasterxml.jackson.core.JsonParser
+import com.fasterxml.jackson.databind.DeserializationContext
+import com.fasterxml.jackson.databind.annotation.JsonDeserialize
+import com.fasterxml.jackson.databind.deser.std.StdDeserializer
/**
* Used to support Jackson serialization along with KotlinX.
*/
abstract class TimestampMixin(
+ @get:JsonDeserialize(using = NeverDeserializer::class)
@get:JsonProperty("t_ms")
val ms: Long
)
+
+class NeverDeserializer : StdDeserializer<Long>(Long::class.java) {
+ override fun deserialize(p: JsonParser, ctxt: DeserializationContext): Long {
+ return if (p.text == "never") -1
+ else p.longValue
+ }
+}
diff --git a/taler-kotlin-android/src/test/java/net/taler/common/ContractTermsTest.kt b/taler-kotlin-android/src/test/java/net/taler/common/ContractTermsTest.kt
index 79a7598..077ff51 100644
--- a/taler-kotlin-android/src/test/java/net/taler/common/ContractTermsTest.kt
+++ b/taler-kotlin-android/src/test/java/net/taler/common/ContractTermsTest.kt
@@ -20,6 +20,7 @@ import com.fasterxml.jackson.databind.DeserializationFeature
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.KotlinModule
import com.fasterxml.jackson.module.kotlin.readValue
+import net.taler.common.Timestamp.Companion.NEVER
import org.junit.Assert.assertEquals
import org.junit.Test
@@ -29,6 +30,7 @@ class ContractTermsTest {
.registerModule(KotlinModule())
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.addMixIn(Amount::class.java, AmountMixin::class.java)
+ .addMixIn(Timestamp::class.java, TimestampMixin::class.java)
@Test
fun test() {
@@ -40,7 +42,7 @@ class ContractTermsTest {
},
"fulfillment_url":"https://shop.test.taler.net/essay/1._The_Free_Software_Definition",
"summary":"Essay: 1. The Free Software Definition",
- "refund_deadline":{"t_ms":1596128414000},
+ "refund_deadline":{"t_ms":"never"},
"wire_transfer_deadline":{"t_ms":1596128564000},
"products":[],
"h_wire":"KV40K023N8EC1F5100TYNS23C4XN68Y1Z3PTJSWFGTMCNYD54KT4S791V2VQ91SZANN86VDAA369M4VEZ0KR6DN71EVRRZA71K681M0",
@@ -69,6 +71,7 @@ class ContractTermsTest {
""".trimIndent()
val contractTerms: ContractTerms = mapper.readValue(json)
assertEquals("Essay: 1. The Free Software Definition", contractTerms.summary)
+ assertEquals(Timestamp(NEVER), contractTerms.refundDeadline)
}
}
diff --git a/taler-kotlin-common/src/commonMain/kotlin/net/taler/common/Time.kt b/taler-kotlin-common/src/commonMain/kotlin/net/taler/common/Time.kt
index 962e004..37b6606 100644
--- a/taler-kotlin-common/src/commonMain/kotlin/net/taler/common/Time.kt
+++ b/taler-kotlin-common/src/commonMain/kotlin/net/taler/common/Time.kt
@@ -18,6 +18,12 @@ package net.taler.common
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
+import kotlinx.serialization.builtins.serializer
+import kotlinx.serialization.json.JsonElement
+import kotlinx.serialization.json.JsonPrimitive
+import kotlinx.serialization.json.JsonTransformingSerializer
+import kotlinx.serialization.json.contentOrNull
+import kotlinx.serialization.json.longOrNull
import net.taler.common.Duration.Companion.FOREVER
import kotlin.math.max
@@ -26,11 +32,12 @@ expect fun nowMillis(): Long
@Serializable
data class Timestamp(
@SerialName("t_ms")
+ @Serializable(NeverSerializer::class)
val ms: Long
) : Comparable<Timestamp> {
companion object {
- const val NEVER: Long = -1 // TODO or UINT64_MAX?
+ const val NEVER: Long = -1
fun now(): Timestamp = Timestamp(nowMillis())
}
@@ -73,9 +80,27 @@ data class Duration(
* Duration in milliseconds.
*/
@SerialName("d_ms")
+ @Serializable(ForeverSerializer::class)
val ms: Long
) {
companion object {
- const val FOREVER: Long = -1 // TODO or UINT64_MAX?
+ const val FOREVER: Long = -1
}
}
+
+abstract class MinusOneSerializer(private val keyword: String) :
+ JsonTransformingSerializer<Long>(Long.serializer(), keyword) {
+
+ override fun readTransform(element: JsonElement): JsonElement {
+ return if (element.contentOrNull == keyword) return JsonPrimitive(-1)
+ else super.readTransform(element)
+ }
+
+ override fun writeTransform(element: JsonElement): JsonElement {
+ return if (element.longOrNull == -1L) return JsonPrimitive(keyword)
+ else element
+ }
+}
+
+object NeverSerializer : MinusOneSerializer("never")
+object ForeverSerializer : MinusOneSerializer("forever")
diff --git a/taler-kotlin-common/src/commonTest/kotlin/net/taler/common/TimeTest.kt b/taler-kotlin-common/src/commonTest/kotlin/net/taler/common/TimeTest.kt
new file mode 100644
index 0000000..3ee0a99
--- /dev/null
+++ b/taler-kotlin-common/src/commonTest/kotlin/net/taler/common/TimeTest.kt
@@ -0,0 +1,49 @@
+/*
+ * This file is part of GNU Taler
+ * (C) 2020 Taler Systems S.A.
+ *
+ * GNU Taler is free software; you can redistribute it and/or modify it under the
+ * terms of the GNU General Public License as published by the Free Software
+ * Foundation; either version 3, or (at your option) any later version.
+ *
+ * GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+ * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
+ */
+
+package net.taler.common
+
+import kotlinx.serialization.UnstableDefault
+import kotlinx.serialization.json.Json.Default.parse
+import kotlinx.serialization.json.Json.Default.stringify
+import net.taler.common.Timestamp.Companion.NEVER
+import kotlin.random.Random
+import kotlin.test.Test
+import kotlin.test.assertEquals
+
+// TODO test other functionality of Timestamp and Duration
+@UnstableDefault
+class TimeTest {
+
+ @Test
+ fun testSerialize() {
+ for (i in 0 until 42) {
+ val t = Random.nextLong()
+ assertEquals("""{"t_ms":$t}""", stringify(Timestamp.serializer(), Timestamp(t)))
+ }
+ assertEquals("""{"t_ms":"never"}""", stringify(Timestamp.serializer(), Timestamp(NEVER)))
+ }
+
+ @Test
+ fun testDeserialize() {
+ for (i in 0 until 42) {
+ val t = Random.nextLong()
+ assertEquals(Timestamp(t), parse(Timestamp.serializer(), """{ "t_ms": $t }"""))
+ }
+ assertEquals(Timestamp(NEVER), parse(Timestamp.serializer(), """{ "t_ms": "never" }"""))
+ }
+
+}