Class BeanSerializerFactory
- java.lang.Object
-
- com.fasterxml.jackson.databind.ser.SerializerFactory
-
- com.fasterxml.jackson.databind.ser.BasicSerializerFactory
-
- com.fasterxml.jackson.databind.ser.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" -- whereObject.getClass()
does not count); as well as for "standard" JDK types. Latter is achieved by delegating calls toBasicSerializerFactory
to find serializers both for "standard" JDK types (and in some cases, sub-classes as is the case for collection classes likeList
s andMap
s) 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 underlyingBasicSerializerFactory
; 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 regularSerializerFactory
interface but rather via direct calls toBasicSerializerFactory
.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 Summary
Fields Modifier and Type Field Description static BeanSerializerFactory
instance
LikeBasicSerializerFactory
, this factory is stateless, and thus a single shared global (== singleton) instance can be used without thread-safety issues.-
Fields inherited from class com.fasterxml.jackson.databind.ser.BasicSerializerFactory
_concrete, _concreteLazy, _factoryConfig
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
BeanSerializerFactory(SerializerFactoryConfig config)
Constructor for creating instances with specified configuration.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected BeanPropertyWriter
_constructWriter(SerializerProvider prov, BeanPropertyDefinition propDef, PropertyBuilder pb, boolean staticTyping, AnnotatedMember accessor)
Secondary helper method for constructingBeanPropertyWriter
for given member (field or method).protected JsonSerializer<?>
_createSerializer2(SerializerProvider prov, JavaType type, BeanDescription beanDesc, boolean staticTyping)
protected JsonSerializer<?>
_findUnsupportedTypeSerializer(SerializerProvider ctxt, JavaType type, BeanDescription beanDesc)
protected JsonSerializer<java.lang.Object>
constructBeanOrAddOnSerializer(SerializerProvider prov, JavaType type, BeanDescription beanDesc, boolean staticTyping)
Method called to construct serializer for serializing specified bean type if (but only if, as of 2.10), at least one property is found.protected JsonSerializer<java.lang.Object>
constructBeanSerializer(SerializerProvider prov, BeanDescription beanDesc)
Deprecated.protected BeanSerializerBuilder
constructBeanSerializerBuilder(BeanDescription beanDesc)
protected BeanPropertyWriter
constructFilteredBeanWriter(BeanPropertyWriter writer, java.lang.Class<?>[] inViews)
Method called to construct a filtered writer, for given view definitions.protected ObjectIdWriter
constructObjectIdHandler(SerializerProvider prov, BeanDescription beanDesc, java.util.List<BeanPropertyWriter> props)
protected PropertyBuilder
constructPropertyBuilder(SerializationConfig config, BeanDescription beanDesc)
JsonSerializer<java.lang.Object>
createSerializer(SerializerProvider prov, JavaType origType)
Main serializer constructor method.protected java.lang.Iterable<Serializers>
customSerializers()
protected java.util.List<BeanPropertyWriter>
filterBeanProperties(SerializationConfig config, BeanDescription beanDesc, java.util.List<BeanPropertyWriter> props)
Overridable method that can filter out properties.JsonSerializer<java.lang.Object>
findBeanOrAddOnSerializer(SerializerProvider prov, JavaType type, BeanDescription beanDesc, boolean staticTyping)
Method that will try to construct aBeanSerializer
for given class if at least one property is found, OR, if not, one of add-on types.protected java.util.List<BeanPropertyWriter>
findBeanProperties(SerializerProvider prov, BeanDescription beanDesc, BeanSerializerBuilder builder)
Method used to collect all actual serializable properties.JsonSerializer<java.lang.Object>
findBeanSerializer(SerializerProvider prov, JavaType type, BeanDescription beanDesc)
Deprecated.TypeSerializer
findPropertyContentTypeSerializer(JavaType containerType, SerializationConfig config, AnnotatedMember accessor)
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)
Method called to create a type information serializer for values of given non-container property if one is needed.protected boolean
isPotentialBeanType(java.lang.Class<?> type)
Helper method used to skip processing for types that we know cannot 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 void
removeIgnorableTypes(SerializationConfig config, BeanDescription beanDesc, java.util.List<BeanPropertyDefinition> properties)
Method that will apply by-type limitations (as per [JACKSON-429]); by default this is based onJsonIgnoreType
annotation but can be supplied by module-provided introspectors too.protected java.util.List<BeanPropertyWriter>
removeOverlappingTypeIds(SerializerProvider prov, BeanDescription beanDesc, BeanSerializerBuilder builder, java.util.List<BeanPropertyWriter> props)
Helper method called to ensure that we do not have "duplicate" type ids.protected void
removeSetterlessGetters(SerializationConfig config, BeanDescription beanDesc, java.util.List<BeanPropertyDefinition> properties)
Helper method that will remove all properties that do not have a mutator.SerializerFactory
withConfig(SerializerFactoryConfig config)
Method used by module registration functionality, to attach additional serializer providers into this serializer factory.-
Methods inherited from class com.fasterxml.jackson.databind.ser.BasicSerializerFactory
_checkMapContentInclusion, _findContentSerializer, _findInclusionWithContent, _findKeySerializer, buildArraySerializer, buildAtomicReferenceSerializer, buildCollectionSerializer, buildCollectionSerializer, buildContainerSerializer, buildEnumSerializer, buildEnumSetSerializer, buildIndexedListSerializer, buildIterableSerializer, buildIteratorSerializer, buildMapEntrySerializer, buildMapSerializer, createKeySerializer, createKeySerializer, createTypeSerializer, findConverter, findConvertingSerializer, findFilterId, findOptionalStdSerializer, findReferenceSerializer, findSerializerByAddonType, findSerializerByAnnotations, findSerializerByLookup, findSerializerByPrimaryType, findSerializerFromAnnotation, getFactoryConfig, isIndexedList, usesStaticTyping, withAdditionalKeySerializers, withAdditionalSerializers, withSerializerModifier
-
-
-
-
Field Detail
-
instance
public static final BeanSerializerFactory instance
LikeBasicSerializerFactory
, this factory is stateless, and thus a single shared global (== singleton) instance can be used without thread-safety issues.
-
-
Constructor Detail
-
BeanSerializerFactory
protected BeanSerializerFactory(SerializerFactoryConfig config)
Constructor for creating instances with specified configuration.
-
-
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 classBasicSerializerFactory
-
customSerializers
protected java.lang.Iterable<Serializers> customSerializers()
- Specified by:
customSerializers
in classBasicSerializerFactory
-
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 classBasicSerializerFactory
- Parameters:
prov
- Provider that needs to be used to resolve annotation-provided serializers (but NOT for others)- Throws:
JsonMappingException
-
_createSerializer2
protected JsonSerializer<?> _createSerializer2(SerializerProvider prov, JavaType type, BeanDescription beanDesc, boolean staticTyping) throws JsonMappingException
- Throws:
JsonMappingException
-
findBeanSerializer
@Deprecated public JsonSerializer<java.lang.Object> findBeanSerializer(SerializerProvider prov, JavaType type, BeanDescription beanDesc) throws JsonMappingException
Deprecated.- Throws:
JsonMappingException
-
findBeanOrAddOnSerializer
public JsonSerializer<java.lang.Object> findBeanOrAddOnSerializer(SerializerProvider prov, JavaType type, BeanDescription beanDesc, boolean staticTyping) throws JsonMappingException
Method that will try to construct aBeanSerializer
for given class if at least one property is found, OR, if not, one of add-on types.NOTE: behavior changed a bit
- 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
-
constructBeanSerializer
@Deprecated protected JsonSerializer<java.lang.Object> constructBeanSerializer(SerializerProvider prov, BeanDescription beanDesc) throws JsonMappingException
Deprecated.- Throws:
JsonMappingException
-
constructBeanOrAddOnSerializer
protected JsonSerializer<java.lang.Object> constructBeanOrAddOnSerializer(SerializerProvider prov, JavaType type, BeanDescription beanDesc, boolean staticTyping) throws JsonMappingException
Method called to construct serializer for serializing specified bean type if (but only if, as of 2.10), at least one property is found.- Throws:
JsonMappingException
- Since:
- 2.10
-
constructObjectIdHandler
protected ObjectIdWriter constructObjectIdHandler(SerializerProvider prov, BeanDescription beanDesc, java.util.List<BeanPropertyWriter> props) throws JsonMappingException
- Throws:
JsonMappingException
-
constructFilteredBeanWriter
protected BeanPropertyWriter constructFilteredBeanWriter(BeanPropertyWriter writer, java.lang.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, BeanDescription beanDesc)
-
constructBeanSerializerBuilder
protected BeanSerializerBuilder constructBeanSerializerBuilder(BeanDescription beanDesc)
-
isPotentialBeanType
protected boolean isPotentialBeanType(java.lang.Class<?> type)
Helper method used to skip processing for types that we know cannot 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 java.util.List<BeanPropertyWriter> findBeanProperties(SerializerProvider prov, BeanDescription beanDesc, BeanSerializerBuilder builder) throws JsonMappingException
Method used to collect all actual serializable properties. Can be overridden to implement custom detection schemes.- Throws:
JsonMappingException
-
filterBeanProperties
protected java.util.List<BeanPropertyWriter> filterBeanProperties(SerializationConfig config, BeanDescription beanDesc, java.util.List<BeanPropertyWriter> props)
Overridable method that can filter out properties. Default implementation checks annotations class may have.
-
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.
-
removeIgnorableTypes
protected void removeIgnorableTypes(SerializationConfig config, BeanDescription beanDesc, java.util.List<BeanPropertyDefinition> properties)
Method that will apply by-type limitations (as per [JACKSON-429]); by default this is based onJsonIgnoreType
annotation but can be supplied by module-provided introspectors too. Starting with 2.8 there are also "Config overrides" to consider.
-
removeSetterlessGetters
protected void removeSetterlessGetters(SerializationConfig config, BeanDescription beanDesc, java.util.List<BeanPropertyDefinition> properties)
Helper method that will remove all properties that do not have a mutator.
-
removeOverlappingTypeIds
protected java.util.List<BeanPropertyWriter> removeOverlappingTypeIds(SerializerProvider prov, BeanDescription beanDesc, BeanSerializerBuilder builder, java.util.List<BeanPropertyWriter> props)
Helper method called to ensure that we do not have "duplicate" type ids. Added to resolve [databind#222]- Since:
- 2.6
-
_constructWriter
protected BeanPropertyWriter _constructWriter(SerializerProvider prov, BeanPropertyDefinition propDef, PropertyBuilder pb, boolean staticTyping, AnnotatedMember accessor) throws JsonMappingException
Secondary helper method for constructingBeanPropertyWriter
for given member (field or method).- Throws:
JsonMappingException
-
_findUnsupportedTypeSerializer
protected JsonSerializer<?> _findUnsupportedTypeSerializer(SerializerProvider ctxt, JavaType type, BeanDescription beanDesc) throws JsonMappingException
- Throws:
JsonMappingException
-
-