package com.fasterxml.jackson.databind.module;
import java.util.*;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.deser.Deserializers;
import com.fasterxml.jackson.databind.jsontype.TypeDeserializer;
import com.fasterxml.jackson.databind.type.*;
public class SimpleDeserializers
extends Deserializers.Base
implements java.io.Serializable
{
private static final long serialVersionUID = 1L;
protected HashMap<ClassKey,JsonDeserializer<?>> _classMappings = null;
protected boolean _hasEnumDeserializer = false;
public SimpleDeserializers() { }
public SimpleDeserializers(Map<Class<?>,JsonDeserializer<?>> desers) {
addDeserializers(desers);
}
public <T> void addDeserializer(Class<T> forClass, JsonDeserializer<? extends T> deser)
{
ClassKey key = new ClassKey(forClass);
if (_classMappings == null) {
_classMappings = new HashMap<ClassKey,JsonDeserializer<?>>();
}
_classMappings.put(key, deser);
if (forClass == Enum.class) {
_hasEnumDeserializer = true;
}
}
@SuppressWarnings("unchecked")
public void addDeserializers(Map<Class<?>,JsonDeserializer<?>> desers)
{
for (Map.Entry<Class<?>,JsonDeserializer<?>> entry : desers.entrySet()) {
Class<?> cls = entry.getKey();
JsonDeserializer<Object> deser = (JsonDeserializer<Object>) entry.getValue();
addDeserializer((Class<Object>) cls, deser);
}
}
@Override
public JsonDeserializer<?> findArrayDeserializer(ArrayType type,
DeserializationConfig config, BeanDescription beanDesc,
TypeDeserializer elementTypeDeserializer, JsonDeserializer<?> elementDeserializer)
throws JsonMappingException
{
return _find(type);
}
@Override
public JsonDeserializer<?> findBeanDeserializer(JavaType type,
DeserializationConfig config, BeanDescription beanDesc)
throws JsonMappingException
{
return _find(type);
}
@Override
public JsonDeserializer<?> findCollectionDeserializer(CollectionType type,
DeserializationConfig config, BeanDescription beanDesc,
TypeDeserializer elementTypeDeserializer,
JsonDeserializer<?> elementDeserializer)
throws JsonMappingException
{
return _find(type);
}
@Override
public JsonDeserializer<?> findCollectionLikeDeserializer(CollectionLikeType type,
DeserializationConfig config, BeanDescription beanDesc,
TypeDeserializer elementTypeDeserializer,
JsonDeserializer<?> elementDeserializer)
throws JsonMappingException
{
return _find(type);
}
@Override
public JsonDeserializer<?> findEnumDeserializer(Class<?> type,
DeserializationConfig config, BeanDescription beanDesc)
throws JsonMappingException
{
if (_classMappings == null) {
return null;
}
JsonDeserializer<?> deser = _classMappings.get(new ClassKey(type));
if (deser == null) {
if (_hasEnumDeserializer && type.isEnum()) {
deser = _classMappings.get(new ClassKey(Enum.class));
}
}
return deser;
}
@Override
public JsonDeserializer<?> findTreeNodeDeserializer(Class<? extends JsonNode> nodeType,
DeserializationConfig config, BeanDescription beanDesc)
throws JsonMappingException
{
if (_classMappings == null) {
return null;
}
return _classMappings.get(new ClassKey(nodeType));
}
@Override
public JsonDeserializer<?> findReferenceDeserializer(ReferenceType refType,
DeserializationConfig config, BeanDescription beanDesc,
TypeDeserializer contentTypeDeserializer, JsonDeserializer<?> contentDeserializer)
throws JsonMappingException {
return _find(refType);
}
@Override
public JsonDeserializer<?> findMapDeserializer(MapType type,
DeserializationConfig config, BeanDescription beanDesc,
KeyDeserializer keyDeserializer,
TypeDeserializer elementTypeDeserializer,
JsonDeserializer<?> elementDeserializer)
throws JsonMappingException
{
return _find(type);
}
@Override
public JsonDeserializer<?> findMapLikeDeserializer(MapLikeType type,
DeserializationConfig config, BeanDescription beanDesc,
KeyDeserializer keyDeserializer,
TypeDeserializer elementTypeDeserializer,
JsonDeserializer<?> elementDeserializer)
throws JsonMappingException
{
return _find(type);
}
@Override
public boolean hasDeserializerFor(DeserializationConfig config,
Class<?> valueType) {
return (_classMappings != null)
&& _classMappings.containsKey(new ClassKey(valueType));
}
private final JsonDeserializer<?> _find(JavaType type) {
if (_classMappings == null) {
return null;
}
return _classMappings.get(new ClassKey(type.getRawClass()));
}
}