jackson-modules-base
jackson-modules-base copied to clipboard
Jackson emitting Jaxby @XmAttribute annotated property as an element when a bean serializer modifier is added.
When I register a custom bean serializer modifier with a mapper (version 2.9.6), like so:
final SimpleModule module = new SimpleModule();
module.setSerializerModifier(new CustomSerializerModifier(serializationConfig));
xmlMapper.registerModule(module);
And also register the Jaxby module (or set annotation introspector ... have tried multiple means).
xmlMapper.registerModule(new JaxbAnnotationModule());
I get an output that looks like this:
<XmlAnnotated>
<CustomerAge>0</CustomerAge>
<CustomerId>0</CustomerId>
... other elements
</XmlAnnotated>
instead of the expected:
<XmlAnnotated CustomerAge=\"0\" CustomerId=\"0\">
... other elements
</XmlAnnotated>"
Class defined as:
/**
* XMLAnnotated class for testing serialization.
*/
@XmlRootElement
public class XmlAnnotated {
private String custName;
private int custAge;
private int custId;
private Element anyElement;
/**
* getCustName.
*/
public String getCustName() {
return custName;
}
/**
* getCustname.
*/
@XmlElement(name = "UserName", required = true)
public void setCustName(final String custName) {
this.custName = custName;
}
/**
* getCustAge.
*/
@XmlAttribute(name = "CustomerAge", required = true)
public int getCustAge() {
return custAge;
}
/**
* getCustAge.
*/
@XmlElement
public void setCustAge(final int custAge) {
this.custAge = custAge;
}
/**
* getCustId.
*/
@XmlAttribute(name = "CustomerId", required = true)
public int getCustId() {
return custId;
}
/**
* setCustId.
*/
@XmlAttribute
public void setCustId(final int custId) {
this.custId = custId;
}
/**
* Gets the Any element parent container.
*/
public Element getAnyElement() {
return anyElement;
}
/**
* Set the Any element.
*/
@XmlElement
public void setAnyElement(final Element anyElement) {
this.anyElement = anyElement;
}
}
If I remove the modifier then Jackson behaves as expected. I can see the annotation introspector is set in both cases appropriately but unsure as of yet why adding the modifier prevents the desired behavior.
It looks like it also rewrites the name correctly to the value in the XmlAttribute tag, it's just the transform to an attribute that is missing (from what I can tell).
XML format module adds its own SerializerModifier as it is required to support (optional) wrapping of Collection valued properties. Although modules may add their own modifiers, it is possible modifier you are adding may cause problems there.
So the question is: what does serializer modifier here do?
It's sole purpose is to pass the default serializer through to our custom serializers so that we can choose to call the default serializer when needed. The modifier also selects which obfuscating serializer to use based on bean class (currently a couple of these, one generic Object serializer and one more narrowly scoped ArrayNode serializer) -- but that's a function of having to do so since we are using a modifier of course.
@Activeghost ok. You may want to see what XML module's modifier does, as that likely conflicts with your usage or vice versa. I don't have time to debug this now but I hope this helps point you in right direction.