JAXB: How to customize Xml serialization of double fields?

You could use an XmlAdapter: bdoughan.blogspot.com/2010/07/xmladapter... The XmlAdapter package example; import javax.xml.bind.annotation.adapters. XmlAdapter; public class DoubleAdapter extends XmlAdapter{ @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; } } } The 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 suggestion is a good one. If you specify the following package level annotation you can avoid the need of individually annotating all the 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.

I was looking at that approach. The problem is that it would be problematic to mark up all the double fields in the legacy class. I was looking for a global "hook" where we could specify or put exactly the kind of type Adapter you list above.So i.e.

Something which says "for every double field in this class, use this XmlAdapter". – Sam Goldberg Nov 10 '10 at 22:07 1 You can also add @XmlAdapter annotations for packages (package-info. Java can contain annotations!

) -- check out java docs. That might make this approach feasible – StaxMan Nov 11 '10 at 4:05 I have updated my answer to include StaxMan's suggestion. – Blaise Doughan Nov 11 '10 at 14:34 I have been trying this approach, this seems like what is needed.

But it seems that the XmlJavaTypeAdapter annotation I put in there isn't being picked up during serialization. I think it maybe because the fields are contained in a class defined in another package (outside of our project). But really, I'm not sure why it isn't being picked up.

– Sam Goldberg Nov 11 '10 at 15:37 As long as those classes are included in your JAXBContext you should be fine. You also may want to check out EclipseLink JAXB (MOXy), I'm the tech lead: eclipse.Org/eclipselink/moxy. Php – Blaise Doughan Nov 11 '10 at 15:40.

Write a getter that returns null, instead of Double. MAX_VALUE? (if type is 'double', need to change it to 'Double' first, to allow nulls).

Since JAXB by default ignores writing out of nulls, that should achieve what you are trying to do. This assumes you can modify legacy class in question.

1 Thanks for suggestion. Unfortunately, legacy class can't easily be modified (and there are a "million" public double fields which need to be wrapped). Was looking for an easier way out... – Sam Goldberg Nov 10 '10 at 22:09.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions