I have an Enum marked with @JsonProperty for JSON serialization/deserialization with Jackson and would like to get the enum value for a given String JsonProperty:
The desired result can be achieved through the following method:
public static <T extends Enum<T>> T getEnumValueFromJsonProperty(Class<T> enumClass, String jsonPropertyValue) {
Field[] fields = enumClass.getFields();
for (int i=0; i<fields.length; i++) {
if (fields[i].getAnnotation(JsonProperty.class).value().equals(jsonPropertyValue)) {
return Enum.valueOf(enumClass, fields[i].getName());
}
}
return null;
}