Skip to content

Commit

Permalink
JsonObject: allow any data type as key
Browse files Browse the repository at this point in the history
  • Loading branch information
Bixilon committed Nov 28, 2021
1 parent 41e7e66 commit 2207f27
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
3 changes: 2 additions & 1 deletion klaxon/src/main/kotlin/com/beust/klaxon/JsonObject.kt
Expand Up @@ -31,7 +31,8 @@ by map {
indent(result, level + 1)
}

result.append(Render.renderString(k)).append(":")
// Do not remove k::toString, it allows any data type (that needs to be casted to a string before), without crashing
result.append(Render.renderString(k.toString())).append(":")
if (prettyPrint && !canonical) {
result.append(" ")
}
Expand Down
27 changes: 27 additions & 0 deletions klaxon/src/test/kotlin/com/beust/klaxon/Issue340Test.kt
@@ -0,0 +1,27 @@
package com.beust.klaxon

import org.testng.annotations.Test
import java.util.*
import kotlin.test.assertEquals

@Test
class Issue340Test {

fun issue340() {
val data:Map<Any, Any?> = mapOf(
12 to "dummy",
"test" to "123",
"test2" to 123,
23.12 to 12,
UUID.fromString("418ef170-f770-4172-8a19-2990e65c6fda") to "abc",
)

val json = JsonObject(data as Map<String, Any?>)

val jsonString = Klaxon().toJsonString(json)

val expected = """{"12": "dummy", "test": "123", "test2": 123, "23.12": 12, "418ef170-f770-4172-8a19-2990e65c6fda": "abc"}"""

assertEquals(jsonString, expected)
}
}

0 comments on commit 2207f27

Please sign in to comment.