Skip to content

Decoding an integer value containing an exponent #2078

@hemantgokhale

Description

@hemantgokhale

I recently came across a situation where a JSON minifier minified an integer value with 3 or more trailing zeros using an exponent. E.g. 1000 minified to 1e3. It appears that the JSON spec allows this, but kotlinx.serialization currently doesn't handle it.

As a side note, I have verified that Swift’s implementation of JSONSerialization (MacOS 12.6, Swift 5.7) handles this situation correctly i.e. decodes 1e3 to 1000.

Example code:
The first statement in main() works as expected. The second statement throws kotlinx.serialization.json.internal.JsonDecodingException.

import kotlinx.serialization.Serializable
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json

@Serializable
data class SomeData(val count: Int)

fun main() {
    println(Json.decodeFromString<SomeData>("""{ "count": 1000 }"""))
    println(Json.decodeFromString<SomeData>("""{ "count": 1e3 }"""))
}

Expected behavior: Should be able to decode integer/long values containing an exponent.

Environment:

  • Kotlin version: 1.7.20
  • Library version: 1.4.1
  • Kotlin platforms: JVM (I didn’t check the behavior on other platforms)
  • Gradle version: 7.4.2
  • IDE version: IntellijIDEA 2022.2.3
  • MacOS 12.6

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions