Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Followup3: JsonObject: allow any data type as key #345

Merged
merged 1 commit into from
Nov 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion klaxon/src/main/kotlin/com/beust/klaxon/JsonObject.kt
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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)
}
}