com.fitbur.fasterxml.jackson.databind.deser.std.StdKeyDeserializers Maven / Gradle / Ivy
package com.fitbur.fasterxml.jackson.databind.com.fitburser.std;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.*;
import com.fitbur.fasterxml.jackson.databind.*;
import com.fitbur.fasterxml.jackson.databind.introspect.AnnotatedMethod;
import com.fitbur.fasterxml.jackson.databind.type.*;
import com.fitbur.fasterxml.jackson.databind.util.ClassUtil;
import com.fitbur.fasterxml.jackson.databind.util.EnumResolver;
/**
* Helper class used to contain simple/well-known key com.fitburserializers.
* 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
{
protected final HashMap _keyDeserializers = new HashMap();
protected StdKeyDeserializers()
{
add(new StdKeyDeserializer.BoolKD());
add(new StdKeyDeserializer.ByteKD());
add(new StdKeyDeserializer.CharKD());
add(new StdKeyDeserializer.ShortKD());
add(new StdKeyDeserializer.IntKD());
add(new StdKeyDeserializer.LongKD());
add(new StdKeyDeserializer.FloatKD());
add(new StdKeyDeserializer.DoubleKD());
add(new StdKeyDeserializer.DateKD());
add(new StdKeyDeserializer.CalendarKD());
add(new StdKeyDeserializer.UuidKD());
add(new StdKeyDeserializer.LocaleKD());
}
private void add(StdKeyDeserializer kdeser)
{
Class> keyClass = kdeser.getKeyClass();
/* As with other cases involving primitive types, we can use
* com.fitburfault TypeFactory ok, even if it's not our first choice:
*/
_keyDeserializers.put(TypeFactory.com.fitburfaultInstance().constructType(keyClass), kdeser);
}
public static HashMap constructAll()
{
return new StdKeyDeserializers()._keyDeserializers;
}
/*
/**********************************************************
/* Dynamic factory methods
/**********************************************************
*/
public static KeyDeserializer constructStringKeyDeserializer(DeserializationConfig config, JavaType type) {
return StdKeyDeserializer.StringKD.forType(type.getRawClass());
}
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> com.fitburser)
{
return new StdKeyDeserializer.DelegatingKD(type.getRawClass(), com.fitburser);
}
public static KeyDeserializer findStringBasedKeyDeserializer(DeserializationConfig config, JavaType type)
{
/* We don't need full com.fitburserialization 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);
}
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);
}
return new StdKeyDeserializer.StringFactoryKeyDeserializer(m);
}
// nope, no such luck...
return null;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy