users@jaxb.java.net

Re: JAXB Unmarshalling: How to use null as default value?

From: Kohsuke Kawaguchi <Kohsuke.Kawaguchi_at_Sun.COM>
Date: Wed, 06 Dec 2006 15:47:50 -0800

janw wrote:
> Hello,
>
> There is an element "temperature" of type decimal defined. In Java it is a
> property of type Double.
>
> Our aim is to allow the customer both to omit the element and to send an
> empty element.
> In both cases the property should be "null".
> When the tag is omitted, everything works fine, the field remains null.
> When the tag is empty (<temperature/>), the parser throws an Exception for
> the empty element when he tries to parse the empty string into a Double.
>
> I cannot do this:
>
> @XmlElement(defaultValue="null")
> protected Double temperature;
>
> The only possibility we see is sth like this:
>
> final static Double DEFAULT_VALUE = Double.MIN_VALUE;
>
> public Double getTemperature() {
> if(this.temperature.equals(DEFAULT_VALUE)){
> this.temperature = null;
> }
> return this.temperature;
> }
>
> But this a bad hack.
>
> Here is the Schema:
>
> <xs:element name="temperatur" minOccurs="0" >
> <xs:simpleType>
> <xs:restriction base="xs:decimal" >
> <xs:minInclusive value="-270" />
> <xs:maxInclusive value="100" />
> </xs:restriction>
> </xs:simpleType>
>
> Is there a more elegant solution?

I believe I followed up on this in the forum [1], so let's keep this
discussion there.

[1] http://forums.java.net/jive/thread.jspa?threadID=20451
-- 
Kohsuke Kawaguchi
Sun Microsystems                   kohsuke.kawaguchi_at_sun.com