encodeToDynamic

fun <T> Json.encodeToDynamic(serializer: SerializationStrategy<T>, value: T): dynamic

Converts Kotlin data structures to plain Javascript objects

Limitations:

  • Map keys must be of primitive or enum type

  • All Long values must be less than abs(2^53-1). Otherwise, they're encoded as doubles with precision loss and require isLenient flag of Json.configuration set to true.

Example of usage:

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

val wrapper = DataWrapper("foo", "bar")
val plainJS: dynamic = Json.encodeToDynamic(DataWrapper.serializer(), wrapper)

inline fun <T> Json.encodeToDynamic(value: T): dynamic

A reified version of encodeToDynamic.