Class SimpleModule
- java.lang.Object
-
- com.fasterxml.jackson.databind.Module
-
- com.fasterxml.jackson.databind.module.SimpleModule
-
- All Implemented Interfaces:
Versioned,java.io.Serializable
- Direct Known Subclasses:
JacksonXmlModule
public class SimpleModule extends Module implements java.io.Serializable
VanillaModuleimplementation 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 customAbstractTypeResolvers,ValueInstantiators).NOTE: that [de]serializers are registered as "default" [de]serializers. As a result, they will have lower priority than the ones indicated through annotations on both Class and property-associated annotations -- for example,
JsonDeserialize.
In cases where both module-based [de]serializers and annotation-based [de]serializers are registered, the [de]serializer specified by the annotation will take precedence.NOTE: although it is not expected that sub-types should need to override
setupModule(SetupContext)method, if they choose to do so they MUST callsuper.setupModule(context);to ensure that registration works as expected.WARNING: when registering
JsonSerializers andJsonDeserializers, only type erasedClassis compared: this means that usually you should NOT use this implementation for registering structured types such asCollections orMaps: 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 implementDeserializersand/orSerializerscallbacks to match full type signatures (withJavaType).- See Also:
- Serialized Form
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class com.fasterxml.jackson.databind.Module
Module.SetupContext
-
-
Constructor Summary
Constructors Constructor Description SimpleModule()Constructors that should only be used for non-reusable convenience modules used by app code: "real" modules should use actual name and version number information.SimpleModule(Version version)Convenience constructor that will use specified Version, including name fromVersion.getArtifactId().SimpleModule(java.lang.String name)Convenience constructor that will default version toVersion.unknownVersion().SimpleModule(java.lang.String name, Version version)Constructor to use for actual reusable modules.SimpleModule(java.lang.String name, Version version, java.util.List<JsonSerializer<?>> serializers)SimpleModule(java.lang.String name, Version version, java.util.Map<java.lang.Class<?>,JsonDeserializer<?>> deserializers)SimpleModule(java.lang.String name, Version version, java.util.Map<java.lang.Class<?>,JsonDeserializer<?>> deserializers, java.util.List<JsonSerializer<?>> serializers)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <T> SimpleModuleaddAbstractTypeMapping(java.lang.Class<T> superType, java.lang.Class<? extends T> subType)Lazily-constructed resolver used for storing mappings from abstract classes to more specific implementing classes (which may be abstract or concrete)<T> SimpleModuleaddDeserializer(java.lang.Class<T> type, JsonDeserializer<? extends T> deser)Method for adding deserializer to handle specified type.SimpleModuleaddKeyDeserializer(java.lang.Class<?> type, KeyDeserializer deser)NOTE: This method registers "default" (de)serializers only.<T> SimpleModuleaddKeySerializer(java.lang.Class<? extends T> type, JsonSerializer<T> ser)NOTE: This method registers "default" (de)serializers only.SimpleModuleaddSerializer(JsonSerializer<?> ser)Method for adding serializer to handle type that the serializer claims to handle (seeJsonSerializer.handledType()).<T> SimpleModuleaddSerializer(java.lang.Class<? extends T> type, JsonSerializer<T> ser)Method for adding serializer to handle values of specific type.SimpleModuleaddValueInstantiator(java.lang.Class<?> beanType, ValueInstantiator inst)Method for registeringValueInstantiatorto use when deserializing instances of typebeanType.java.lang.StringgetModuleName()Method that returns a display that can be used by Jackson for informational purposes, as well as in associating extensions with module that provides them.java.lang.ObjectgetTypeId()Since instances are likely to be custom, implementation returnsnullif (but only if!) this class is directly instantiated; but class name (default impl) for sub-classes.SimpleModuleregisterSubtypes(NamedType... subtypes)Method for adding set of subtypes (along with type name to use) to be registered withObjectMapperthis is an alternative to using annotations in super type to indicate subtypes.SimpleModuleregisterSubtypes(java.lang.Class<?>... subtypes)Method for adding set of subtypes to be registered withObjectMapperthis is an alternative to using annotations in super type to indicate subtypes.SimpleModuleregisterSubtypes(java.util.Collection<java.lang.Class<?>> subtypes)Method for adding set of subtypes (along with type name to use) to be registered withObjectMapperthis is an alternative to using annotations in super type to indicate subtypes.voidsetAbstractTypes(SimpleAbstractTypeResolver atr)Resets currently configured abstract type mappingsSimpleModulesetDeserializerModifier(BeanDeserializerModifier mod)voidsetDeserializers(SimpleDeserializers d)Resets all currently configured deserializers.voidsetKeyDeserializers(SimpleKeyDeserializers kd)Resets all currently configured key deserializers.voidsetKeySerializers(SimpleSerializers ks)Resets all currently configured key serializers.SimpleModulesetMixInAnnotation(java.lang.Class<?> targetType, java.lang.Class<?> mixinClass)Method for specifying that annotations define bymixinClassshould be "mixed in" with annotations thattargetTypehas (as if they were directly included on it!).SimpleModulesetSerializerModifier(BeanSerializerModifier mod)voidsetSerializers(SimpleSerializers s)Resets all currently configured serializers.voidsetupModule(Module.SetupContext context)Standard implementation handles registration of all configured customizations: it is important that sub-classes call this implementation (usually before additional custom logic) if they choose to override it; otherwise customizations will not be registered.voidsetValueInstantiators(SimpleValueInstantiators svi)Resets all currently configured value instantiatorsVersionversion()Method that returns version of this module.-
Methods inherited from class com.fasterxml.jackson.databind.Module
getDependencies
-
-
-
-
Constructor Detail
-
SimpleModule
public SimpleModule()
Constructors that should only be used for non-reusable convenience modules used by app code: "real" modules should use actual name and version number information.
-
SimpleModule
public SimpleModule(java.lang.String name)
Convenience constructor that will default version toVersion.unknownVersion().
-
SimpleModule
public SimpleModule(Version version)
Convenience constructor that will use specified Version, including name fromVersion.getArtifactId().
-
SimpleModule
public SimpleModule(java.lang.String name, Version version)Constructor to use for actual reusable modules. ObjectMapper may use name as identifier to notice attempts for multiple registrations of the same module (although it does not have to).- Parameters:
name- Unique name of the moduleversion- Version of the module
-
SimpleModule
public SimpleModule(java.lang.String name, Version version, java.util.Map<java.lang.Class<?>,JsonDeserializer<?>> deserializers)- Since:
- 2.1
-
SimpleModule
public SimpleModule(java.lang.String name, Version version, java.util.List<JsonSerializer<?>> serializers)- Since:
- 2.1
-
SimpleModule
public SimpleModule(java.lang.String name, Version version, java.util.Map<java.lang.Class<?>,JsonDeserializer<?>> deserializers, java.util.List<JsonSerializer<?>> serializers)- Since:
- 2.1
-
-
Method Detail
-
getTypeId
public java.lang.Object getTypeId()
Since instances are likely to be custom, implementation returnsnullif (but only if!) this class is directly instantiated; but class name (default impl) for sub-classes.
-
setSerializers
public void setSerializers(SimpleSerializers s)
Resets all currently configured serializers.
-
setDeserializers
public void setDeserializers(SimpleDeserializers d)
Resets all currently configured deserializers.
-
setKeySerializers
public void setKeySerializers(SimpleSerializers ks)
Resets all currently configured key serializers.
-
setKeyDeserializers
public void setKeyDeserializers(SimpleKeyDeserializers kd)
Resets all currently configured key deserializers.
-
setAbstractTypes
public void setAbstractTypes(SimpleAbstractTypeResolver atr)
Resets currently configured abstract type mappings
-
setValueInstantiators
public void setValueInstantiators(SimpleValueInstantiators svi)
Resets all currently configured value instantiators
-
setDeserializerModifier
public SimpleModule setDeserializerModifier(BeanDeserializerModifier mod)
- Since:
- 2.2
-
setSerializerModifier
public SimpleModule setSerializerModifier(BeanSerializerModifier mod)
- Since:
- 2.2
-
addSerializer
public SimpleModule addSerializer(JsonSerializer<?> ser)
Method for adding serializer to handle type that the serializer claims to handle (seeJsonSerializer.handledType()).WARNING! Type matching only uses type-erased
Classand should NOT be used when registering serializers for generic types likeCollectionandMap.WARNING! "Last one wins" rule is applied. Possible earlier addition of a serializer for a given Class will be replaced.
NOTE: This method registers "default" (de)serializers only. See a note on precedence in class JavaDoc.
-
addSerializer
public <T> SimpleModule addSerializer(java.lang.Class<? extends T> type, JsonSerializer<T> ser)
Method for adding serializer to handle values of specific type.NOTE: This method registers "default" (de)serializers only. See a note on precedence in class JavaDoc.
WARNING! Type matching only uses type-erased
Classand should NOT be used when registering serializers for generic types likeCollectionandMap.WARNING! "Last one wins" rule is applied. Possible earlier addition of a serializer for a given Class will be replaced.
NOTE: This method registers "default" (de)serializers only. See a note on precedence in class JavaDoc.
-
addKeySerializer
public <T> SimpleModule addKeySerializer(java.lang.Class<? extends T> type, JsonSerializer<T> ser)
NOTE: This method registers "default" (de)serializers only. See a note on precedence in class JavaDoc.
-
addDeserializer
public <T> SimpleModule addDeserializer(java.lang.Class<T> type, JsonDeserializer<? extends T> deser)
Method for adding deserializer to handle specified type.WARNING! Type matching only uses type-erased
Classand should NOT be used when registering serializers for generic types likeCollectionandMap.WARNING! "Last one wins" rule is applied. Possible earlier addition of a serializer for a given Class will be replaced.
NOTE: This method registers "default" (de)serializers only. See a note on precedence in class JavaDoc.
-
addKeyDeserializer
public SimpleModule addKeyDeserializer(java.lang.Class<?> type, KeyDeserializer deser)
NOTE: This method registers "default" (de)serializers only. See a note on precedence in class JavaDoc.
-
addAbstractTypeMapping
public <T> SimpleModule addAbstractTypeMapping(java.lang.Class<T> superType, java.lang.Class<? extends T> subType)
Lazily-constructed resolver used for storing mappings from abstract classes to more specific implementing classes (which may be abstract or concrete)
-
registerSubtypes
public SimpleModule registerSubtypes(java.lang.Class<?>... subtypes)
Method for adding set of subtypes to be registered withObjectMapperthis is an alternative to using annotations in super type to indicate subtypes.
-
registerSubtypes
public SimpleModule registerSubtypes(NamedType... subtypes)
Method for adding set of subtypes (along with type name to use) to be registered withObjectMapperthis is an alternative to using annotations in super type to indicate subtypes.
-
registerSubtypes
public SimpleModule registerSubtypes(java.util.Collection<java.lang.Class<?>> subtypes)
Method for adding set of subtypes (along with type name to use) to be registered withObjectMapperthis is an alternative to using annotations in super type to indicate subtypes.- Since:
- 2.9
-
addValueInstantiator
public SimpleModule addValueInstantiator(java.lang.Class<?> beanType, ValueInstantiator inst)
Method for registeringValueInstantiatorto use when deserializing instances of typebeanType.Instantiator is registered when module is registered for
ObjectMapper.
-
setMixInAnnotation
public SimpleModule setMixInAnnotation(java.lang.Class<?> targetType, java.lang.Class<?> mixinClass)
Method for specifying that annotations define bymixinClassshould be "mixed in" with annotations thattargetTypehas (as if they were directly included on it!).Mix-in annotations are registered when module is registered for
ObjectMapper.
-
getModuleName
public java.lang.String getModuleName()
Description copied from class:ModuleMethod that returns a display that can be used by Jackson for informational purposes, as well as in associating extensions with module that provides them.- Specified by:
getModuleNamein classModule
-
setupModule
public void setupModule(Module.SetupContext context)
Standard implementation handles registration of all configured customizations: it is important that sub-classes call this implementation (usually before additional custom logic) if they choose to override it; otherwise customizations will not be registered.- Specified by:
setupModulein classModule
-
-