jackson-dataformat-xml
jackson-dataformat-xml copied to clipboard
Problem with Enums, `@JsonTypeInfo` (and indirectly, `As.WRAPPER_ARRAY`)
(from Jackson user forum by Rich M)
Hi all. I am using jackson 1.7.1 and xml-databind 0.5.3, and ran into a minor issue. I have an attribute of type Object, which is unavoidable because its value can be a primitive or an Enum. I have tagged the attribute with @JsonTypeInfo. Everything works fine in JSON, however, the xml is erroneous when working with an Enum:
public class MyClass {
@JsonTypeInfo(use=JsonTypeInfo.Id.MINIMAL_CLASS, include=JsonTypeInfo.As.PROPERTY, property="__type")
public Object value;
}
The JSON fragment for an Enum is:
"value" : [ "PersonBloodTypeCode", "ABMNUS" ]
The XML fragment does not nest properly. It looks like:
<value>PersonBloodTypeCode</value>
<value>ABMNUS</value>
Which, when deserialized, results in a String instance "ABMNUS", rather than an enum.
Quick note on work-around: using "JsonTypeInfo.As.WRAPPER_OBJECT" works -- this might actually just be a general problem with JsonTypeInfo with As.PROPERTY and As.WRAPPER_ARRAY inclusion methods.
Ok, so part of the problem is that enums are serialized as scalar value (string), and thus As.PROPERTY is effectively same as As.WRAPPER_ARRAY; and that choice does not work for any type yet.
That is: basically same as #4.