decodeFromDynamic

Converts native JavaScript objects into Kotlin ones, verifying their types.

A result of decodeFromDynamic(nativeObj) should be the same as kotlinx.serialization.json.Json.decodeFromString(kotlin.js.JSON.stringify(nativeObj)). This class also supports array-based polymorphism if the corresponding flag in Json.configuration is set to true. Does not support any other Map keys than String. Has limitation on Long type: any JS number that is greater than abs(2^53-1) is considered to be imprecise and therefore can't be deserialized to Long. Either use Double type for such values or pass them as strings using LongAsStringSerializer afterwards.

Usage example:

@Serializable
data class Data(val a: Int)

@Serializable
data class DataWrapper(val s: String, val d: Data?)

val dyn: dynamic = js("""{s:"foo", d:{a:42}}""")
val parsed = Json.decodeFromDynamic(DataWrapper.serializer(), dyn)
parsed == DataWrapper("foo", Data(42)) // true

inline fun <T> Json.decodeFromDynamic(dynamic: dynamic): T(source)

A reified version of decodeFromDynamic.