com.fasterxml.jackson.databind.ser

## Class BasicSerializerFactory

- java.lang.Object
- - com.fasterxml.jackson.databind.ser.SerializerFactory
  - - com.fasterxml.jackson.databind.ser.BasicSerializerFactory

- All Implemented Interfaces:
    java.io.Serializable
  Direct Known Subclasses:
    BeanSerializerFactory

      public abstract class BasicSerializerFactory
      extends SerializerFactory
      implements java.io.Serializable

  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.

  See Also:
    Serialized Form

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `protected static java.util.HashMap<java.lang.String,JsonSerializer<?>>` | `_concrete` Since these are all JDK classes, we shouldn't have to worry about ClassLoader used to load them.
    `protected static java.util.HashMap<java.lang.String,java.lang.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 SerializerFactoryConfig` | `_factoryConfig` Configuration settings for this factory; immutable instance (just like this factory), new version created via copy-constructor (fluent-style)

  - ### Constructor Summary

    Constructors
    Modifier | Constructor and Description
    --- | ---
    `protected` | `BasicSerializerFactory(SerializerFactoryConfig config)` 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<java.lang.Object>` | `_findContentSerializer(SerializerProvider prov, Annotated a)` Helper method called to try to find whether there is an annotation in the class that indicates content ("value") serializer to use.
    `protected JsonSerializer<java.lang.Object>` | `_findKeySerializer(SerializerProvider prov, Annotated a)` Helper method called to try to find whether there is an annotation in the class that indicates key serializer to use.
    `protected java.lang.Class<?>` | `_verifyAsClass(java.lang.Object src, java.lang.String methodName, java.lang.Class<?> noneClass)`
    `protected JsonSerializer<?>` | `buildArraySerializer(SerializerProvider prov, ArrayType type, BeanDescription beanDesc, boolean staticTyping, TypeSerializer elementTypeSerializer, JsonSerializer<java.lang.Object> elementValueSerializer)` Helper method that handles configuration details when constructing serializers for `Object[]` (and subtypes, except for String).
    `ContainerSerializer<?>` | `buildCollectionSerializer(JavaType elemType, boolean staticTyping, TypeSerializer vts, JsonSerializer<java.lang.Object> valueSerializer)`
    `protected JsonSerializer<?>` | `buildCollectionSerializer(SerializerProvider prov, CollectionType type, BeanDescription beanDesc, boolean staticTyping, TypeSerializer elementTypeSerializer, JsonSerializer<java.lang.Object> elementValueSerializer)` Helper method that handles configuration details when constructing serializers for `List` types that support efficient by-index access
    `protected JsonSerializer<?>` | `buildContainerSerializer(SerializerProvider prov, JavaType type, BeanDescription beanDesc, boolean staticTyping)`
    `protected JsonSerializer<?>` | `buildEnumSerializer(SerializationConfig config, JavaType type, BeanDescription beanDesc)`
    `JsonSerializer<?>` | `buildEnumSetSerializer(JavaType enumType)`
    `ContainerSerializer<?>` | `buildIndexedListSerializer(JavaType elemType, boolean staticTyping, TypeSerializer vts, JsonSerializer<java.lang.Object> valueSerializer)`
    `protected JsonSerializer<?>` | `buildIterableSerializer(SerializationConfig config, JavaType type, BeanDescription beanDesc, boolean staticTyping, JavaType valueType)`
    `protected JsonSerializer<?>` | `buildIteratorSerializer(SerializationConfig config, JavaType type, BeanDescription beanDesc, boolean staticTyping, JavaType valueType)`
    `protected JsonSerializer<?>` | `buildMapEntrySerializer(SerializationConfig config, JavaType type, BeanDescription beanDesc, boolean staticTyping, JavaType keyType, JavaType valueType)`
    `protected JsonSerializer<?>` | `buildMapSerializer(SerializerProvider prov, MapType type, BeanDescription beanDesc, boolean staticTyping, JsonSerializer<java.lang.Object> keySerializer, TypeSerializer elementTypeSerializer, JsonSerializer<java.lang.Object> elementValueSerializer)` Helper method that handles configuration details when constructing serializers for `Map` types.
    `JsonSerializer<java.lang.Object>` | `createKeySerializer(SerializationConfig config, JavaType keyType, JsonSerializer<java.lang.Object> defaultImpl)` Method called to create serializer to use for serializing JSON property names (which must be output as `JsonToken.FIELD_NAME`) for Map that has specified declared key type, and is for specified property (or, if property is null, as root value)
    `abstract JsonSerializer<java.lang.Object>` | `createSerializer(SerializerProvider prov, JavaType type)` Method called to create (or, for immutable serializers, reuse) a serializer for given type.
    `TypeSerializer` | `createTypeSerializer(SerializationConfig config, JavaType baseType)` Method called to construct a type serializer for values with given declared base type.
    `protected abstract java.lang.Iterable<Serializers>` | `customSerializers()`
    `protected Converter<java.lang.Object,java.lang.Object>` | `findConverter(SerializerProvider prov, Annotated a)`
    `protected JsonSerializer<?>` | `findConvertingSerializer(SerializerProvider prov, Annotated a, JsonSerializer<?> ser)` Helper method that will check whether given annotated entity (usually class, but may also be a property accessor) indicates that a `Converter` is to be used; and if so, to construct and return suitable serializer for it.
    `protected java.lang.Object` | `findFilterId(SerializationConfig config, BeanDescription beanDesc)` Method called to find filter that is configured to be used with bean serializer being built, if any.
    `protected JsonSerializer<?>` | `findOptionalStdSerializer(SerializerProvider prov, JavaType type, BeanDescription beanDesc, boolean staticTyping)` Overridable method called after checking all other types.
    `protected JsonSerializer<?>` | `findSerializerByAddonType(SerializationConfig config, JavaType javaType, BeanDescription beanDesc, boolean staticTyping)` Reflection-based serialized find method, which checks if given class implements one of recognized "add-on" interfaces.
    `protected JsonSerializer<?>` | `findSerializerByAnnotations(SerializerProvider prov, JavaType type, BeanDescription beanDesc)` Method called to see if one of primary per-class annotations (or related, like implementing of `JsonSerializable`) determines the serializer to use.
    `protected JsonSerializer<?>` | `findSerializerByLookup(JavaType type, SerializationConfig config, BeanDescription beanDesc, boolean staticTyping)` Method that will use fast lookup (and identity comparison) methods to see if we know serializer to use for given type.
    `protected JsonSerializer<?>` | `findSerializerByPrimaryType(SerializerProvider prov, JavaType type, BeanDescription beanDesc, 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<java.lang.Object>` | `findSerializerFromAnnotation(SerializerProvider prov, Annotated a)` Helper method called to check if a class or method has an annotation (@link com.fasterxml.jackson.databind.annotation.JsonSerialize#using) that tells the class to use for serialization.
    `protected java.lang.Object` | `findSuppressableContentValue(SerializationConfig config, JavaType contentType, BeanDescription beanDesc)` NOTE: although return type is left opaque, it really needs to be `JsonInclude.Include` for things to work as expected.
    `SerializerFactoryConfig` | `getFactoryConfig()` Method for getting current `SerializerFactoryConfig`.
    `protected boolean` | `isIndexedList(java.lang.Class<?> cls)`
    `protected boolean` | `usesStaticTyping(SerializationConfig config, BeanDescription beanDesc, TypeSerializer typeSer)` 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.
    `SerializerFactory` | `withAdditionalKeySerializers(Serializers additional)` Convenience method for creating a new factory instance with an additional key serializer provider.
    `SerializerFactory` | `withAdditionalSerializers(Serializers additional)` Convenience method for creating a new factory instance with an additional serializer provider.
    `abstract SerializerFactory` | `withConfig(SerializerFactoryConfig config)` Method used for creating a new instance of this factory, but with different configuration.
    `SerializerFactory` | `withSerializerModifier(BeanSerializerModifier modifier)` Convenience method for creating a new factory instance with additional bean serializer modifier.

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

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

- - ### Field Detail

    - #### _concrete

          protected static final java.util.HashMap<java.lang.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 java.util.HashMap<java.lang.String,java.lang.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

    - #### _factoryConfig

          protected final SerializerFactoryConfig _factoryConfig

      Configuration settings for this factory; immutable instance (just like this factory), new version created via copy-constructor (fluent-style)

  - ### Constructor Detail

    - #### BasicSerializerFactory

          protected BasicSerializerFactory(SerializerFactoryConfig config)

      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

    - #### getFactoryConfig

          public SerializerFactoryConfig getFactoryConfig()

      Method for getting current `SerializerFactoryConfig`.

      Note that since instances are immutable, you can NOT change settings by accessing an instance and calling methods: this will simply create new instance of config object.

    - #### withConfig

          public abstract SerializerFactory withConfig(SerializerFactoryConfig config)

      Method used for creating a new instance of this factory, but with different configuration. Reason for specifying factory method (instead of plain constructor) is to allow proper sub-classing of factories.

      Note that custom sub-classes generally must override implementation of this method, as it usually requires instantiating a new instance of factory type. Check out javadocs for `BeanSerializerFactory` for more details.

    - #### withAdditionalSerializers

          public final SerializerFactory withAdditionalSerializers(Serializers additional)

      Convenience method for creating a new factory instance with an additional serializer provider.
      Specified by:
        `withAdditionalSerializers` in class `SerializerFactory`

    - #### withAdditionalKeySerializers

          public final SerializerFactory withAdditionalKeySerializers(Serializers additional)

      Convenience method for creating a new factory instance with an additional key serializer provider.
      Specified by:
        `withAdditionalKeySerializers` in class `SerializerFactory`

    - #### withSerializerModifier

          public final SerializerFactory withSerializerModifier(BeanSerializerModifier modifier)

      Convenience method for creating a new factory instance with additional bean serializer modifier.
      Specified by:
        `withSerializerModifier` in class `SerializerFactory`

    - #### createSerializer

          public abstract JsonSerializer<java.lang.Object> createSerializer(SerializerProvider prov,
                                                          JavaType type)
                                                                     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`
      Parameters:
        `prov` - Provider that needs to be used to resolve annotation-provided serializers (but NOT for others)
      Throws:
        `JsonMappingException`

    - #### createKeySerializer

          public JsonSerializer<java.lang.Object> createKeySerializer(SerializationConfig config,
                                                             JavaType keyType,
                                                             JsonSerializer<java.lang.Object> defaultImpl)

      Description copied from class: `SerializerFactory`
      Method called to create serializer to use for serializing JSON property names (which must be output as `JsonToken.FIELD_NAME`) for Map that has specified declared key type, and is for specified property (or, if property is null, as root value)
      Specified by:
        `createKeySerializer` in class `SerializerFactory`
        `keyType` - Declared type for Map keys
        `defaultImpl` - Default key serializer implementation to use, if no custom ones are found (may be null)
      Returns:
        Serializer to use, if factory knows it; null if not (in which case default serializer is to be used)

    - #### createTypeSerializer

          public TypeSerializer createTypeSerializer(SerializationConfig config,
                                            JavaType baseType)

      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.

    - #### customSerializers

          protected abstract java.lang.Iterable<Serializers> customSerializers()

    - #### findSerializerByLookup

          protected final JsonSerializer<?> findSerializerByLookup(JavaType type,
                                                 SerializationConfig config,
                                                 BeanDescription beanDesc,
                                                 boolean staticTyping)

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

    - #### findSerializerByAnnotations

          protected final JsonSerializer<?> findSerializerByAnnotations(SerializerProvider prov,
                                                      JavaType type,
                                                      BeanDescription beanDesc)
                                                                 throws JsonMappingException

      Method called to see if one of primary per-class annotations (or related, like implementing of `JsonSerializable`) determines the serializer to use.

      Currently handles things like:

      - If type implements `JsonSerializable`, use that
      - If type has `JsonValue` annotation (or equivalent), build serializer based on that property
      Throws:
        `JsonMappingException`
      Since:
        2.0

    - #### findSerializerByPrimaryType

          protected final JsonSerializer<?> findSerializerByPrimaryType(SerializerProvider prov,
                                                      JavaType type,
                                                      BeanDescription beanDesc,
                                                      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`

    - #### findOptionalStdSerializer

          protected JsonSerializer<?> findOptionalStdSerializer(SerializerProvider prov,
                                                    JavaType type,
                                                    BeanDescription beanDesc,
                                                    boolean staticTyping)
                                                         throws JsonMappingException

      Overridable method called after checking all other types.
      Throws:
        `JsonMappingException`
      Since:
        2.2

    - #### findSerializerByAddonType

          protected final JsonSerializer<?> findSerializerByAddonType(SerializationConfig config,
                                                    JavaType javaType,
                                                    BeanDescription beanDesc,
                                                    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<java.lang.Object> findSerializerFromAnnotation(SerializerProvider prov,
                                                                      Annotated a)
                                                                           throws JsonMappingException

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

    - #### findConvertingSerializer

          protected JsonSerializer<?> findConvertingSerializer(SerializerProvider prov,
                                                   Annotated a,
                                                   JsonSerializer<?> ser)
                                                        throws JsonMappingException

      Helper method that will check whether given annotated entity (usually class, but may also be a property accessor) indicates that a `Converter` is to be used; and if so, to construct and return suitable serializer for it. If not, will simply return given serializer as is.
      Throws:
        `JsonMappingException`

    - #### findConverter

          protected Converter<java.lang.Object,java.lang.Object> findConverter(SerializerProvider prov,
                                                                   Annotated a)
                                                                        throws JsonMappingException

      Throws:
        `JsonMappingException`

    - #### buildContainerSerializer

          protected JsonSerializer<?> buildContainerSerializer(SerializerProvider prov,
                                                   JavaType type,
                                                   BeanDescription beanDesc,
                                                   boolean staticTyping)
                                                        throws JsonMappingException

      Throws:
        `JsonMappingException`
      Since:
        2.1

    - #### buildCollectionSerializer

          protected JsonSerializer<?> buildCollectionSerializer(SerializerProvider prov,
                                                    CollectionType type,
                                                    BeanDescription beanDesc,
                                                    boolean staticTyping,
                                                    TypeSerializer elementTypeSerializer,
                                                    JsonSerializer<java.lang.Object> elementValueSerializer)
                                                         throws JsonMappingException

      Helper method that handles configuration details when constructing serializers for `List` types that support efficient by-index access
      Throws:
        `JsonMappingException`
      Since:
        2.1

    - #### isIndexedList

          protected boolean isIndexedList(java.lang.Class<?> cls)

    - #### buildIndexedListSerializer

          public ContainerSerializer<?> buildIndexedListSerializer(JavaType elemType,
                                                          boolean staticTyping,
                                                          TypeSerializer vts,
                                                          JsonSerializer<java.lang.Object> valueSerializer)

    - #### buildCollectionSerializer

          public ContainerSerializer<?> buildCollectionSerializer(JavaType elemType,
                                                         boolean staticTyping,
                                                         TypeSerializer vts,
                                                         JsonSerializer<java.lang.Object> valueSerializer)

    - #### buildEnumSetSerializer

          public JsonSerializer<?> buildEnumSetSerializer(JavaType enumType)

    - #### buildMapSerializer

          protected JsonSerializer<?> buildMapSerializer(SerializerProvider prov,
                                             MapType type,
                                             BeanDescription beanDesc,
                                             boolean staticTyping,
                                             JsonSerializer<java.lang.Object> keySerializer,
                                             TypeSerializer elementTypeSerializer,
                                             JsonSerializer<java.lang.Object> elementValueSerializer)
                                                  throws JsonMappingException

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

    - #### findSuppressableContentValue

          protected java.lang.Object findSuppressableContentValue(SerializationConfig config,
                                                      JavaType contentType,
                                                      BeanDescription beanDesc)
                                                           throws JsonMappingException

      NOTE: although return type is left opaque, it really needs to be `JsonInclude.Include` for things to work as expected.

      Throws:
        `JsonMappingException`
      Since:
        2.5

    - #### buildArraySerializer

          protected JsonSerializer<?> buildArraySerializer(SerializerProvider prov,
                                               ArrayType type,
                                               BeanDescription beanDesc,
                                               boolean staticTyping,
                                               TypeSerializer elementTypeSerializer,
                                               JsonSerializer<java.lang.Object> elementValueSerializer)
                                                    throws JsonMappingException

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

    - #### buildIteratorSerializer

          protected JsonSerializer<?> buildIteratorSerializer(SerializationConfig config,
                                                  JavaType type,
                                                  BeanDescription beanDesc,
                                                  boolean staticTyping,
                                                  JavaType valueType)
                                                       throws JsonMappingException

      Throws:
        `JsonMappingException`
      Since:
        2.5

    - #### buildIterableSerializer

          protected JsonSerializer<?> buildIterableSerializer(SerializationConfig config,
                                                  JavaType type,
                                                  BeanDescription beanDesc,
                                                  boolean staticTyping,
                                                  JavaType valueType)
                                                       throws JsonMappingException

      Throws:
        `JsonMappingException`
      Since:
        2.5

    - #### buildMapEntrySerializer

          protected JsonSerializer<?> buildMapEntrySerializer(SerializationConfig config,
                                                  JavaType type,
                                                  BeanDescription beanDesc,
                                                  boolean staticTyping,
                                                  JavaType keyType,
                                                  JavaType valueType)
                                                       throws JsonMappingException

      Throws:
        `JsonMappingException`
      Since:
        2.5

    - #### buildEnumSerializer

          protected JsonSerializer<?> buildEnumSerializer(SerializationConfig config,
                                              JavaType type,
                                              BeanDescription beanDesc)
                                                   throws JsonMappingException

      Throws:
        `JsonMappingException`

    - #### _findKeySerializer

          protected JsonSerializer<java.lang.Object> _findKeySerializer(SerializerProvider prov,
                                                            Annotated a)
                                                                 throws JsonMappingException

      Helper method called to try to find whether there is an annotation in the class that indicates key serializer to use. If so, will try to instantiate key serializer and return it; otherwise returns null.
      Throws:
        `JsonMappingException`

    - #### _findContentSerializer

          protected JsonSerializer<java.lang.Object> _findContentSerializer(SerializerProvider prov,
                                                                Annotated a)
                                                                     throws JsonMappingException

      Helper method called to try to find whether there is an annotation in the class that indicates content ("value") serializer to use. If so, will try to instantiate value serializer and return it; otherwise returns null.
      Throws:
        `JsonMappingException`

    - #### findFilterId

          protected java.lang.Object findFilterId(SerializationConfig config,
                                      BeanDescription beanDesc)

      Method called to find filter that is configured to be used with bean serializer being built, if any.

    - #### usesStaticTyping

          protected boolean usesStaticTyping(SerializationConfig config,
                                 BeanDescription beanDesc,
                                 TypeSerializer typeSer)

      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).
      Since:
        2.1 (earlier had variant with additional 'property' parameter)

    - #### _verifyAsClass

          protected java.lang.Class<?> _verifyAsClass(java.lang.Object src,
                                          java.lang.String methodName,
                                          java.lang.Class<?> noneClass)