Skip to content

Conversation

pschichtel
Copy link
Contributor

@pschichtel pschichtel commented Aug 15, 2025

Currently, and independent of the isLenient option, the JSON decoders will coerce Strings to other primitiv types, allowing e.g. "true" to be decoded as Boolean. 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:

  • There are no additional tests, however the existing tests show that the default behavior is unchanged.
  • No documentation has been written for the new functionality.
  • There are some TODO's which will require closer inspection for a final implementation and some dedicated tests.
  • The new option should probably be marked as @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:

consumeOther(allowQuoted = true)

fun consumeOther(allowQuoted: Boolean): String {
// TODO this string peeking stuff might break things...
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the code around peekedString I'm pretty sure this is not actually a problem, because this branch would only ever be hit when attempting to read a string. Right?

@pschichtel pschichtel changed the title RFC: Allow disabling coercion between primitives RFC: Allow disabling coercion between JSON primitives Aug 15, 2025
@pschichtel pschichtel force-pushed the feature/disable-coercion branch from 8cc0590 to 2e5185f Compare August 18, 2025 10:54
@pschichtel pschichtel changed the base branch from master to dev August 18, 2025 10:55
@pdvrieze
Copy link
Contributor

Looks like a good idea to me

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

Successfully merging this pull request may close these issues.

2 participants