My example code is below:
fun main(args: Array<String>) {
val testData = mapOf<String, Any>(
"name" to "albert",
"age" to 26,
"work" to listOf("1", "2", "3")
)
var value = JSON.stringify(testData, { _, value -> value.toString() }, 2)
println(value)
}
The result is "{name=albert, age=26, work=[1, 2, 3]}".
Seems it misses all the double quotes around the property name and string value.
I'm using KotlinJS rather than Kotlin
So, how to solve this?
Map.toString()and then gives this value to jsJSON.stringify, so"{name=albert, age=26, work=[1, 2, 3]}"is not json, but map.toString() andJSON.stringifyjust adds quotes around this string. What you could do is to convert test data with jackson or gson in java and just forget about JSON or you could try github.com/Kotlin/kotlinx.serialization Also take a look at discuss.kotlinlang.org/t/kotlin-serialization/2063/25println(JSON.stringify(testData.toString())), still no quotes. According to the posts you share, seems that built-in serialization things haven't land to KotlinJS world yet. I will look into 3rd party libs as you said, thanks.