Open rgmz opened 3 years ago
You saved me a lot of time, thanks @rgmz :) I stumbled across this and had no clue what was going on...
Still here on 1.7.3, reproducer:
@Test
fun testException() {
val jsonObject = JsonObject(mapOf("foo" to JsonPrimitive("bar")))
println(Json.decodeFromJsonElement<String>(jsonObject))
}
Description
I encountered a confusing exception when writing JsonElement-related tests. I don't know how likely someone would be to stumble across this in the wild.
While it isn't a bug per se, the exception message could be clarified to point out the actual failure cause.
To Reproduce
Consider the following (contrived) code:
Attempting to decode
JsonObject
orJsonArray
as a primitive (String, Boolean, Long, etc.) will yield the confusing error mentioned above.https://github.com/Kotlin/kotlinx.serialization/blob/43d5f7841fc744b072a636b712e194081456b5ba/core/commonMain/src/kotlinx/serialization/internal/Tagged.kt#L320-L324
Expected behavior
Provide a meaningful exception, if possible. For example, attempting to decode a
JsonObject
orJsonArray
as aJsonPrimitive
yields a more friendly error:Environment