org.codehaus.jackson.map.ser
Class CustomSerializerFactory

java.lang.Object
  extended by org.codehaus.jackson.map.SerializerFactory
      extended by org.codehaus.jackson.map.ser.BasicSerializerFactory
          extended by org.codehaus.jackson.map.ser.BeanSerializerFactory
              extended by org.codehaus.jackson.map.ser.CustomSerializerFactory

public class CustomSerializerFactory
extends BeanSerializerFactory

Serializer factory implementation that allows for configuring mapping between types (classes) and serializers to use, by using multiple types of overrides. Existing mappings established by 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:

In near future, following features are planned to be added:


Field Summary
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.
 
Fields inherited from class org.codehaus.jackson.map.ser.BeanSerializerFactory
instance
 
Fields inherited from class org.codehaus.jackson.map.ser.BasicSerializerFactory
_concrete, _concreteLazy, optionalHandlers
 
Constructor Summary
CustomSerializerFactory()
           
 
Method Summary
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.
<T> JsonSerializer<T>
createSerializer(Class<T> type, SerializationConfig config)
          Main serializer constructor method.
 JsonSerializer<Object> createSerializer(JavaType type, SerializationConfig config)
          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.
 
Methods inherited from class org.codehaus.jackson.map.ser.BeanSerializerFactory
_constructWriter, _sortBeanProperties, constructBeanSerializer, constructFilteredBeanWriter, constructPropertyBuilder, filterBeanProperties, findBeanProperties, findBeanSerializer, findPropertyContentTypeSerializer, findPropertyTypeSerializer, isPotentialBeanType, processViews, sortBeanProperties
 
Methods inherited from class org.codehaus.jackson.map.ser.BasicSerializerFactory
buildCollectionSerializer, buildEnumMapSerializer, buildEnumSetSerializer, buildIndexedListSerializer, buildIterableSerializer, buildIteratorSerializer, buildMapSerializer, buildObjectArraySerializer, createTypeSerializer, findSerializerByAddonType, findSerializerByLookup, findSerializerByPrimaryType, findSerializerFromAnnotation, getNullSerializer, usesStaticTyping
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_directClassMappings

protected HashMap<ClassKey,JsonSerializer<?>> _directClassMappings
Direct mappings that are only used for exact class type matches, but not for sub-class checks.


_enumSerializerOverride

protected JsonSerializer<?> _enumSerializerOverride
And for Enum handling we may specify a single default serializer to use, regardless of actual enumeration. Usually used to provide "toString - serializer".


_transitiveClassMappings

protected HashMap<ClassKey,JsonSerializer<?>> _transitiveClassMappings
And then class-based mappings that are used both for exact and sub-class matches.


_interfaceMappings

protected HashMap<ClassKey,JsonSerializer<?>> _interfaceMappings
And finally interface-based matches.

Constructor Detail

CustomSerializerFactory

public CustomSerializerFactory()
Method Detail

addGenericMapping

public <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. When resolving a type into a serializer, explicit class is checked first, then immediate super-class, and so forth along inheritance chain. But if this fails, implemented interfaces are checked; ordering is done such that first interfaces implemented by the exact type are checked (in order returned by 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.

Parameters:
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)

addSpecificMapping

public <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. This means that binding is not used for sub-types. It also means that no such mappings are to be defined for abstract classes or interfaces: and if an attempt is made, IllegalArgumentException will be thrown to indicate caller error.

Parameters:
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)

setEnumSerializer

public void setEnumSerializer(JsonSerializer<?> enumSer)
Method that can be used to force specified serializer to be used for serializing all Enum instances. This is most commonly used to specify serializers that call either 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.


createSerializer

public <T> JsonSerializer<T> createSerializer(Class<T> type,
                                              SerializationConfig config)
Description copied from class: BasicSerializerFactory
Main serializer constructor method. The base implementation within this class first calls a fast lookup method that can find serializers for well-known JDK classes; and if that fails, a slower one that tries to check out which interfaces given Class implements. Sub-classes can (and do) change this behavior to alter behavior.

Overrides:
createSerializer in class BasicSerializerFactory
Parameters:
type - Type to be serialized
config - Generic serialization configuration

createSerializer

public JsonSerializer<Object> createSerializer(JavaType type,
                                               SerializationConfig config)
Description copied from class: BeanSerializerFactory
Main serializer constructor method. We will have to be careful with respect to ordering of various method calls: essentially we want to reliably figure out which classes are standard types, and which are beans. The problem is that some bean Classes may implement standard interfaces (say, Iterable.

Note: sub-classes may choose to complete replace implementation, if they want to alter priority of serializer lookups.

Overrides:
createSerializer in class BeanSerializerFactory
Parameters:
type - Type to be serialized
config - Generic serialization configuration

findCustomSerializer

protected JsonSerializer<?> findCustomSerializer(Class<?> type,
                                                 SerializationConfig config)

_findInterfaceMapping

protected JsonSerializer<?> _findInterfaceMapping(Class<?> cls,
                                                  ClassKey key)