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 --- src/main/kotlin/pl/treksoft/kvision/utils/JSON.kt | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'src/main/kotlin/pl/treksoft/kvision/utils/JSON.kt') 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