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?
Actually, you don't get the result of
JSON.stringify
. Instead, you get result ofkotlin.collections.HashMap.toString
. The reason is following: you pass the lambda as a second parameter:{ _, value -> value.toString() }
. This converts your entire map to string, using,toString()
function. AndHashMap.toString
function is defined to generate such string, which is not a JSON string. You should have usedJSON.stringify
without second and third parameter. However, this won't work as well, producingUncaught TypeError: Converting circular structure to JSON
error. The reason is following:JSON.stringify
is not part of Kotlin language, it's just a typed definition of a native browser object, called JSON.HashMap
is not an empty JavaScript object, it allows using any types of objects as keys, it exposes Java-likeMap
API, which is unavailable in JavaScript object. So,HashMap
is not suitable for what you doing. There are several solutions:You can wait until we publish Kotlin multiplatform serilization, see the corresponding discussion. This API is capable of understanding Kotlin clases and converting them to JSON properly.
Don't use Kotlin maps and lists, use native JavaScript entities, like json and pure arrays. Your example can be rewritten the following way:
import kotlin.js.json