users@jaxb.java.net

Re: Boolean type - unmarshalling

From: Kohsuke Kawaguchi <Kohsuke.Kawaguchi_at_Sun.COM>
Date: Thu, 08 Jun 2006 15:51:33 -0700

Vallamshettla, Anand wrote:
> If a document with <BooleanTypeTag>hello</ BooleanTypeTag> is
> unmarshalled, Jaxb2 is setting the value to 'false'. Shouldn't the value
> of the field be 'null'?
>
> Let's say, we unmashall <BooleanTypeTag>turkey</ BooleanTypeTag>, the
> value of the field is set to 'true'.
>
> In both cases, we would expect Jaxb2 to set the value of the field to
> 'null' as the type doesn't match.

Generally speaking, unless you turn the validation on, the JAXB RI
itself won't enforce the datatype constraints. So just like
java.lang.Boolean.parseBoolean() takes everything except "true" as
false, the JAXB RI also recognizes things other than "true"/"false"/"0"/"1".

It's not clear to me if the behavior you proposed is better, too. For
example, I can argue that the fact that <BooleanTypeTag> was present
does indicate that the document is trying to set some value, and in that
sense true or false is better than null as it at least captures the
presence of some value.


> 2) Is there a document that describes how unmarshalling will be
> done when values doesn't math type?

No. Those aren't really committed behaviors either. The spec doesn't
really say what to do when invalid values are encountered.

If you'd like to catch those errors, use validation.


-- 
Kohsuke Kawaguchi
Sun Microsystems                   kohsuke.kawaguchi_at_sun.com