users@jaxb.java.net

JAXB Unmarshalling: How to use null as default value?

From: janw <jam.tiga_at_gmx.de>
Date: Thu, 23 Nov 2006 06:47:57 -0800 (PST)

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?

Regards,
Jan
-- 
View this message in context: http://www.nabble.com/JAXB-Unmarshalling%3A-How-to-use-null-as-default-value--tf2692870.html#a7509342
Sent from the java.net - jaxb users mailing list archive at Nabble.com.