From e94718175bbe0436306d7c4c988d24d5c2196e82 Mon Sep 17 00:00:00 2001 From: Robert Jaros Date: Mon, 4 Nov 2019 11:26:12 +0100 Subject: Support for Date type serialization/deserialization in RestClient (#101) --- .../kotlin/pl/treksoft/kvision/rest/RestClient.kt | 27 ++++++++++++++++++---- src/main/kotlin/pl/treksoft/kvision/types/Date.kt | 7 +++++- 2 files changed, 29 insertions(+), 5 deletions(-) (limited to 'src/main/kotlin/pl') diff --git a/src/main/kotlin/pl/treksoft/kvision/rest/RestClient.kt b/src/main/kotlin/pl/treksoft/kvision/rest/RestClient.kt index 49222298..b2aae632 100644 --- a/src/main/kotlin/pl/treksoft/kvision/rest/RestClient.kt +++ b/src/main/kotlin/pl/treksoft/kvision/rest/RestClient.kt @@ -25,12 +25,15 @@ import kotlinx.serialization.DeserializationStrategy import kotlinx.serialization.DynamicObjectParser import kotlinx.serialization.ImplicitReflectionSerializer import kotlinx.serialization.SerializationStrategy +import kotlinx.serialization.modules.serializersModuleOf import kotlinx.serialization.serializer import pl.treksoft.jquery.JQueryAjaxSettings import pl.treksoft.jquery.JQueryXHR import pl.treksoft.jquery.jQuery +import pl.treksoft.kvision.types.DateSerializer import pl.treksoft.kvision.utils.JSON.toObj import pl.treksoft.kvision.utils.obj +import kotlin.js.Date import kotlin.js.Promise import kotlin.js.JSON as NativeJSON @@ -103,7 +106,10 @@ open class RestClient { } else { result } - DynamicObjectParser().parse(transformed, deserializer) + DynamicObjectParser(context = serializersModuleOf(Date::class, DateSerializer)).parse( + transformed, + deserializer + ) } } @@ -157,7 +163,10 @@ open class RestClient { } else { result } - DynamicObjectParser().parse(transformed, deserializer) + DynamicObjectParser(context = serializersModuleOf(Date::class, DateSerializer)).parse( + transformed, + deserializer + ) } } @@ -381,7 +390,12 @@ open class RestClient { } else { result.data } - Response(DynamicObjectParser().parse(transformed, deserializer), result.textStatus, result.jqXHR) + Response( + DynamicObjectParser(context = serializersModuleOf(Date::class, DateSerializer)).parse( + transformed, + deserializer + ), result.textStatus, result.jqXHR + ) } } @@ -441,7 +455,12 @@ open class RestClient { } else { result.data } - Response(DynamicObjectParser().parse(transformed, deserializer), result.textStatus, result.jqXHR) + Response( + DynamicObjectParser(context = serializersModuleOf(Date::class, DateSerializer)).parse( + transformed, + deserializer + ), result.textStatus, result.jqXHR + ) } } diff --git a/src/main/kotlin/pl/treksoft/kvision/types/Date.kt b/src/main/kotlin/pl/treksoft/kvision/types/Date.kt index 4af141f7..83d20ea9 100644 --- a/src/main/kotlin/pl/treksoft/kvision/types/Date.kt +++ b/src/main/kotlin/pl/treksoft/kvision/types/Date.kt @@ -69,7 +69,12 @@ object DateSerializer : KSerializer { override val descriptor: SerialDescriptor = SerialClassDescImpl("kotlin.js.Date") override fun deserialize(decoder: Decoder): Date { - return decoder.decodeString().toDateF() + val str = decoder.decodeString() + return if (str.length == 10) { + "$str 00:00:00".toDateF() + } else { + str.toDateF() + } } override fun serialize(encoder: Encoder, obj: Date) { -- cgit