Skip to content

Improve failure message on encountering unknown key in JSON #2637

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

Closed
qwwdfsad opened this issue Apr 17, 2024 · 2 comments
Closed

Improve failure message on encountering unknown key in JSON #2637

qwwdfsad opened this issue Apr 17, 2024 · 2 comments

Comments

@qwwdfsad
Copy link
Contributor

Right now, the failure is the following:

Exception in thread "main" kotlinx.serialization.json.internal.JsonDecodingException: Unexpected JSON token at offset 20: Encountered an unknown key 'version' at path: $.name
Use 'ignoreUnknownKeys = true' in 'Json {}' builder to ignore unknown keys.
JSON input: {"name":"unknown", "version": 2.0}
	at kotlinx.serialization.json.internal.JsonExceptionsKt.JsonDecodingException(JsonExceptions.kt:24)

Maybe it is worth making it more explicit and straightforward, for example:

Exception in thread "main" kotlinx.serialization.json.internal.JsonUnknownKeyException: Encountered an unknown key 'version' at path: $.name
Use 'ignoreUnknownKeys = true' in 'Json {}' builder to ignore unknown keys.
JSON input: {"name":"unknown", "version": 2.0}
	at kotlinx.serialization.json.internal.JsonExceptionsKt.JsonDecodingException(JsonExceptions.kt:24)

Note that the actual exception can still be internal

@sandwwraith
Copy link
Member

The version you suggest lacks an offset, which may be important for large json inputs which do not fit in the hint

@qwwdfsad
Copy link
Contributor Author

Well, it can be put after the main message. The problem with the current one is that it first explains where things went wrong instead of what went wrong

sandwwraith added a commit that referenced this issue Dec 5, 2024
for more fine-grained control over JsonBuilder.ignoreUnknownKeys setting.

Fixes #1420

Also improve error message and path handling when 'Unknown key' exception is thrown

Fixes #2869
Fixes #2637
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