Reputation: 2726
I am trying to marshal/unmarshal a Color to XML. The JAXB project had example code for doing this exact thing via a XmlJavaTypeAdapter https://jaxb.java.net/guide/XML_layout_and_in_memory_data_layout.html.
The marshaling works fine and the output is what I expect:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<beanWithColor>
<foreground>#ff0000ff</foreground>
<name>CleverName</name>
</beanWithColor>
However, when trying to go from XML to object the unmarshal method is never called. Can anyone provide insight as to why? After it is unmarshalled foreground color is null and I have confirmed with my debugger that unmarshal is never being called:
BeanWithColor{foreground=null, name='CleverName'}
SCCE:
@XmlRootElement
public class BeanWithColor {
private String name;
private Color foreground;
public BeanWithColor() {
}
public BeanWithColor(Color foreground, String name) {
this.foreground = foreground;
this.name = name;
}
@XmlJavaTypeAdapter(ColorAdapter.class)
public Color getForeground() {
return this.foreground;
}
public void setForeground(Color foreground) {
this.foreground = foreground;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("BeanWithColor{");
sb.append("foreground=").append(foreground);
sb.append(", name='").append(name).append('\'');
sb.append('}');
return sb.toString();
}
public static void main(String[] args) {
BeanWithColor bean = new BeanWithColor(Color.blue, "CleverName");
try {
StringWriter writer = new StringWriter(1000);
JAXBContext context = JAXBContext.newInstance(BeanWithColor.class);
final Marshaller marshaller = context.createMarshaller();
marshaller.marshal(bean, writer);
System.out.println("Marshaled XML: " + writer);
final Unmarshaller unmarshaller = context.createUnmarshaller();
BeanWithColor beanWithColor = (BeanWithColor) unmarshaller.unmarshal(new StringReader(writer.toString()));
System.out.println("beanWithColor = " + beanWithColor);
} catch (JAXBException e) {
e.printStackTrace();
}
}
static class ColorAdapter extends XmlAdapter<String, Color> {
public Color unmarshal(String s) {
return Color.decode(s);
}
public String marshal(Color color) {
return '#' + Integer.toHexString(color.getRGB());
}
}
}
Upvotes: 1
Views: 2443
Reputation: 148977
I suspect that XmlAdapter
is being called for unmarshal
but that the Color.decode
method is failing (this is what happens when I debugged your code).
Color.decode("#ff0000ff");
results in the following:
Exception in thread "main" java.lang.NumberFormatException: For input string: "ff0000ff"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:461)
at java.lang.Integer.valueOf(Integer.java:528)
at java.lang.Integer.decode(Integer.java:958)
at java.awt.Color.decode(Color.java:707)
You can set a ValidationEventHandler
on the Unmarshaller
to get a hook on all failures. By default a JAXB impl wouldn't report that problem.
The Fix
The ColorAdapter.marshal
method needs to be fixed to return the correct value.
String rgb = Integer.toHexString(color.getRGB());
return "#" + rgb.substring(2, rgb.length());
Upvotes: 2