Note that there are some explanatory texts on larger screens.

plurals
  1. POJAXB: How to customize Xml serialization of double fields
    primarykey
    data
    text
    <p>I have a legacy class, with a lot of public double fields. All double fields are initialized with <code>Double.MAX_VALUE</code> to indicate that they are empty. (The legacy serialization is coded to ignore the field and not serialize if field is equals to <code>Double.MAX_VALUE</code>).</p> <p>We are now trying to serialize this class to Xml using JAXB Marshaller. It is working fine, except that we want to prevent generating Xml for fields which equal <code>Double.MAX_VALUE</code>. </p> <p>We aren't using a separate JAXB schema, just marking up our classes with various <code>javax.xml.bind.annotation</code> Annotations. If a schema is used, you can add a <code>&lt;javaType&gt;</code> element to specify a custom DataType converter. Is there any way to do this using Annotations or programmatically?</p> <p>After trying approach recommended below, I still can't get <code>XmlAdapter</code> picked up:</p> <pre><code>@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; </code></pre> <p>My top level class is: tta.penstock.data.iserver.OrderBlotter, which contains a list of tta.penstock.data.iserver.OrderResponseWrappers which extends <code>com.eztech.OrderResponse</code>. All the double fields are contained in <code>com.eztech.OrderResponse</code>.</p> <p>My unit test code does the following:</p> <pre><code>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()); </code></pre> <p>But the double values still don't get handled by the <code>XmlAdapter</code>. I know I'm missing something basic, but I'm not sure what it is.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload