pdvrieze/xmlutil

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

mgroth0 opened this issue ยท 4 comments

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.

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.

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

@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.

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