diff options
author | Robert Jaros <rjaros@finn.pl> | 2019-02-13 13:42:22 +0100 |
---|---|---|
committer | Robert Jaros <rjaros@finn.pl> | 2019-02-13 13:42:22 +0100 |
commit | 8beb2bcc4806a69dc99acdd706f7de94bbe35955 (patch) | |
tree | c3d78714cd4982943472fcd7e878efe23bea238b /src/main/kotlin/pl/treksoft/kvision/utils/JSON.kt | |
parent | ae28bdd65cbc56b10d36d33a9090ee31ca96908e (diff) | |
download | kvision-8beb2bcc4806a69dc99acdd706f7de94bbe35955.tar.gz kvision-8beb2bcc4806a69dc99acdd706f7de94bbe35955.tar.bz2 kvision-8beb2bcc4806a69dc99acdd706f7de94bbe35955.zip |
Unification of @Serializable -> JSON functions
Diffstat (limited to 'src/main/kotlin/pl/treksoft/kvision/utils/JSON.kt')
-rw-r--r-- | src/main/kotlin/pl/treksoft/kvision/utils/JSON.kt | 16 |
1 files changed, 14 insertions, 2 deletions
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 <reified T : Any> T.toObj(): Any { - return kotlin.js.JSON.parse(plain.stringify(T::class.serializer(), this)) + inline fun <reified T : Any> 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> T.toObj(serializer: SerializationStrategy<T>): dynamic { + return kotlin.js.JSON.parse(plain.stringify(serializer, this)) } } |