package com.fasterxml.jackson.databind.module; import java.util.Collection; import java.util.HashMap; import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import com.fasterxml.jackson.core.Version; import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.databind.deser.BeanDeserializerModifier; import com.fasterxml.jackson.databind.deser.ValueInstantiator; import com.fasterxml.jackson.databind.jsontype.NamedType; import com.fasterxml.jackson.databind.ser.BeanSerializerModifier; /** * Vanilla {@link Module} implementation that allows registration * of serializers and deserializers, bean serializer * and deserializer modifiers, registration of subtypes and mix-ins * as well as some other commonly * needed aspects (addition of custom {@link AbstractTypeResolver}s, * {@link com.fasterxml.jackson.databind.deser.ValueInstantiator}s). *
* NOTE: although it is not expected that sub-types should need to * override {@link #setupModule(SetupContext)} method, if they choose * to do so they MUST call {@code super.setupModule(context);} * to ensure that registration works as expected. *
* WARNING: when registering {@link JsonSerializer}s and {@link JsonDeserializer}s,
* only type erased {@code Class} is compared: this means that usually you should
* NOT use this implementation for registering structured types such as
* {@link java.util.Collection}s or {@link java.util.Map}s: this because parametric
* type information will not be considered and you may end up having "wrong" handler
* for your type.
* What you need to do, instead, is to implement {@link com.fasterxml.jackson.databind.deser.Deserializers}
* and/or {@link com.fasterxml.jackson.databind.ser.Serializers} callbacks to match full type
* signatures (with {@link JavaType}).
*/
public class SimpleModule
extends com.fasterxml.jackson.databind.Module
implements java.io.Serializable
{
private static final long serialVersionUID = 1L; // 2.5.0
protected final String _name;
protected final Version _version;
protected SimpleSerializers _serializers = null;
protected SimpleDeserializers _deserializers = null;
protected SimpleSerializers _keySerializers = null;
protected SimpleKeyDeserializers _keyDeserializers = null;
/**
* Lazily-constructed resolver used for storing mappings from
* abstract classes to more specific implementing classes
* (which may be abstract or concrete)
*/
protected SimpleAbstractTypeResolver _abstractTypes = null;
/**
* Lazily-constructed resolver used for storing mappings from
* abstract classes to more specific implementing classes
* (which may be abstract or concrete)
*/
protected SimpleValueInstantiators _valueInstantiators = null;
/**
* @since 2.2
*/
protected BeanDeserializerModifier _deserializerModifier = null;
/**
* @since 2.2
*/
protected BeanSerializerModifier _serializerModifier = null;
/**
* Lazily-constructed map that contains mix-in definitions, indexed
* by target class, value being mix-in to apply.
*/
protected HashMap
* WARNING! Type matching only uses type-erased {@code Class} and should NOT
* be used when registering serializers for generic types like
* {@link java.util.Collection} and {@link java.util.Map}.
*/
public SimpleModule addSerializer(JsonSerializer> ser)
{
_checkNotNull(ser, "serializer");
if (_serializers == null) {
_serializers = new SimpleSerializers();
}
_serializers.addSerializer(ser);
return this;
}
/**
* Method for adding serializer to handle values of specific type.
*
* WARNING! Type matching only uses type-erased {@code Class} and should NOT
* be used when registering serializers for generic types like
* {@link java.util.Collection} and {@link java.util.Map}.
*/
public
* WARNING! Type matching only uses type-erased {@code Class} and should NOT
* be used when registering serializers for generic types like
* {@link java.util.Collection} and {@link java.util.Map}.
*/
public
* Instantiator is
* registered when module is registered for
* Mix-in annotations are
* registered when module is registered for null
if (but only if!) this class is directly instantiated;
* but class name (default impl) for sub-classes.
*/
@Override
public Object getTypeId() {
if (getClass() == SimpleModule.class) {
return null;
}
return super.getTypeId();
}
/*
/**********************************************************
/* Simple setters to allow overriding
/**********************************************************
*/
/**
* Resets all currently configured serializers.
*/
public void setSerializers(SimpleSerializers s) {
_serializers = s;
}
/**
* Resets all currently configured deserializers.
*/
public void setDeserializers(SimpleDeserializers d) {
_deserializers = d;
}
/**
* Resets all currently configured key serializers.
*/
public void setKeySerializers(SimpleSerializers ks) {
_keySerializers = ks;
}
/**
* Resets all currently configured key deserializers.
*/
public void setKeyDeserializers(SimpleKeyDeserializers kd) {
_keyDeserializers = kd;
}
/**
* Resets currently configured abstract type mappings
*/
public void setAbstractTypes(SimpleAbstractTypeResolver atr) {
_abstractTypes = atr;
}
/**
* Resets all currently configured value instantiators
*/
public void setValueInstantiators(SimpleValueInstantiators svi) {
_valueInstantiators = svi;
}
/**
* @since 2.2
*/
public SimpleModule setDeserializerModifier(BeanDeserializerModifier mod) {
_deserializerModifier = mod;
return this;
}
/**
* @since 2.2
*/
public SimpleModule setSerializerModifier(BeanSerializerModifier mod) {
_serializerModifier = mod;
return this;
}
/**
* @since 2.3
*/
protected SimpleModule setNamingStrategy(PropertyNamingStrategy naming) {
_namingStrategy = naming;
return this;
}
/*
/**********************************************************
/* Configuration methods, adding serializers
/**********************************************************
*/
/**
* Method for adding serializer to handle type that the serializer claims to handle
* (see {@link JsonSerializer#handledType()}).
*beanType
.
*ObjectMapper
.
*/
public SimpleModule addValueInstantiator(Class> beanType, ValueInstantiator inst)
{
_checkNotNull(beanType, "class to register value instantiator for");
_checkNotNull(inst, "value instantiator");
if (_valueInstantiators == null) {
_valueInstantiators = new SimpleValueInstantiators();
}
_valueInstantiators = _valueInstantiators.addValueInstantiator(beanType, inst);
return this;
}
/**
* Method for specifying that annotations define by mixinClass
* should be "mixed in" with annotations that targetType
* has (as if they were directly included on it!).
*ObjectMapper
.
*/
public SimpleModule setMixInAnnotation(Class> targetType, Class> mixinClass)
{
_checkNotNull(targetType, "target type");
_checkNotNull(mixinClass, "mixin class");
if (_mixins == null) {
_mixins = new HashMap