m

    Michal Janos

    11 months ago
    Hi, please, is there any way how to use
    encodeToString
    of double in json for js (multiplatform) with trailing zeros? In our case, we have test json with double format (with trailing zeros), we use
    decodeFromString
    and than
    encodeToString
    and compare it. For JVM it is allright, but in JS output is without trailing zeros.
    rnett

    rnett

    11 months ago
    You're wanting to keep the trailing 0s in the serialized double? That's not going to happen. I'd add an Int field of # of digits or # of 0s to add.
    m

    Michal Janos

    11 months ago
    Quick test for what I mean
    @Test
        fun testDecodeEncode() {
            val jsonTestData = "{\"foo\":0.0,\"bar\":1.25}"
            val testItemData: TestItem =
                Json.decodeFromString(
                    jsonTestData
                )
    
            assertEquals(jsonTestData, Json.encodeToString(testItemData))
        }
    
        @Serializable
        data class TestItem(
            val foo: Double,
            val bar: Double,
        )
    What is OK for JVM, but for JS:
    AssertionError: Expected <{"foo":0.0,"bar":1.25}>, actual <{"foo":0,"bar":1.25}>.
    e

    ephemient

    11 months ago
    JS Number stringification is not only different from Java, it's different in different implementations: https://kotlinlang.slack.com/archives/C0B8L3U69/p1634116661180600?thread_ts=1634116456.180200&amp;cid=C0B8L3U69
    j

    Joffrey

    10 months ago
    If your requirements are about the deserialized values, it should be tested differently. If your requirements are about the actual JSON, maybe reconsider the requirements? 🙂 More precisely, why should
    0
    be a problem in the JSON as opposed to
    0.0
    ?
    m

    Michal Janos

    10 months ago
    Thanks for replies, for now we changed test data as quick sollution.