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

Deserializing invalid JSON does not always throw SerializationException #1261

Closed
diesieben07 opened this issue Dec 21, 2020 · 0 comments
Closed
Assignees

Comments

@diesieben07
Copy link

When deserializing JSON that does not fit the target classes, SerializationException should be thrown always. If the input JSON has the wrong type (e.g. 123 where a Boolean is expected), IllegalStateException is thrown instead (by StreamingJsonDecoder, which uses toBooleanStrict, for example).

This makes it difficult to distinguish invalid JSON from other errors, especially in a server environment, where you want to inform clients that their request is invalid (as opposed to some internal server error).

To Reproduce

@Serializable
data class SerializerBug(val booleanValue: Boolean)

fun main() {
    val json = Json {  }
    // throws java.lang.IllegalStateException: 123 does not represent a Boolean
    json.decodeFromString(SerializerBug.serializer(), """{"booleanValue": 123}""")
}

Expected behavior
kotlinx.serialization.SerializationException is thrown instead of generic IllegalStateException.

Environment

  • Kotlin version: 1.4.21
  • Library version: 1.0.1
  • Kotlin platforms: Tested on JVM, but this bug exists in common code
  • Gradle version: 6.7
@qwwdfsad qwwdfsad added the bug label Dec 21, 2020
@qwwdfsad qwwdfsad self-assigned this Dec 21, 2020
qwwdfsad added a commit that referenced this issue Jan 20, 2021
…JSON

Also, narrow down the type of swallowed exceptions

Fixes #1261
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

3 participants