pdvrieze / xmlutil

XML Serialization library for Kotlin
https://pdvrieze.github.io/xmlutil/
Apache License 2.0
377 stars 30 forks source link

Boolean decoding should be strict by default, or at least optionally #182

Closed mgroth0 closed 9 months ago

mgroth0 commented 1 year ago

I ran into issues because I assumed boolean decoding was strict by default. But in fact, it by default uses the non-strict toBoolean which means an empty string is interpreted as false.

Boolean decoding should be strict by default. Meaning it will throw an error on empty strings. In my case, I needed empty strings to be interpreted to null, and so I ended up having to create a custom type. Its simply not type safe and chaotic. Interpretting empty strings (empty tags) as false would be nice as a configuration option, but it should be strict by default.

If for some reason we need to keep it non-strict by default, it would be nice to at least add the option to make it strict in the XML configuration. If the option is enabled, XMLDecoder.decodeBoolean can just use toBooleanStrict instead of toBoolean.

pdvrieze commented 1 year ago

For your case where empty means null you'd technically need a custom serializer, you want to support a 3-state system.

I will add some sort of flag/configuration option for this, as changing this behaviour will likely break some-one-else's code that relies on the current behaviour. In addition I'll introduce an XmlBoolean alias that automatically applies the xml proper behaviour.

mgroth0 commented 1 year ago

Thats understandable. Thank you for considering making it into an option!

pdvrieze commented 1 year ago

@mgroth0 You can also use XmlBoolean (which is just a Boolean with a serializer annotation), so you don't need the option in that case.

pdvrieze commented 9 months ago

Now supported (and added to the recommended/recommended_0_86_3 policy.