Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JsonPrimitive.intOrNull returns an invalid value instead of null when the content string starts with a digit #2656

Open
codeZfox opened this issue Apr 29, 2024 · 1 comment

Comments

@codeZfox
Copy link

Extension property JsonPrimitive.intOrNull returns an invalid value instead of null when the content string starts with a digit

To Reproduce

    @Serializable
    class SomeData(
        @SerialName("number")
        val number: Int
    )
    
   val someData2 = Json.decodeFromString<SomeData>("""{"number": "3 digit then random string"}""")
   
   someData2.number == 3 // true

the actual value in the number field is 3

Expected behavior
the expected value in the number field is 3

Environment

  • Kotlin version: 1.9.10
  • Library version: 1.6.0+
  • Kotlin platforms: JVM
@sandwwraith
Copy link
Member

Hi, can you please clarify the issue? I do not see any usages of JsonPrimitive.intOrNull in your code. decodeFromString function tries to map input directly onto @Serializable class. If a value cannot be converted to Int, an exception is thrown. "3 digit then random string" cannot be converted to Int, because it is not appropriate to convert only a part of the string.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants