kotlinx.serialization icon indicating copy to clipboard operation
kotlinx.serialization copied to clipboard

Kotlinx.serialization rounds longs on deserialization when using Kotlin/JS's IR compiler

Open MrPowerGamerBR opened this issue 4 years ago • 3 comments

Describe the bug When trying to deserialize a object that has a long when using the new IR compiler, kotlinx.serialization rounds the long instead of keeping it as is.

This does not happen with the legacy compiler!

And yes, I know, the IR compiler is still experimental... (I'm using it because the IR compiler has some very nice size gains compared to the legacy compiler!) but it is still a very nasty bug!

To Reproduce

import kotlinx.browser.document
import kotlinx.serialization.Serializable
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json

fun main() {
    document.write("Hello, world!")

    val data = DataWithId(236167700777271297L)

    val json = Json.encodeToString(data)

    println("Serialized Data: $json")

    val dataDeserialized = Json.decodeFromString<DataWithId>(json)

    println("Deserialized Data: $dataDeserialized")
}

@Serializable
data class DataWithId(val id: Long)

Expected behavior

That the id would be 236167700777271297, not 236167700777271300

With the IR compiler: image

With the Legacy compiler: image

Environment

  • Kotlin version: 1.4.20
  • Library version: 1.1.0
  • Kotlin platforms: JavaScript (with the IR compiler)
  • Gradle version: 6.7
  • IDE version (if bug is related to the IDE) [e.g. IntellijIDEA 2019.1, Android Studio 3.4]
  • Other relevant context [e.g. OS version, JRE version, ... ]

MrPowerGamerBR avatar Mar 13 '21 12:03 MrPowerGamerBR

Still happening in Kotlin 1.5.31 + kotlinx.serialization 1.3.1, however I think I'm going to shift the blame to Kotlin itself, not kotlinx.serialization!

println("Test: ${"197308318119755776".toLong()}")

Prints Test: 197308318119755780 😭

MrPowerGamerBR avatar Nov 26 '21 02:11 MrPowerGamerBR

Created YouTrack for this compiler bug: https://youtrack.jetbrains.com/issue/KT-49913

MrPowerGamerBR avatar Nov 26 '21 02:11 MrPowerGamerBR

Fixed in Kotlin 1.6.20-M1

MrPowerGamerBR avatar Feb 26 '22 01:02 MrPowerGamerBR