Search code examples
javascriptjsonkotlinkotlin-js-interop

How to serialize a Map to JSON string via JSON.stringify in kotlin JS?


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?


Solution

  • Actually, you don't get the result of JSON.stringify. Instead, you get result of kotlin.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. And HashMap.toString function is defined to generate such string, which is not a JSON string. You should have used JSON.stringify without second and third parameter. However, this won't work as well, producing Uncaught 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-like Map API, which is unavailable in JavaScript object. So, HashMap is not suitable for what you doing. There are several solutions:

    1. 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.

    2. 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

      fun main(args: Array<String>) {
          val testData = json(
              "name" to "albert",
              "age" to 26,
              "work" to arrayOf("1", "2", "3")
          )
      
          var value = JSON.stringify(testData)
      
          println(value)
      }