com.groupbyinc.common.jackson.databind.deser.std.StdKeyDeserializers Maven / Gradle / Ivy
package com.fasterxml.jackson.databind.deser.std;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.deser.KeyDeserializers;
import com.fasterxml.jackson.databind.introspect.AnnotatedMethod;
import com.fasterxml.jackson.databind.util.ClassUtil;
import com.fasterxml.jackson.databind.util.EnumResolver;
/**
* Helper class used to contain simple/well-known key deserializers.
* Following kinds of Objects can be handled currently:
*
* - Primitive wrappers (Boolean, Byte, Char, Short, Integer, Float, Long, Double)
* - Enums (usually not needed, since EnumMap doesn't call us)
* - {@link java.util.Date}
* - {@link java.util.Calendar}
* - {@link java.util.UUID}
* - {@link java.util.Locale}
* - Anything with constructor that takes a single String arg
* (if not explicitly @JsonIgnore'd)
* - Anything with {@code static T valueOf(String)} factory method
* (if not explicitly @JsonIgnore'd)
*
*/
public class StdKeyDeserializers
implements KeyDeserializers, java.io.Serializable
{
private static final long serialVersionUID = 1L;
public static KeyDeserializer constructEnumKeyDeserializer(EnumResolver enumResolver) {
return new StdKeyDeserializer.EnumKD(enumResolver, null);
}
public static KeyDeserializer constructEnumKeyDeserializer(EnumResolver enumResolver,
AnnotatedMethod factory) {
return new StdKeyDeserializer.EnumKD(enumResolver, factory);
}
public static KeyDeserializer constructDelegatingKeyDeserializer(DeserializationConfig config,
JavaType type, JsonDeserializer> deser)
{
return new StdKeyDeserializer.DelegatingKD(type.getRawClass(), deser);
}
public static KeyDeserializer findStringBasedKeyDeserializer(DeserializationConfig config,
JavaType type)
{
/* We don't need full deserialization information, just need to
* know creators.
*/
BeanDescription beanDesc = config.introspect(type);
// Ok, so: can we find T(String) constructor?
Constructor> ctor = beanDesc.findSingleArgConstructor(String.class);
if (ctor != null) {
if (config.canOverrideAccessModifiers()) {
ClassUtil.checkAndFixAccess(ctor, config.isEnabled(MapperFeature.OVERRIDE_PUBLIC_ACCESS_MODIFIERS));
}
return new StdKeyDeserializer.StringCtorKeyDeserializer(ctor);
}
/* or if not, "static T valueOf(String)" (or equivalent marked
* with @JsonCreator annotation?)
*/
Method m = beanDesc.findFactoryMethod(String.class);
if (m != null){
if (config.canOverrideAccessModifiers()) {
ClassUtil.checkAndFixAccess(m, config.isEnabled(MapperFeature.OVERRIDE_PUBLIC_ACCESS_MODIFIERS));
}
return new StdKeyDeserializer.StringFactoryKeyDeserializer(m);
}
// nope, no such luck...
return null;
}
/*
/**********************************************************
/* KeyDeserializers implementation
/**********************************************************
*/
@Override
public KeyDeserializer findKeyDeserializer(JavaType type,
DeserializationConfig config, BeanDescription beanDesc) throws JsonMappingException
{
Class> raw = type.getRawClass();
// 23-Apr-2013, tatu: Map primitive types, just in case one was given
if (raw.isPrimitive()) {
raw = ClassUtil.wrapperType(raw);
}
return StdKeyDeserializer.forType(raw);
}
}