public class CustomSerializerFactory extends BeanSerializerFactory
BeanSerializerFactory
(and its super class,
BasicSerializerFactory
) are used if no overrides are
defined.
Unlike base serializer factories (BasicSerializerFactory
,
BeanSerializerFactory
), this factory is stateful because
of configuration settings. It is thread-safe, however, as long as
all configuration as done before using the factory -- a single
instance can be shared between providers and mappers.
Configurations currently available are:
Note: as of version 1.7, this class is not as useful as it used to be, due to addition of "modules", which allow simpler addition of custom serializers and deserializers. In fact, use of module system is recommended even when not exposing serializers or deserializers as a pluggable library (just using them locally).
BeanSerializerFactory.ConfigImpl
SerializerFactory.Config
Modifier and Type | Field and Description |
---|---|
protected HashMap<ClassKey,JsonSerializer<?>> |
_directClassMappings
Direct mappings that are only used for exact class type
matches, but not for sub-class checks.
|
protected JsonSerializer<?> |
_enumSerializerOverride
And for Enum handling we may specify a single default
serializer to use, regardless of actual enumeration.
|
protected HashMap<ClassKey,JsonSerializer<?>> |
_interfaceMappings
And finally interface-based matches.
|
protected HashMap<ClassKey,JsonSerializer<?>> |
_transitiveClassMappings
And then class-based mappings that are used both for exact and
sub-class matches.
|
_factoryConfig, instance
_arraySerializers, _concrete, _concreteLazy, optionalHandlers
Constructor and Description |
---|
CustomSerializerFactory() |
CustomSerializerFactory(SerializerFactory.Config config) |
Modifier and Type | Method and Description |
---|---|
protected JsonSerializer<?> |
_findInterfaceMapping(Class<?> cls,
ClassKey key) |
<T> void |
addGenericMapping(Class<? extends T> type,
JsonSerializer<T> ser)
Method used to add a generic (transitive) mapping from specified
class or its sub-classes into a serializer.
|
<T> void |
addSpecificMapping(Class<? extends T> forClass,
JsonSerializer<T> ser)
Method used to add a mapping from specific type -- and only that
type -- to specified serializer.
|
JsonSerializer<Object> |
createSerializer(SerializationConfig config,
JavaType type,
BeanProperty property)
Main serializer constructor method.
|
protected JsonSerializer<?> |
findCustomSerializer(Class<?> type,
SerializationConfig config) |
void |
setEnumSerializer(JsonSerializer<?> enumSer)
Method that can be used to force specified serializer to be used for
serializing all Enum instances.
|
SerializerFactory |
withConfig(SerializerFactory.Config config)
Method used by module registration functionality, to attach additional
serializer providers into this serializer factory.
|
_constructWriter, constructBeanSerializer, constructBeanSerializerBuilder, constructFilteredBeanWriter, constructPropertyBuilder, createKeySerializer, customSerializers, filterBeanProperties, findBeanProperties, findBeanSerializer, findFilterId, findPropertyContentTypeSerializer, findPropertyTypeSerializer, getConfig, isPotentialBeanType, processViews, removeIgnorableTypes, removeSetterlessGetters, sortBeanProperties
buildArraySerializer, buildCollectionLikeSerializer, buildCollectionSerializer, buildContainerSerializer, buildEnumMapSerializer, buildEnumSetSerializer, buildIterableSerializer, buildIteratorSerializer, buildMapLikeSerializer, buildMapSerializer, createTypeSerializer, findContentSerializer, findKeySerializer, findSerializerByAddonType, findSerializerByLookup, findSerializerByPrimaryType, findSerializerFromAnnotation, getNullSerializer, isIndexedList, modifySecondaryTypesByAnnotation, modifyTypeByAnnotation, usesStaticTyping
createSerializer, createTypeSerializer, withAdditionalKeySerializers, withAdditionalSerializers, withSerializerModifier
protected HashMap<ClassKey,JsonSerializer<?>> _directClassMappings
protected JsonSerializer<?> _enumSerializerOverride
protected HashMap<ClassKey,JsonSerializer<?>> _transitiveClassMappings
protected HashMap<ClassKey,JsonSerializer<?>> _interfaceMappings
public CustomSerializerFactory()
public CustomSerializerFactory(SerializerFactory.Config config)
public SerializerFactory withConfig(SerializerFactory.Config config)
BeanSerializerFactory
withConfig
in class BeanSerializerFactory
public <T> void addGenericMapping(Class<? extends T> type, JsonSerializer<T> ser)
Class.getInterfaces()
), then super-type's and so forth.
Note that adding generic mappings may lead to problems with sub-classing: if sub-classes add new properties, these may not get properly serialized.
type
- Class for which specified serializer is to be
used. May be more specific type than what serializer indicates,
but must be compatible (same or sub-class)public <T> void addSpecificMapping(Class<? extends T> forClass, JsonSerializer<T> ser)
IllegalArgumentException
will be thrown to
indicate caller error.forClass
- Class for which specified serializer is to be
used. May be more specific type than what serializer indicates,
but must be compatible (same or sub-class)public void setEnumSerializer(JsonSerializer<?> enumSer)
enum.toString()
, or modify
value returned by enum.name()
(such as upper- or
lower-casing it).
Note: this serializer has lower precedence than that of specific types; so if a specific serializer is assigned to an Enum type, this serializer will NOT be used. It has higher precedence than generic mappings have however.
public JsonSerializer<Object> createSerializer(SerializationConfig config, JavaType type, BeanProperty property) throws JsonMappingException
BeanSerializerFactory
Iterable
.
Note: sub-classes may choose to complete replace implementation, if they want to alter priority of serializer lookups.
createSerializer
in class BeanSerializerFactory
JsonMappingException
protected JsonSerializer<?> findCustomSerializer(Class<?> type, SerializationConfig config)
protected JsonSerializer<?> _findInterfaceMapping(Class<?> cls, ClassKey key)