Change in handling of enum when moving from 2.6 to 2.7

534 views
Skip to first unread message

Jan Stenberg

unread,
Jun 15, 2016, 2:15:38 PM6/15/16
to jackson-user
I have an enum in Java as this:

@XmlEnum(String.class)
public enum BoxTypeEventPart {
@XmlEnumValue("EDUCATION")
EDUCATIONBOX(),

@XmlEnumValue("INSTANCE")
INSTANCEBOX(),

@XmlEnumValue("SELECTION")
SELECTIONBOX()
}

When I serialize an object containing the enum value BoxTypeEventPart.INSTANCEBOX using Jackson 2.6.7 the string will contain" "INSTANCEBOX"
When I change to 2.7.4 the string will instead contain the value from @XmlEnumValue: "INSTANCE"

Is this an intentional change in Jackson?

If I add a  @JsonProperty I'm getting the same serialization in 2.7.4 as in 2.6.7
@XmlEnumValue("INSTANCE")
@JsonProperty(value = "INSTANCEBOX")
INSTANCEBOX()

Is that a correct way to solve my problem?

Thanks,
Jan

Tatu Saloranta

unread,
Jun 15, 2016, 2:32:28 PM6/15/16
to jackson-user
Since `XmlEnumValue` is a JAXB annotation, it is used if (and only if) JAXB module is included.
`@JsonProperty` is applied through standard `jackson-databind`. Precedence of annotations depends on how `AnnotationIntrospector`s are registered (which has precedence). I assume that you have JAXB module and introspector specified as the primary one, with highest precedence.

As to change: I can not think of explicit change, but it is possible that changes made to resolve enum values more efficiently lead to the change. I think 2.7 behavior is correct one, assuming you have JAXB module registered; 2.6 appears to have ignored annotation. Exactly why this is the case is unclear as I do not see resolved issue for 2.7.x, and since 2.6 did have handling for `@XmlEnumValue`.
But I think it has come about due to cleanup of enum resolution code which did occur for 2.7.0.

Use of `@JsonProperty` is appropriate, if you do not like serialization defined by `@XmlEnumValue`. However, wouldn't it be easiest to just remove `@XmlEnumValue` annotation? In absence of annotations (and with default settings), enum's `name` is used as the serialization.

I hope this helps,

-+ Tatu +-


--
You received this message because you are subscribed to the Google Groups "jackson-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jackson-user...@googlegroups.com.
To post to this group, send email to jackso...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jan Stenberg

unread,
Jun 15, 2016, 3:40:17 PM6/15/16
to jackson-user
We are using the same representation objects both for XML and JSON, hence the `@XmlEnumValue` annotation.

Thanks Tatu, a great answer that will help us resolve this issue. '@JsonProperty' seems to be the correct solution and it works as my tests show.

Jan
Reply all
Reply to author
Forward
0 new messages