JAXB: How to Set Up Serialization of Xml Double Fields

I have a legacy class with lots of open double fields. All double fields are initialized with the Double.MAX_VALUE symbol to indicate that they are empty. (Inherited serialization is encoded to ignore the field and not serialize if the field is Double.MAX_VALUE ).

Now we are trying to serialize this class in Xml using the JAXB Marshaller. It works fine, except that we want to prevent Xml generation for fields that are Double.MAX_VALUE .

We do not use a separate JAXB scheme, simply marking our classes with various javax.xml.bind.annotation Annotations. If a schema is used, you can add a <javaType> element to specify your own DataType converter. Is there a way to do this using annotations or programmatically?

Having tried the approach recommended below, I still cannot get the XmlAdapter :

 @XmlJavaTypeAdapters({ @XmlJavaTypeAdapter(value=EmptyDoubleValueHandler.class, type=Double.class), @XmlJavaTypeAdapter(value=EmptyDoubleValueHandler.class, type=double.class)}) package tta.penstock.data.iserver; import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapters; 

My top level class is tta.penstock.data.iserver.OrderBlotter, which contains a list of tta.penstock.data.iserver.OrderResponseWrappers, which extends com.eztech.OrderResponse . All double fields are contained in com.eztech.OrderResponse .

My unit test code does the following:

 JAXBContext context = JAXBContext.newInstance(new Class[] { OrderBlotter.class, OrderResponseWrapper.class, OrderResponse.class}); Marshaller marshaller = context.createMarshaller(); StringWriter stringWriter = new StringWriter(); marshaller.marshal(blotter, stringWriter); System.out.println("result xml=\n" + stringWriter.toString()); 

But double values ​​are still not processed by the XmlAdapter . I know that I have something missing, but I'm not sure what it is.

+4
source share
2 answers

You can use the XmlAdapter:

Xmladapter

 package example; import javax.xml.bind.annotation.adapters.XmlAdapter; public class DoubleAdapter extends XmlAdapter<Double, Double>{ @Override public Double unmarshal(Double v) throws Exception { return v; } @Override public Double marshal(Double v) throws Exception { if(Double.MAX_VALUE == v) { return null; } else { return v; } } } 

Model object

 package example; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; @XmlRootElement public class Root { @XmlJavaTypeAdapter(DoubleAdapter.class) public Double maxDouble = Double.MAX_VALUE; @XmlJavaTypeAdapter(DoubleAdapter.class) public Double aDouble = 123d; } 

Demo code

 package example; import javax.xml.bind.JAXBContext; import javax.xml.bind.Marshaller; public class Demo { public static void main(String[] args) throws Exception { JAXBContext jc = JAXBContext.newInstance(Root.class); Marshaller marshaller = jc.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(new Root(), System.out); } } 

UPDATE

StaxMan's offer is good. If you specify the following annotation at the package level, you can avoid the need to individually annotate all double properties

package-info.java

 @XmlJavaTypeAdapters({ @XmlJavaTypeAdapter(type=Double.class, value=DoubleAdapter.class) }) package example; import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapters; 
+9
source

Write a receiver that returns null, not Double.MAX_VALUE? (if the type is "double", you must first change it to "Double" to allow null). Since JAXB ignores zeros by default, this should achieve what you are trying to do. This suggests that you can modify the class in question.

0
source

All Articles