org.codehaus.jackson.map.ser
Class BeanSerializerFactory

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
Direct Known Subclasses:
CustomSerializerFactory

public class BeanSerializerFactory
extends BasicSerializerFactory

Factory class that can provide serializers for any regular Java beans (as defined by "having at least one get method recognizable as bean accessor" -- where Object.getClass() does not count); as well as for "standard" JDK types. Latter is achieved by delegating calls to BasicSerializerFactory to find serializers both for "standard" JDK types (and in some cases, sub-classes as is the case for collection classes like Lists and Maps) and bean (value) classes.

Note about delegating calls to BasicSerializerFactory: although it would be nicer to use linear delegation for construction (to essentially dispatch all calls first to the underlying BasicSerializerFactory; or alternatively after failing to provide bean-based serializer}, there is a problem: priority levels for detecting standard types are mixed. That is, we want to check if a type is a bean after some of "standard" JDK types, but before the rest. As a result, "mixed" delegation used, and calls are NOT done using regular SerializerFactory interface but rather via direct calls to BasicSerializerFactory.

Finally, since all caching is handled by the serializer provider (not factory) and there is no configurability, this factory is stateless. This means that a global singleton instance can be used.

Notes for version 1.7 (and above): the new module registration system required addition of withConfig(org.codehaus.jackson.map.SerializerFactory.Config), which has to be redefined by sub-classes so that they can work properly with pluggable additional serializer providing components.


Nested Class Summary
static class BeanSerializerFactory.ConfigImpl
          Configuration settings container class for bean serializer factory
 
Nested classes/interfaces inherited from class org.codehaus.jackson.map.SerializerFactory
SerializerFactory.Config
 
Field Summary
protected  SerializerFactory.Config _factoryConfig
          Configuration settings for this factory; immutable instance (just like this factory), new version created via copy-constructor (fluent-style)
static BeanSerializerFactory instance
          Like BasicSerializerFactory, this factory is stateless, and thus a single shared global (== singleton) instance can be used without thread-safety issues.
 
Fields inherited from class org.codehaus.jackson.map.ser.BasicSerializerFactory
_concrete, _concreteLazy, optionalHandlers
 
Constructor Summary
protected BeanSerializerFactory()
          Deprecated. 
protected BeanSerializerFactory(SerializerFactory.Config config)
          Constructor for creating instances with specified configuration.
 
Method Summary
protected  BeanPropertyWriter _constructWriter(SerializationConfig config, TypeBindings typeContext, PropertyBuilder pb, boolean staticTyping, String name, AnnotatedMember accessor)
          Secondary helper method for constructing BeanPropertyWriter for given member (field or method).
protected  List<BeanPropertyWriter> _sortBeanProperties(List<BeanPropertyWriter> props, List<String> creatorProps, String[] propertyOrder, boolean sort)
          Helper method that will sort given List of properties according to defined criteria (usually detected by annotations)
protected  JsonSerializer<Object> constructBeanSerializer(SerializationConfig config, BasicBeanDescription beanDesc, BeanProperty property)
          Method called to construct serializer for serializing specified bean type.
protected  BeanSerializerBuilder constructBeanSerializerBuilder(BasicBeanDescription beanDesc)
           
protected  BeanPropertyWriter constructFilteredBeanWriter(BeanPropertyWriter writer, Class<?>[] inViews)
          Method called to construct a filtered writer, for given view definitions.
protected  PropertyBuilder constructPropertyBuilder(SerializationConfig config, BasicBeanDescription beanDesc)
           
 JsonSerializer<Object> createSerializer(SerializationConfig config, JavaType type, BeanProperty property)
          Main serializer constructor method.
protected  List<BeanPropertyWriter> filterBeanProperties(SerializationConfig config, BasicBeanDescription beanDesc, List<BeanPropertyWriter> props)
          Overridable method that can filter out properties.
protected  List<BeanPropertyWriter> findBeanProperties(SerializationConfig config, BasicBeanDescription beanDesc)
          Method used to collect all actual serializable properties.
 JsonSerializer<Object> findBeanSerializer(SerializationConfig config, JavaType type, BasicBeanDescription beanDesc, BeanProperty property)
          Method that will try to construct a BeanSerializer for given class.
protected  Object findFilterId(SerializationConfig config, BasicBeanDescription beanDesc)
          Method called to find filter that is configured to be used with bean serializer being built, if any.
 TypeSerializer findPropertyContentTypeSerializer(JavaType containerType, SerializationConfig config, AnnotatedMember accessor, BeanProperty property)
          Method called to create a type information serializer for values of given container property if one is needed.
 TypeSerializer findPropertyTypeSerializer(JavaType baseType, SerializationConfig config, AnnotatedMember accessor, BeanProperty property)
          Method called to create a type information serializer for values of given non-container property if one is needed.
 SerializerFactory.Config getConfig()
           
protected  boolean isPotentialBeanType(Class<?> type)
          Helper method used to skip processing for types that we know can not be (i.e.
protected  void processViews(SerializationConfig config, BeanSerializerBuilder builder)
          Method called to handle view information for constructed serializer, based on bean property writers.
protected
<T extends AnnotatedMember>
void
removeIgnorableTypes(SerializationConfig config, BasicBeanDescription beanDesc, Map<String,T> props)
          Method that will apply by-type limitations (as per [JACKSON-429]); by default this is based on JsonIgnoreType annotation but can be supplied by module-provided introspectors too.
protected  List<BeanPropertyWriter> sortBeanProperties(SerializationConfig config, BasicBeanDescription beanDesc, List<BeanPropertyWriter> props)
          Overridable method that will impose given partial ordering on list of discovered propertied.
 SerializerFactory withConfig(SerializerFactory.Config config)
          Method used by module registration functionality, to attach additional serializer providers into this serializer factory.
 
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 org.codehaus.jackson.map.SerializerFactory
createSerializer, createTypeSerializer, withAdditionalSerializers, withSerializerModifier
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

instance

public static final BeanSerializerFactory instance
Like BasicSerializerFactory, this factory is stateless, and thus a single shared global (== singleton) instance can be used without thread-safety issues.


_factoryConfig

protected final SerializerFactory.Config _factoryConfig
Configuration settings for this factory; immutable instance (just like this factory), new version created via copy-constructor (fluent-style)

Since:
1.7
Constructor Detail

BeanSerializerFactory

@Deprecated
protected BeanSerializerFactory()
Deprecated. 


BeanSerializerFactory

protected BeanSerializerFactory(SerializerFactory.Config config)
Constructor for creating instances with specified configuration.

Method Detail

getConfig

public SerializerFactory.Config getConfig()
Specified by:
getConfig in class SerializerFactory

withConfig

public SerializerFactory withConfig(SerializerFactory.Config config)
Method used by module registration functionality, to attach additional serializer providers into this serializer factory. This is typically handled by constructing a new instance with additional serializers, to ensure thread-safe access.

Specified by:
withConfig in class SerializerFactory
Since:
1.7

createSerializer

public JsonSerializer<Object> createSerializer(SerializationConfig config,
                                               JavaType type,
                                               BeanProperty property)
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 BasicSerializerFactory

findBeanSerializer

public JsonSerializer<Object> findBeanSerializer(SerializationConfig config,
                                                 JavaType type,
                                                 BasicBeanDescription beanDesc,
                                                 BeanProperty property)
Method that will try to construct a BeanSerializer for given class. Returns null if no properties are found.


findPropertyTypeSerializer

public TypeSerializer findPropertyTypeSerializer(JavaType baseType,
                                                 SerializationConfig config,
                                                 AnnotatedMember accessor,
                                                 BeanProperty property)
Method called to create a type information serializer for values of given non-container property if one is needed. If not needed (no polymorphic handling configured), should return null.

Parameters:
baseType - Declared type to use as the base type for type information serializer
Returns:
Type serializer to use for property values, if one is needed; null if not.
Since:
1.5

findPropertyContentTypeSerializer

public TypeSerializer findPropertyContentTypeSerializer(JavaType containerType,
                                                        SerializationConfig config,
                                                        AnnotatedMember accessor,
                                                        BeanProperty property)
Method called to create a type information serializer for values of given container property if one is needed. If not needed (no polymorphic handling configured), should return null.

Parameters:
containerType - Declared type of the container to use as the base type for type information serializer
Returns:
Type serializer to use for property value contents, if one is needed; null if not.
Since:
1.5

constructBeanSerializer

protected JsonSerializer<Object> constructBeanSerializer(SerializationConfig config,
                                                         BasicBeanDescription beanDesc,
                                                         BeanProperty property)
Method called to construct serializer for serializing specified bean type.

Since:
1.6

constructFilteredBeanWriter

protected BeanPropertyWriter constructFilteredBeanWriter(BeanPropertyWriter writer,
                                                         Class<?>[] inViews)
Method called to construct a filtered writer, for given view definitions. Default implementation constructs filter that checks active view type to views property is to be included in.


constructPropertyBuilder

protected PropertyBuilder constructPropertyBuilder(SerializationConfig config,
                                                   BasicBeanDescription beanDesc)

constructBeanSerializerBuilder

protected BeanSerializerBuilder constructBeanSerializerBuilder(BasicBeanDescription beanDesc)

findFilterId

protected Object findFilterId(SerializationConfig config,
                              BasicBeanDescription beanDesc)
Method called to find filter that is configured to be used with bean serializer being built, if any.

Since:
1.7

isPotentialBeanType

protected boolean isPotentialBeanType(Class<?> type)
Helper method used to skip processing for types that we know can not be (i.e. are never consider to be) beans: things like primitives, Arrays, Enums, and proxy types.

Note that usually we shouldn't really be getting these sort of types anyway; but better safe than sorry.


findBeanProperties

protected List<BeanPropertyWriter> findBeanProperties(SerializationConfig config,
                                                      BasicBeanDescription beanDesc)
Method used to collect all actual serializable properties. Can be overridden to implement custom detection schemes.


filterBeanProperties

protected List<BeanPropertyWriter> filterBeanProperties(SerializationConfig config,
                                                        BasicBeanDescription beanDesc,
                                                        List<BeanPropertyWriter> props)
Overridable method that can filter out properties. Default implementation checks annotations class may have.


sortBeanProperties

protected List<BeanPropertyWriter> sortBeanProperties(SerializationConfig config,
                                                      BasicBeanDescription beanDesc,
                                                      List<BeanPropertyWriter> props)
Overridable method that will impose given partial ordering on list of discovered propertied. Method can be overridden to provide custom ordering of properties, beyond configurability offered by annotations (whic allow alphabetic ordering, as well as explicit ordering by providing array of property names).

By default Creator properties will be ordered before other properties. Explicit custom ordering will override this implicit default ordering.


processViews

protected void processViews(SerializationConfig config,
                            BeanSerializerBuilder builder)
Method called to handle view information for constructed serializer, based on bean property writers.

Note that this method is designed to be overridden by sub-classes if they want to provide custom view handling. As such it is not considered an internal implementation detail, and will be supported as part of API going forward.

NOTE: signature of this method changed in 1.7, due to other significant changes (esp. use of builder for serializer construction).


removeIgnorableTypes

protected <T extends AnnotatedMember> void removeIgnorableTypes(SerializationConfig config,
                                                                BasicBeanDescription beanDesc,
                                                                Map<String,T> props)
Method that will apply by-type limitations (as per [JACKSON-429]); by default this is based on JsonIgnoreType annotation but can be supplied by module-provided introspectors too.


_constructWriter

protected BeanPropertyWriter _constructWriter(SerializationConfig config,
                                              TypeBindings typeContext,
                                              PropertyBuilder pb,
                                              boolean staticTyping,
                                              String name,
                                              AnnotatedMember accessor)
Secondary helper method for constructing BeanPropertyWriter for given member (field or method).


_sortBeanProperties

protected List<BeanPropertyWriter> _sortBeanProperties(List<BeanPropertyWriter> props,
                                                       List<String> creatorProps,
                                                       String[] propertyOrder,
                                                       boolean sort)
Helper method that will sort given List of properties according to defined criteria (usually detected by annotations)