Created
September 25, 2014 10:24
-
-
Save bhdrk/02307ba8066d26fa1537 to your computer and use it in GitHub Desktop.
Jackson Case Insensitive Enum
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.fasterxml.jackson.databind.BeanDescription; | |
import com.fasterxml.jackson.databind.DeserializationConfig; | |
import com.fasterxml.jackson.databind.JsonDeserializer; | |
import com.fasterxml.jackson.databind.JsonMappingException; | |
import com.fasterxml.jackson.databind.deser.std.EnumDeserializer; | |
import com.fasterxml.jackson.databind.module.SimpleDeserializers; | |
import com.fasterxml.jackson.databind.util.EnumResolver; | |
import java.util.HashMap; | |
import java.util.Map; | |
public class CustomDeserializers extends SimpleDeserializers { | |
@Override | |
@SuppressWarnings("unchecked") | |
public JsonDeserializer<?> findEnumDeserializer(Class<?> type, DeserializationConfig config, BeanDescription beanDesc) throws JsonMappingException { | |
return createDeserializer((Class<Enum>) type); | |
} | |
private <T extends Enum<T>> JsonDeserializer<?> createDeserializer(Class<T> enumCls) { | |
T[] enumValues = enumCls.getEnumConstants(); | |
HashMap<String, T> map = createEnumValuesMap(enumValues); | |
return new EnumDeserializer(new EnumCaseInsensitiveResolver<T>(enumCls, enumValues, map)); | |
} | |
private <T extends Enum<T>> HashMap<String, T> createEnumValuesMap(T[] enumValues) { | |
HashMap<String, T> map = new HashMap<String, T>(); | |
// from last to first, so that in case of duplicate values, first wins | |
for (int i = enumValues.length; --i >= 0; ) { | |
T e = enumValues[i]; | |
map.put(e.toString(), e); | |
} | |
return map; | |
} | |
public static class EnumCaseInsensitiveResolver<T extends Enum<T>> extends EnumResolver<T> { | |
protected EnumCaseInsensitiveResolver(Class<T> enumClass, T[] enums, HashMap<String, T> map) { | |
super(enumClass, enums, map); | |
} | |
@Override | |
public T findEnum(String key) { | |
for (Map.Entry<String, T> entry : _enumsById.entrySet()) { | |
if (entry.getKey().equalsIgnoreCase(key)) { // magic line <-- | |
return entry.getValue(); | |
} | |
} | |
return null; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.fasterxml.jackson.databind.ObjectMapper; | |
import com.fasterxml.jackson.databind.module.SimpleModule; | |
public class JSON { | |
public static void main(String[] args) { | |
SimpleModule enumModule = new SimpleModule(); | |
enumModule.setDeserializers(new CustomDeserializers()); | |
ObjectMapper mapper = new ObjectMapper(); | |
mapper.registerModule(enumModule); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment