From Json Number 1.0 to Kotlin Int
See original GitHub issueDescribe the bug I assume the prerequesite of the json number format
JSON can represent four primitive types (strings, numbers, booleans, and null) and two structured types (objects and arrays).
Now Java / Kotlin has Integers, when deserializing from {“x”: 1.0} to x: Int The expected behavior for me would be that kotlinx would accept 1.0 in json for an integer but:
Unexpected JSON token at offset 6: Unexpected symbol ‘.’ in numeric literal kotlinx.serialization.json.internal.JsonDecodingException: Unexpected JSON token at offset 6: Unexpected symbol ‘.’ in numeric literal JSON input: {“x”: 1.0}
To Reproduce run: ` @Serializable data class Data (val x: Int )
val json = “”“{“x”: 1.0}”“”
val jsonDeserializer = Json {}
val data: Data = jsonDeserializer.decodeFromString(json)
`
Expected behavior kotlinx (json) would accept 1.0 for Integers (obv. not for 1.2 / 1.7 etc.)
Environment
- Kotlin version: [1.5.30]
- Library version: [1.2.2]
- Kotlin platforms: [JVM]
- Gradle version: [e.g. 4.10]
Is this a bug, wrongly configurated from me or intentional and I have to cast after deserialization? (I found nothing in the doc about this)
Issue Analytics
- State:
- Created 2 years ago
- Comments:8 (3 by maintainers)
Top GitHub Comments
We definitely not going to change the current behaviour.
What we are considering right now is whether to allow such parsing in
coerceInputValues
orlenient
modesSince JSON specification in this place is vague and JSON per se does not differentiate integer/fractional types, I’d say this behavior is implementation-defined and coincidental. We should take a look at other libraries and decide whether we want to support it