I'm currently working on a scenario where I need to map a String to an enum Object using the Jackson ObjectMapper.readValue(String,Class) API. The issue arises when my JSON string contains a Task Object with an Action enum defined as follows:
public enum Action {
@XmlEnumValue("Add")
ADD("Add"),
@XmlEnumValue("Amend")
AMEND("Amend"),
@XmlEnumValue("Delete")
DELETE("Delete"),
@XmlEnumValue("Pending")
PENDING("Pending");
private final String value;
Action(String v) {
value = v;
}
public String value() {
return value;
}
public static Action fromValue(String v) {
for (Action c: Action.values()) {
if (c.value.equals(v)) {
return c;
}
}
throw new IllegalArgumentException(v);
}
}
When the JSON string looks like this "{"action":"Add"}", calling ObjectMapper.readValue(jsonString, Task.Class) results in org.codehaus.jackson.map.deser.StdDeserializationContext.weirdStringException(StdDeserializationContext.java:243) for the Action "Add" because it cannot convert this Enum properly.
I attempted to add a custom Deserializer, but the EnumDeserializer is still being called regardless. Does anyone have any suggestions or ideas?
All objects are generated by JAXB, so annotations are not an option in this case.
Thank you for your assistance!