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

How the deserialization failures are supposed to be catched? #2805

Open
unclechu opened this issue Sep 1, 2024 · 4 comments
Open

How the deserialization failures are supposed to be catched? #2805

unclechu opened this issue Sep 1, 2024 · 4 comments
Labels

Comments

@unclechu
Copy link

unclechu commented Sep 1, 2024

I’m trying to catch a deserialization error (from a call like this: Json.decodeFromString<Foo>("...")) and transform it into a pure value (e.g. Either<JsonDeserializationFailure, Foo>). But I couldn’t find a solution, I don’t want to catch a wide-scoped Exception that can be totally not related to deserialization issue. I see that the library throws kotlinx.serialization.json.internal.JsonDecodingException but I can’t access it because it’s internal. How is it supposed to be done?

I went through a bunch of articles about kotlinx.serialization but apparently none of them are talking about errors handling which leaves me confused.

@unclechu
Copy link
Author

unclechu commented Sep 1, 2024

I wonder why does the kotlinx.serialization.json.internal.JsonDecodingException has to be internal and not a publicly exposed one?

@sandwwraith
Copy link
Member

sandwwraith commented Sep 2, 2024

decodeFromString documentation (https://kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-json/kotlinx.serialization.json/-json/decode-from-string.html) mentions that this method can throw kotlinx.serialization.SerializationException in case of decoding errors and IllegalArgumentException in case of data validation errors (e.g. require(...) in init {} block of the class). Does that answer your question?

@pdvrieze
Copy link
Contributor

pdvrieze commented Sep 2, 2024

@sandwwraith Just note that require throws IllegalArgumentException (check throws IllegalStateException). In this case require/IllegalArgumentException would be the appropriate exceptions as it is "input" validation.

@sandwwraith
Copy link
Member

Yeah, I mixed them up in my comment, documentation clearly mentions IllegalArgument.

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

No branches or pull requests

3 participants