aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Jaros <rjaros@finn.pl>2019-11-04 11:26:12 +0100
committerRobert Jaros <rjaros@finn.pl>2019-11-04 11:26:12 +0100
commite94718175bbe0436306d7c4c988d24d5c2196e82 (patch)
tree53ddf6f23c7234d1d00b34aa8ea7b34a3825e071
parentafb2bf0b4fb0446a6bc427e8ee31cbd56b23d052 (diff)
downloadkvision-e94718175bbe0436306d7c4c988d24d5c2196e82.tar.gz
kvision-e94718175bbe0436306d7c4c988d24d5c2196e82.tar.bz2
kvision-e94718175bbe0436306d7c4c988d24d5c2196e82.zip
Support for Date type serialization/deserialization in RestClient (#101)
-rw-r--r--src/main/kotlin/pl/treksoft/kvision/rest/RestClient.kt27
-rw-r--r--src/main/kotlin/pl/treksoft/kvision/types/Date.kt7
2 files changed, 29 insertions, 5 deletions
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<Date> {
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) {