Class BeanSerializerFactory

  • All Implemented Interfaces:
    java.io.Serializable

    public class BeanSerializerFactory
    extends BasicSerializerFactory
    implements java.io.Serializable
    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.

    See Also:
    Serialized Form
    • 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.
    • Method Detail

      • withConfig

        public SerializerFactory withConfig​(SerializerFactoryConfig 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 BasicSerializerFactory
      • createSerializer

        public JsonSerializer<java.lang.Object> createSerializer​(SerializerProvider prov,
                                                                 JavaType origType)
                                                          throws JsonMappingException
        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.

        Specified by:
        createSerializer in class BasicSerializerFactory
        Parameters:
        prov - Provider that needs to be used to resolve annotation-provided serializers (but NOT for others)
        Throws:
        JsonMappingException
      • findPropertyTypeSerializer

        public TypeSerializer findPropertyTypeSerializer​(JavaType baseType,
                                                         SerializationConfig config,
                                                         AnnotatedMember accessor)
                                                  throws JsonMappingException
        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.
        Throws:
        JsonMappingException
      • findPropertyContentTypeSerializer

        public TypeSerializer findPropertyContentTypeSerializer​(JavaType containerType,
                                                                SerializationConfig config,
                                                                AnnotatedMember accessor)
                                                         throws JsonMappingException
        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.
        Throws:
        JsonMappingException