Skip to content

Json.decodeFromDynamic() broken for UInt #2713

Open
@swankjesse

Description

@swankjesse

Describe the bug
I’d like to use decodeFromDynamic() in Kotlin/JS to read a large unsigned integer. Unfortunately the implementation doesn’t convert the native value from a large JavaScript number (ie. a double) to a value in the accepted range of Int.

To Reproduce

  fun testAsInt() {
    assertEquals(
      2147483647u,
      Json.decodeFromDynamic<UInt>(UInt.serializer(), js("2147483647"))
    )
    assertEquals(
      2147483648u,
      Json.decodeFromDynamic<UInt>(UInt.serializer(), js("2147483648"))
    )
  }

Expected behavior
Test pass

Actual behavior
The in-memory object is declared as an Int but it’s actually beyond the range of Int. We get a test that fails in a totally weird way, because if you don’t enforce that data is actually an Int, you have multiple non-equal representations of each value.

AssertionError: Expected <2147483648>, actual <2147483648>.
	at DefaultJsAsserter.protoOf.assertTrue_rpw5fg(/Volumes/Development/zipline/zipline/build/compileSync/js/test/testDevelopmentExecutable/kotlin/jsMainSources/kotlin/kotlin/test/JsImpl.kt:23)
	at DefaultJsAsserter.assertEquals(/Volumes/Development/zipline/zipline/build/compileSync/js/test/testDevelopmentExecutable/kotlin/commonMainSources/kotlin/kotlin/test/Assertions.kt:671)
	at DefaultJsAsserter.protoOf.assertEquals_ldumo(/Volumes/Development/zipline/zipline/build/compileSync/js/test/testDevelopmentExecutable/kotlin/jsMainSources/kotlin/kotlin/test/DefaultJsAsserter.kt:27)
	at <global>.assertEquals(/Volumes/Development/zipline/zipline/build/compileSync/js/test/testDevelopmentExecutable/kotlin/commonMainSources/kotlin/kotlin/test/Assertions.kt:63)

Environment

  • Kotlin version: 2.0.0
  • Library version: 1.6.3
  • Kotlin platforms: Kotlin/JS

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions