BasicSerializerFactory (Jackson JSON Processor)
Date: 2013-07-14

org.codehaus.jackson.map.ser

## Class BasicSerializerFactory

- java.lang.Object
- - org.codehaus.jackson.map.SerializerFactory
  - - org.codehaus.jackson.map.ser.BasicSerializerFactory

- Direct Known Subclasses:
    BeanSerializerFactory

      public abstract class BasicSerializerFactory
      extends SerializerFactory

  Factory class that can provide serializers for standard JDK classes, as well as custom classes that extend standard classes or implement one of "well-known" interfaces (such as `Collection`).

  Since all the serializers are eagerly instantiated, and there is no additional introspection or customizability of these types, this factory is essentially stateless.

- - ### Nested Class Summary

    - ### Nested classes/interfaces inherited from class org.codehaus.jackson.map.SerializerFactory

      `SerializerFactory.Config`

  - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `protected static HashMap<String,JsonSerializer<?>>` | `_arraySerializers`
    `protected static HashMap<String,JsonSerializer<?>>` | `_concrete` Since these are all JDK classes, we shouldn't have to worry about ClassLoader used to load them.
    `protected static HashMap<String,Class<? extends JsonSerializer<?>>>` | `_concreteLazy` Actually it may not make much sense to eagerly instantiate all kinds of serializers: so this Map actually contains class references, not instances
    `protected OptionalHandlerFactory` | `optionalHandlers` Helper object used to deal with serializers for optional JDK types (like ones omitted from GAE, Android)

  - ### Constructor Summary

    Constructors
    Modifier | Constructor and Description
    --- | ---
    `protected` | `BasicSerializerFactory()` We will provide default constructor to allow sub-classing, but make it protected so that no non-singleton instances of the class will be instantiated.

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `protected JsonSerializer<?>` | `buildArraySerializer(SerializationConfig config, ArrayType type, BasicBeanDescription beanDesc, BeanProperty property, boolean staticTyping, TypeSerializer elementTypeSerializer, JsonSerializer<Object> elementValueSerializer)` Helper method that handles configuration details when constructing serializers for `Object[]` (and subtypes, except for String).
    `protected JsonSerializer<?>` | `buildCollectionLikeSerializer(SerializationConfig config, CollectionLikeType type, BasicBeanDescription beanDesc, BeanProperty property, boolean staticTyping, TypeSerializer elementTypeSerializer, JsonSerializer<Object> elementValueSerializer)` Helper method that handles configuration details when constructing serializers for Collection and Collection-like types.
    `protected JsonSerializer<?>` | `buildCollectionSerializer(SerializationConfig config, CollectionType type, BasicBeanDescription beanDesc, BeanProperty property, boolean staticTyping, TypeSerializer elementTypeSerializer, JsonSerializer<Object> elementValueSerializer)` Helper method that handles configuration details when constructing serializers for `List` types that support efficient by-index access
    `JsonSerializer<?>` | `buildContainerSerializer(SerializationConfig config, JavaType type, BasicBeanDescription beanDesc, BeanProperty property, boolean staticTyping)`
    `protected JsonSerializer<?>` | `buildEnumMapSerializer(SerializationConfig config, JavaType type, BasicBeanDescription beanDesc, BeanProperty property, boolean staticTyping, TypeSerializer elementTypeSerializer, JsonSerializer<Object> elementValueSerializer)` Helper method that handles configuration details when constructing serializers for `EnumMap` types.
    `protected JsonSerializer<?>` | `buildEnumSetSerializer(SerializationConfig config, JavaType type, BasicBeanDescription beanDesc, BeanProperty property, boolean staticTyping, TypeSerializer elementTypeSerializer, JsonSerializer<Object> elementValueSerializer)`
    `protected JsonSerializer<?>` | `buildIterableSerializer(SerializationConfig config, JavaType type, BasicBeanDescription beanDesc, BeanProperty property, boolean staticTyping)`
    `protected JsonSerializer<?>` | `buildIteratorSerializer(SerializationConfig config, JavaType type, BasicBeanDescription beanDesc, BeanProperty property, boolean staticTyping)`
    `protected JsonSerializer<?>` | `buildMapLikeSerializer(SerializationConfig config, MapLikeType type, BasicBeanDescription beanDesc, BeanProperty property, boolean staticTyping, JsonSerializer<Object> keySerializer, TypeSerializer elementTypeSerializer, JsonSerializer<Object> elementValueSerializer)` Helper method that handles configuration details when constructing serializers for all "Map-like" types; both ones that implement `Map` and ones that do not (but that have been indicated to behave like Maps).
    `protected JsonSerializer<?>` | `buildMapSerializer(SerializationConfig config, MapType type, BasicBeanDescription beanDesc, BeanProperty property, boolean staticTyping, JsonSerializer<Object> keySerializer, TypeSerializer elementTypeSerializer, JsonSerializer<Object> elementValueSerializer)` Helper method that handles configuration details when constructing serializers for `Map` types.
    `abstract JsonSerializer<Object>` | `createSerializer(SerializationConfig config, JavaType type, BeanProperty property)` Method called to create (or, for immutable serializers, reuse) a serializer for given type.
    `TypeSerializer` | `createTypeSerializer(SerializationConfig config, JavaType baseType, BeanProperty property)` Method called to construct a type serializer for values with given declared base type.
    `protected abstract Iterable<Serializers>` | `customSerializers()`
    `protected static JsonSerializer<Object>` | `findContentSerializer(SerializationConfig config, Annotated a, BeanProperty property)`
    `protected static JsonSerializer<Object>` | `findKeySerializer(SerializationConfig config, Annotated a, BeanProperty property)`
    `JsonSerializer<?>` | `findSerializerByAddonType(SerializationConfig config, JavaType javaType, BasicBeanDescription beanDesc, BeanProperty property, boolean staticTyping)` Reflection-based serialized find method, which checks if given class implements one of recognized "add-on" interfaces.
    `JsonSerializer<?>` | `findSerializerByLookup(JavaType type, SerializationConfig config, BasicBeanDescription beanDesc, BeanProperty property, boolean staticTyping)` Method that will use fast lookup (and identity comparison) methods to see if we know serializer to use for given type.
    `JsonSerializer<?>` | `findSerializerByPrimaryType(JavaType type, SerializationConfig config, BasicBeanDescription beanDesc, BeanProperty property, boolean staticTyping)` Method for checking if we can determine serializer to use based on set of known primary types, checking for set of known base types (exact matches having been compared against with `findSerializerByLookup`).
    `protected JsonSerializer<Object>` | `findSerializerFromAnnotation(SerializationConfig config, Annotated a, BeanProperty property)` Helper method called to check if a class or method has an annotation (@link org.codehaus.jackson.map.ser.JsonSerialize#using) that tells the class to use for serialization.
    `JsonSerializer<?>` | `getNullSerializer()`
    `protected boolean` | `isIndexedList(Class<?> cls)`
    `protected static <T extends JavaType> T` | `modifySecondaryTypesByAnnotation(SerializationConfig config, Annotated a, T type)`
    `protected <T extends JavaType> T` | `modifyTypeByAnnotation(SerializationConfig config, Annotated a, T type)` Helper method used to encapsulate details of annotation-based type coercion
    `protected boolean` | `usesStaticTyping(SerializationConfig config, BasicBeanDescription beanDesc, TypeSerializer typeSer, BeanProperty property)` Helper method to check whether global settings and/or class annotations for the bean class indicate that static typing (declared types) should be used for properties.

    - ### Methods inherited from class org.codehaus.jackson.map.SerializerFactory

      `createKeySerializer, createSerializer, createTypeSerializer, getConfig, withAdditionalKeySerializers, withAdditionalSerializers, withConfig, withSerializerModifier`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### _concrete

          protected static final HashMap<String,JsonSerializer<?>> _concrete

      Since these are all JDK classes, we shouldn't have to worry about ClassLoader used to load them. Rather, we can just use the class name, and keep things simple and efficient.

    - #### _concreteLazy

          protected static final HashMap<String,Class<? extends JsonSerializer<?>>> _concreteLazy

      Actually it may not make much sense to eagerly instantiate all kinds of serializers: so this Map actually contains class references, not instances
      Since:
        1.6

    - #### _arraySerializers

          protected static final HashMap<String,JsonSerializer<?>> _arraySerializers

    - #### optionalHandlers

          protected OptionalHandlerFactory optionalHandlers

      Helper object used to deal with serializers for optional JDK types (like ones omitted from GAE, Android)

  - ### Constructor Detail

    - #### BasicSerializerFactory

          protected BasicSerializerFactory()

      We will provide default constructor to allow sub-classing, but make it protected so that no non-singleton instances of the class will be instantiated.

  - ### Method Detail

    - #### createSerializer

          public abstract JsonSerializer<Object> createSerializer(SerializationConfig config,
                                                JavaType type,
                                                BeanProperty property)
                                                           throws JsonMappingException

      Description copied from class: `SerializerFactory`
      Method called to create (or, for immutable serializers, reuse) a serializer for given type.
      Specified by:
        `createSerializer` in class `SerializerFactory`
      Throws:
        `JsonMappingException`

    - #### createTypeSerializer

          public TypeSerializer createTypeSerializer(SerializationConfig config,
                                            JavaType baseType,
                                            BeanProperty property)

      Method called to construct a type serializer for values with given declared base type. This is called for values other than those of bean property types.
      Specified by:
        `createTypeSerializer` in class `SerializerFactory`
        `baseType` - Declared type to use as the base type for type information serializer
      Returns:
        Type serializer to use for the base type, if one is needed; null if not.

    - #### getNullSerializer

          public final JsonSerializer<?> getNullSerializer()

    - #### customSerializers

          protected abstract Iterable<Serializers> customSerializers()

    - #### findSerializerByLookup

          public final JsonSerializer<?> findSerializerByLookup(JavaType type,
                                                 SerializationConfig config,
                                                 BasicBeanDescription beanDesc,
                                                 BeanProperty property,
                                                 boolean staticTyping)

      Method that will use fast lookup (and identity comparison) methods to see if we know serializer to use for given type.

    - #### findSerializerByPrimaryType

          public final JsonSerializer<?> findSerializerByPrimaryType(JavaType type,
                                                      SerializationConfig config,
                                                      BasicBeanDescription beanDesc,
                                                      BeanProperty property,
                                                      boolean staticTyping)
                                                              throws JsonMappingException

      Method for checking if we can determine serializer to use based on set of known primary types, checking for set of known base types (exact matches having been compared against with `findSerializerByLookup`). This does not include "secondary" interfaces, but mostly concrete or abstract base classes.
      Throws:
        `JsonMappingException`

    - #### findSerializerByAddonType

          public final JsonSerializer<?> findSerializerByAddonType(SerializationConfig config,
                                                    JavaType javaType,
                                                    BasicBeanDescription beanDesc,
                                                    BeanProperty property,
                                                    boolean staticTyping)
                                                            throws JsonMappingException

      Reflection-based serialized find method, which checks if given class implements one of recognized "add-on" interfaces. Add-on here means a role that is usually or can be a secondary trait: for example, bean classes may implement `Iterable`, but their main function is usually something else. The reason for
      Throws:
        `JsonMappingException`

    - #### findSerializerFromAnnotation

          protected JsonSerializer<Object> findSerializerFromAnnotation(SerializationConfig config,
                                                            Annotated a,
                                                            BeanProperty property)
                                                                 throws JsonMappingException

      Helper method called to check if a class or method has an annotation (@link org.codehaus.jackson.map.ser.JsonSerialize#using) that tells the class to use for serialization. Returns null if no such annotation found.
      Throws:
        `JsonMappingException`

    - #### buildContainerSerializer

          public JsonSerializer<?> buildContainerSerializer(SerializationConfig config,
                                                   JavaType type,
                                                   BasicBeanDescription beanDesc,
                                                   BeanProperty property,
                                                   boolean staticTyping)

    - #### buildCollectionLikeSerializer

          protected JsonSerializer<?> buildCollectionLikeSerializer(SerializationConfig config,
                                                        CollectionLikeType type,
                                                        BasicBeanDescription beanDesc,
                                                        BeanProperty property,
                                                        boolean staticTyping,
                                                        TypeSerializer elementTypeSerializer,
                                                        JsonSerializer<Object> elementValueSerializer)

      Helper method that handles configuration details when constructing serializers for Collection and Collection-like types.
      Since:
        1.8

    - #### buildCollectionSerializer

          protected JsonSerializer<?> buildCollectionSerializer(SerializationConfig config,
                                                    CollectionType type,
                                                    BasicBeanDescription beanDesc,
                                                    BeanProperty property,
                                                    boolean staticTyping,
                                                    TypeSerializer elementTypeSerializer,
                                                    JsonSerializer<Object> elementValueSerializer)

      Helper method that handles configuration details when constructing serializers for `List` types that support efficient by-index access

      Note: signature changed in 1.8, to take 'staticTyping' argument

    - #### buildEnumSetSerializer

          protected JsonSerializer<?> buildEnumSetSerializer(SerializationConfig config,
                                                 JavaType type,
                                                 BasicBeanDescription beanDesc,
                                                 BeanProperty property,
                                                 boolean staticTyping,
                                                 TypeSerializer elementTypeSerializer,
                                                 JsonSerializer<Object> elementValueSerializer)

    - #### isIndexedList

          protected boolean isIndexedList(Class<?> cls)

      Since:
        1.8

    - #### buildMapLikeSerializer

          protected JsonSerializer<?> buildMapLikeSerializer(SerializationConfig config,
                                                 MapLikeType type,
                                                 BasicBeanDescription beanDesc,
                                                 BeanProperty property,
                                                 boolean staticTyping,
                                                 JsonSerializer<Object> keySerializer,
                                                 TypeSerializer elementTypeSerializer,
                                                 JsonSerializer<Object> elementValueSerializer)

      Helper method that handles configuration details when constructing serializers for all "Map-like" types; both ones that implement `Map` and ones that do not (but that have been indicated to behave like Maps).
      Since:
        1.8

    - #### buildMapSerializer

          protected JsonSerializer<?> buildMapSerializer(SerializationConfig config,
                                             MapType type,
                                             BasicBeanDescription beanDesc,
                                             BeanProperty property,
                                             boolean staticTyping,
                                             JsonSerializer<Object> keySerializer,
                                             TypeSerializer elementTypeSerializer,
                                             JsonSerializer<Object> elementValueSerializer)

      Helper method that handles configuration details when constructing serializers for `Map` types.

      Note: signature changed in 1.8, to take 'staticTyping' argument

    - #### buildEnumMapSerializer

          protected JsonSerializer<?> buildEnumMapSerializer(SerializationConfig config,
                                                 JavaType type,
                                                 BasicBeanDescription beanDesc,
                                                 BeanProperty property,
                                                 boolean staticTyping,
                                                 TypeSerializer elementTypeSerializer,
                                                 JsonSerializer<Object> elementValueSerializer)

      Helper method that handles configuration details when constructing serializers for `EnumMap` types.

      Note: signature changed in 1.8, to take 'staticTyping' argument

    - #### buildArraySerializer

          protected JsonSerializer<?> buildArraySerializer(SerializationConfig config,
                                               ArrayType type,
                                               BasicBeanDescription beanDesc,
                                               BeanProperty property,
                                               boolean staticTyping,
                                               TypeSerializer elementTypeSerializer,
                                               JsonSerializer<Object> elementValueSerializer)

      Helper method that handles configuration details when constructing serializers for `Object[]` (and subtypes, except for String).

    - #### buildIteratorSerializer

          protected JsonSerializer<?> buildIteratorSerializer(SerializationConfig config,
                                                  JavaType type,
                                                  BasicBeanDescription beanDesc,
                                                  BeanProperty property,
                                                  boolean staticTyping)

    - #### buildIterableSerializer

          protected JsonSerializer<?> buildIterableSerializer(SerializationConfig config,
                                                  JavaType type,
                                                  BasicBeanDescription beanDesc,
                                                  BeanProperty property,
                                                  boolean staticTyping)

    - #### modifyTypeByAnnotation

          protected <T extends JavaType> T modifyTypeByAnnotation(SerializationConfig config,
                                                      Annotated a,
                                                      T type)

      Helper method used to encapsulate details of annotation-based type coercion
      Since:
        1.8

    - #### modifySecondaryTypesByAnnotation

          protected static <T extends JavaType> T modifySecondaryTypesByAnnotation(SerializationConfig config,
                                                                Annotated a,
                                                                T type)

      Since:
        1.8

    - #### findKeySerializer

          protected static JsonSerializer<Object> findKeySerializer(SerializationConfig config,
                                                 Annotated a,
                                                 BeanProperty property)

    - #### findContentSerializer

          protected static JsonSerializer<Object> findContentSerializer(SerializationConfig config,
                                                     Annotated a,
                                                     BeanProperty property)

    - #### usesStaticTyping

          protected boolean usesStaticTyping(SerializationConfig config,
                                 BasicBeanDescription beanDesc,
                                 TypeSerializer typeSer,
                                 BeanProperty property)

      Helper method to check whether global settings and/or class annotations for the bean class indicate that static typing (declared types) should be used for properties. (instead of dynamic runtime types).