diff --git a/klaxon/src/main/kotlin/com/beust/klaxon/JsonObject.kt b/klaxon/src/main/kotlin/com/beust/klaxon/JsonObject.kt index dfd0395..936b240 100644 --- a/klaxon/src/main/kotlin/com/beust/klaxon/JsonObject.kt +++ b/klaxon/src/main/kotlin/com/beust/klaxon/JsonObject.kt @@ -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(" ") } diff --git a/klaxon/src/test/kotlin/com/beust/klaxon/Issue340Test.kt b/klaxon/src/test/kotlin/com/beust/klaxon/Issue340Test.kt new file mode 100644 index 0000000..cca22c3 --- /dev/null +++ b/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 = 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) + + 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) + } +}