RFC: Allow disabling coercion between JSON primitives #3063
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Currently, and independent of the
isLenient
option, the JSON decoders will coerce Strings to other primitiv types, allowing e.g."true"
to be decoded asBoolean
. We are currently transitioning from a very strict decoder setup based on Jackson over to kotlinx serialization for some of the nicer kotlin-specific functionality and the lack of reflection.This PR is currently meant as an RFC to gauge if this is something that would be accepted upstream. As such there are some caveats:
@ExperimentalSerializationApi
, but it is not currently.I would obviously add those things, if there is a chance this would be pulled.
The implementation is rather simple: Introduce a boolean option and in and around the lexer prevent the use of quotes strings, where they wouldn't be necessary unless coercion is wanted. In some places the necessary functionality was already in place as separate methods (decodeBoolean vs decodeBooleanLenient), in other I was able to add minimal changes, usually as part of existing checks around string quotes. I tried to keep the interfaces stable.
Related issues: