From 8beb2bcc4806a69dc99acdd706f7de94bbe35955 Mon Sep 17 00:00:00 2001 From: Robert Jaros Date: Wed, 13 Feb 2019 13:42:22 +0100 Subject: Unification of @Serializable -> JSON functions --- .../main/kotlin/pl/treksoft/kvision/remote/CallAgent.kt | 11 +++-------- src/main/kotlin/pl/treksoft/kvision/utils/JSON.kt | 16 ++++++++++++++-- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/remote/CallAgent.kt b/kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/remote/CallAgent.kt index 1ec5610a..e15e43cb 100644 --- a/kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/remote/CallAgent.kt +++ b/kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/remote/CallAgent.kt @@ -25,18 +25,15 @@ import kotlinx.serialization.DeserializationStrategy import kotlinx.serialization.DynamicObjectParser import kotlinx.serialization.ImplicitReflectionSerializer import kotlinx.serialization.SerializationStrategy -import kotlinx.serialization.json.Json import kotlinx.serialization.serializer import kotlinx.serialization.stringify import pl.treksoft.jquery.JQueryAjaxSettings import pl.treksoft.jquery.JQueryXHR import pl.treksoft.jquery.jQuery import pl.treksoft.kvision.utils.JSON +import pl.treksoft.kvision.utils.JSON.toObj import pl.treksoft.kvision.utils.obj import kotlin.js.Promise -import kotlin.js.then -import kotlin.js.undefined -import kotlin.js.JSON as NativeJSON /** * HTTP status unauthorized (401). @@ -193,8 +190,7 @@ open class CallAgent { contentType: String = "application/json", beforeSend: ((JQueryXHR, JQueryAjaxSettings) -> Boolean)? = null ): Promise { - val dynamicData = NativeJSON.parse(Json.stringify(serializer, data)) - return remoteCall(url, dynamicData, method, contentType, beforeSend) + return remoteCall(url, data.toObj(serializer), method, contentType, beforeSend) } @@ -220,8 +216,7 @@ open class CallAgent { beforeSend: ((JQueryXHR, JQueryAjaxSettings) -> Boolean)? = null, transform: ((dynamic) -> dynamic)? = null ): Promise { - val dynamicData = NativeJSON.parse(Json.stringify(serializer, data)) - return remoteCall(url, dynamicData, method, contentType, beforeSend).then { result: dynamic -> + return remoteCall(url, data.toObj(serializer), method, contentType, beforeSend).then { result: dynamic -> val transformed = if (transform != null) { transform(result) } else { diff --git a/src/main/kotlin/pl/treksoft/kvision/utils/JSON.kt b/src/main/kotlin/pl/treksoft/kvision/utils/JSON.kt index 4dc1ccd9..e30935ab 100644 --- a/src/main/kotlin/pl/treksoft/kvision/utils/JSON.kt +++ b/src/main/kotlin/pl/treksoft/kvision/utils/JSON.kt @@ -22,12 +22,16 @@ package pl.treksoft.kvision.utils import kotlinx.serialization.ImplicitReflectionSerializer +import kotlinx.serialization.SerializationStrategy import kotlinx.serialization.context.SimpleModule import kotlinx.serialization.json.Json import kotlinx.serialization.serializer import pl.treksoft.kvision.types.DateSerializer import kotlin.js.Date +/** + * JSON utility functions + */ object JSON { val plain = Json().apply { @@ -42,7 +46,15 @@ object JSON { * An extension function to convert Serializable object to JS dynamic object */ @UseExperimental(ImplicitReflectionSerializer::class) - inline fun T.toObj(): Any { - return kotlin.js.JSON.parse(plain.stringify(T::class.serializer(), this)) + inline fun T.toObj(): dynamic { + return this.toObj(T::class.serializer()) + } + + /** + * An extension function to convert Serializable object to JS dynamic object + * @param serializer a serializer for T + */ + fun T.toObj(serializer: SerializationStrategy): dynamic { + return kotlin.js.JSON.parse(plain.stringify(serializer, this)) } } -- cgit