question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

From Json Number 1.0 to Kotlin Int

See original GitHub issue

Describe the bug I assume the prerequesite of the json number format

JSON can represent four primitive types (strings, numbers, booleans, and null) and two structured types (objects and arrays).

Now Java / Kotlin has Integers, when deserializing from {“x”: 1.0} to x: Int The expected behavior for me would be that kotlinx would accept 1.0 in json for an integer but:

Unexpected JSON token at offset 6: Unexpected symbol ‘.’ in numeric literal kotlinx.serialization.json.internal.JsonDecodingException: Unexpected JSON token at offset 6: Unexpected symbol ‘.’ in numeric literal JSON input: {“x”: 1.0}

To Reproduce run: ` @Serializable data class Data (val x: Int )

val json = “”“{“x”: 1.0}”“” val jsonDeserializer = Json {} val data: Data = jsonDeserializer.decodeFromString(json)
`

Expected behavior kotlinx (json) would accept 1.0 for Integers (obv. not for 1.2 / 1.7 etc.)

Environment

  • Kotlin version: [1.5.30]
  • Library version: [1.2.2]
  • Kotlin platforms: [JVM]
  • Gradle version: [e.g. 4.10]

Is this a bug, wrongly configurated from me or intentional and I have to cast after deserialization? (I found nothing in the doc about this)

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
qwwdfsadcommented, Oct 28, 2021

We definitely not going to change the current behaviour.

What we are considering right now is whether to allow such parsing in coerceInputValues or lenient modes

2reactions
sandwwraithcommented, Aug 31, 2021

Since JSON specification in this place is vague and JSON per se does not differentiate integer/fractional types, I’d say this behavior is implementation-defined and coincidental. We should take a look at other libraries and decide whether we want to support it

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to parse JSON in Kotlin? - Stack Overflow
Copy your JSON and go to this(https://json2kt.com) website and paste your JSON to Input Json box. Write package(ex: com.example.appName) and ...
Read more >
Serializer coerce string to int · Issue #1042 · Kotlin ... - GitHub
Describe the bug When requiring type of Int in a serializable class, type coerce string to int without throwing an error.
Read more >
IntRange - Kotlin Programming Language
Returns the first non-null value produced by transform function being applied to elements of this collection in iteration order, or null if no...
Read more >
BigDecimal and BigInteger in Java - Baeldung
Unscaled value – an arbitrary precision integer; Scale – a 32-bit integer representing the number of digits to the right of the decimal ......
Read more >
JsonNode (jackson-databind 2.7.0 API) - FasterXML
Method that will try to convert value of this node to a Java boolean. JSON booleans map naturally; integer numbers other than 0...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found