Class StdSerializerProvider
- java.lang.Object
-
- org.codehaus.jackson.map.SerializerProvider
-
- org.codehaus.jackson.map.ser.StdSerializerProvider
-
public class StdSerializerProvider extends SerializerProvider
DefaultSerializerProviderimplementation. Handles caching aspects of serializer handling; all construction details are delegated toSerializerFactoryinstance.One note about implementation: the main instance constructed will be so-called "blueprint" object, and will NOT be used during actual serialization. Rather, an "instance" instance is created so that state can be carried along, as well as to avoid synchronization during serializer access. Because of this, if sub-classing, one must override method
createInstance(org.codehaus.jackson.map.SerializationConfig, org.codehaus.jackson.map.SerializerFactory): if this is not done, an exception will get thrown as base class verifies that the instance has same class as the blueprint (instance.getClass() == blueprint.getClass()). Check is done to prevent weird bugs that would otherwise occur.Starting with version 1.5, provider is also responsible for some parts of type serialization; specifically for locating proper type serializers to use for types.
-
-
Field Summary
Fields Modifier and Type Field Description protected DateFormat_dateFormatLazily acquired and instantiated formatter object: initialized first time it is needed, reused afterwards.protected JsonSerializer<Object>_keySerializerSerializer used to output non-null keys of Maps (which will get output as JSON Objects), if not null; if null, us the standard default key serializer.protected ReadOnlyClassToSerializerMap_knownSerializersFor fast lookups, we will have a local non-shared read-only map that contains serializers previously fetched.protected JsonSerializer<Object>_nullKeySerializerSerializer used to (try to) output a null key, due to an entry ofMaphaving null key.protected JsonSerializer<Object>_nullValueSerializerSerializer used to output a null value.protected RootNameLookup_rootNamesHelper object for keeping track of introspected root namesprotected SerializerCache_serializerCacheCache for doing type-to-value-serializer lookups.protected SerializerFactory_serializerFactoryprotected JsonSerializer<Object>_unknownTypeSerializerSerializer that gets called for values of types for which no serializers can be constructed.static JsonSerializer<Object>DEFAULT_KEY_SERIALIZERDeprecated.Since 1.9, useStdKeySerializersinsteadstatic JsonSerializer<Object>DEFAULT_NULL_KEY_SERIALIZERstatic JsonSerializer<Object>DEFAULT_UNKNOWN_SERIALIZER-
Fields inherited from class org.codehaus.jackson.map.SerializerProvider
_config, _serializationView, TYPE_OBJECT
-
-
Constructor Summary
Constructors Modifier Constructor Description StdSerializerProvider()Constructor for creating master (or "blue-print") provider object, which is only used as the template for constructing per-binding instances.protectedStdSerializerProvider(SerializationConfig config, StdSerializerProvider src, SerializerFactory f)"Copy-constructor", used fromcreateInstance(org.codehaus.jackson.map.SerializationConfig, org.codehaus.jackson.map.SerializerFactory)(or by sub-classes)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected JsonSerializer<Object>_createAndCacheUntypedSerializer(Class<?> type, BeanProperty property)Method that will try to construct a value serializer; and if one is successfully created, cache it for reuse.protected JsonSerializer<Object>_createAndCacheUntypedSerializer(JavaType type, BeanProperty property)protected JsonSerializer<Object>_createUntypedSerializer(JavaType type, BeanProperty property)protected JsonSerializer<Object>_findExplicitUntypedSerializer(Class<?> runtimeType, BeanProperty property)Method that will try to find a serializer, either from cache or by constructing one; but will not return an "unknown" serializer if this can not be done but rather returns null.protected JsonSerializer<Object>_handleContextualResolvable(JsonSerializer<Object> ser, BeanProperty property)protected void_reportIncompatibleRootType(Object value, JavaType rootType)protected void_serializeValue(JsonGenerator jgen, Object value)Method called on the actual non-blueprint provider instance object, to kick off the serialization.protected void_serializeValue(JsonGenerator jgen, Object value, JavaType rootType)Method called on the actual non-blueprint provider instance object, to kick off the serialization, when root type is explicitly specified and not determined from value.intcachedSerializersCount()Method that can be used to determine how many serializers this provider is caching currently (if it does caching: default implementation does) Exact count depends on what kind of serializers get cached; default implementation caches all serializers, including ones that are eagerly constructed (for optimal access speed)protected StdSerializerProvidercreateInstance(SerializationConfig config, SerializerFactory jsf)Overridable method, used to create a non-blueprint instances from the blueprint.voiddefaultSerializeDateKey(long timestamp, JsonGenerator jgen)Method that will handle serialization of Dates used asMapkeys, based onSerializationConfig.Feature.WRITE_DATE_KEYS_AS_TIMESTAMPSvalue (and if using textual representation, configured date format)voiddefaultSerializeDateKey(Date date, JsonGenerator jgen)Method that will handle serialization of Dates used asMapkeys, based onSerializationConfig.Feature.WRITE_DATE_KEYS_AS_TIMESTAMPSvalue (and if using textual representation, configured date format)voiddefaultSerializeDateValue(long timestamp, JsonGenerator jgen)Method that will handle serialization of Date(-like) values, usingSerializationConfigsettings to determine expected serialization behavior.voiddefaultSerializeDateValue(Date date, JsonGenerator jgen)Method that will handle serialization of Date(-like) values, usingSerializationConfigsettings to determine expected serialization behavior.JsonSerializer<Object>findKeySerializer(JavaType keyType, BeanProperty property)Method called to get the serializer to use for serializing non-null Map keys.JsonSerializer<Object>findTypedValueSerializer(Class<?> valueType, boolean cache, BeanProperty property)Method called to locate regular serializer, matching type serializer, and if both found, wrap them in a serializer that calls both in correct sequence.JsonSerializer<Object>findTypedValueSerializer(JavaType valueType, boolean cache, BeanProperty property)Method called to locate regular serializer, matching type serializer, and if both found, wrap them in a serializer that calls both in correct sequence.JsonSerializer<Object>findValueSerializer(Class<?> valueType, BeanProperty property)Method called to get hold of a serializer for a value of given type; or if no such serializer can be found, a default handler (which may do a best-effort generic serialization or just simply throw an exception when invoked).JsonSerializer<Object>findValueSerializer(JavaType valueType, BeanProperty property)This variant was added in 1.5, to allow for efficient access using full structured types, not just classes.voidflushCachedSerializers()Method that will drop all serializers currently cached by this provider.JsonSchemagenerateJsonSchema(Class<?> type, SerializationConfig config, SerializerFactory jsf)Generate Json-schema for given type.JsonSerializer<Object>getNullKeySerializer()Method called to get the serializer to use for serializing Map keys that are nulls: this is needed since JSON does not allow any non-String value as key, including null.JsonSerializer<Object>getNullValueSerializer()Method called to get the serializer to use for serializing values (root level, Array members or List field values) that are nulls.JsonSerializer<Object>getUnknownTypeSerializer(Class<?> unknownType)Method called to get the serializer to use if provider can not determine an actual type-specific serializer to use; typically when none ofSerializerFactoryinstances are able to construct a serializer.booleanhasSerializerFor(SerializationConfig config, Class<?> cls, SerializerFactory jsf)Method that can be called to see if this serializer provider can find a serializer for an instance of given class.voidserializeValue(SerializationConfig config, JsonGenerator jgen, Object value, SerializerFactory jsf)The method to be called byObjectMapperto execute recursive serialization, using serializers that this provider has access to.voidserializeValue(SerializationConfig config, JsonGenerator jgen, Object value, JavaType rootType, SerializerFactory jsf)The method to be called byObjectMapperto execute recursive serialization, using serializers that this provider has access to; and using specified root type for locating first-level serializer.voidsetDefaultKeySerializer(JsonSerializer<Object> ks)Method that can be used to specify serializer to use for serializing all non-null JSON property names, unless more specific key serializer is found (i.e.voidsetNullKeySerializer(JsonSerializer<Object> nks)Method that can be used to specify serializer that will be used to write JSON property names matching null keys for Java Maps (which will throw an exception if try write such property name)voidsetNullValueSerializer(JsonSerializer<Object> nvs)Method that can be used to specify serializer that will be used to write JSON values matching Java null values instead of default one (which simply writes JSON null)-
Methods inherited from class org.codehaus.jackson.map.SerializerProvider
constructSpecializedType, constructType, defaultSerializeField, defaultSerializeNull, defaultSerializeValue, findTypedValueSerializer, findTypedValueSerializer, findValueSerializer, findValueSerializer, getConfig, getFilterProvider, getKeySerializer, getKeySerializer, getSerializationView, isEnabled
-
-
-
-
Field Detail
-
DEFAULT_NULL_KEY_SERIALIZER
public static final JsonSerializer<Object> DEFAULT_NULL_KEY_SERIALIZER
-
DEFAULT_KEY_SERIALIZER
@Deprecated public static final JsonSerializer<Object> DEFAULT_KEY_SERIALIZER
Deprecated.Since 1.9, useStdKeySerializersinstead
-
DEFAULT_UNKNOWN_SERIALIZER
public static final JsonSerializer<Object> DEFAULT_UNKNOWN_SERIALIZER
-
_serializerFactory
protected final SerializerFactory _serializerFactory
-
_serializerCache
protected final SerializerCache _serializerCache
Cache for doing type-to-value-serializer lookups.
-
_rootNames
protected final RootNameLookup _rootNames
Helper object for keeping track of introspected root names
-
_unknownTypeSerializer
protected JsonSerializer<Object> _unknownTypeSerializer
Serializer that gets called for values of types for which no serializers can be constructed.The default serializer will simply thrown an exception; a possible alternative that can be used would be
ToStringSerializer.
-
_keySerializer
protected JsonSerializer<Object> _keySerializer
Serializer used to output non-null keys of Maps (which will get output as JSON Objects), if not null; if null, us the standard default key serializer.
-
_nullValueSerializer
protected JsonSerializer<Object> _nullValueSerializer
Serializer used to output a null value. Default implementation writes nulls usingJsonGenerator.writeNull().
-
_nullKeySerializer
protected JsonSerializer<Object> _nullKeySerializer
Serializer used to (try to) output a null key, due to an entry ofMaphaving null key. The default implementation will throw an exception if this happens; alternative implementation (like one that would write an Empty String) can be defined.
-
_knownSerializers
protected final ReadOnlyClassToSerializerMap _knownSerializers
For fast lookups, we will have a local non-shared read-only map that contains serializers previously fetched.
-
_dateFormat
protected DateFormat _dateFormat
Lazily acquired and instantiated formatter object: initialized first time it is needed, reused afterwards. Used via instances (not blueprints), so that access need not be thread-safe.
-
-
Constructor Detail
-
StdSerializerProvider
public StdSerializerProvider()
Constructor for creating master (or "blue-print") provider object, which is only used as the template for constructing per-binding instances.
-
StdSerializerProvider
protected StdSerializerProvider(SerializationConfig config, StdSerializerProvider src, SerializerFactory f)
"Copy-constructor", used fromcreateInstance(org.codehaus.jackson.map.SerializationConfig, org.codehaus.jackson.map.SerializerFactory)(or by sub-classes)- Parameters:
src- Blueprint object used as the baseline for this instance
-
-
Method Detail
-
createInstance
protected StdSerializerProvider createInstance(SerializationConfig config, SerializerFactory jsf)
Overridable method, used to create a non-blueprint instances from the blueprint. This is needed to retain state during serialization.
-
setDefaultKeySerializer
public void setDefaultKeySerializer(JsonSerializer<Object> ks)
Description copied from class:SerializerProviderMethod that can be used to specify serializer to use for serializing all non-null JSON property names, unless more specific key serializer is found (i.e. if not custom key serializer has been registered for Java type).Note that key serializer registration are different from value serializer registrations.
- Specified by:
setDefaultKeySerializerin classSerializerProvider
-
setNullValueSerializer
public void setNullValueSerializer(JsonSerializer<Object> nvs)
Description copied from class:SerializerProviderMethod that can be used to specify serializer that will be used to write JSON values matching Java null values instead of default one (which simply writes JSON null)- Specified by:
setNullValueSerializerin classSerializerProvider
-
setNullKeySerializer
public void setNullKeySerializer(JsonSerializer<Object> nks)
Description copied from class:SerializerProviderMethod that can be used to specify serializer that will be used to write JSON property names matching null keys for Java Maps (which will throw an exception if try write such property name)- Specified by:
setNullKeySerializerin classSerializerProvider
-
serializeValue
public final void serializeValue(SerializationConfig config, JsonGenerator jgen, Object value, SerializerFactory jsf) throws IOException, JsonGenerationException
Description copied from class:SerializerProviderThe method to be called byObjectMapperto execute recursive serialization, using serializers that this provider has access to.- Specified by:
serializeValuein classSerializerProviderjsf- Underlying factory object used for creating serializers as needed- Throws:
IOExceptionJsonGenerationException
-
serializeValue
public final void serializeValue(SerializationConfig config, JsonGenerator jgen, Object value, JavaType rootType, SerializerFactory jsf) throws IOException, JsonGenerationException
Description copied from class:SerializerProviderThe method to be called byObjectMapperto execute recursive serialization, using serializers that this provider has access to; and using specified root type for locating first-level serializer.- Specified by:
serializeValuein classSerializerProviderrootType- Type to use for locating serializer to use, instead of actual runtime type. Must be actual type, or one of its super types- Throws:
IOExceptionJsonGenerationException
-
generateJsonSchema
public JsonSchema generateJsonSchema(Class<?> type, SerializationConfig config, SerializerFactory jsf) throws JsonMappingException
Description copied from class:SerializerProviderGenerate Json-schema for given type.- Specified by:
generateJsonSchemain classSerializerProvider- Parameters:
type- The type for which to generate schema- Throws:
JsonMappingException
-
hasSerializerFor
public boolean hasSerializerFor(SerializationConfig config, Class<?> cls, SerializerFactory jsf)
Description copied from class:SerializerProviderMethod that can be called to see if this serializer provider can find a serializer for an instance of given class.Note that no Exceptions are thrown, including unchecked ones: implementations are to swallow exceptions if necessary.
- Specified by:
hasSerializerForin classSerializerProvider
-
cachedSerializersCount
public int cachedSerializersCount()
Description copied from class:SerializerProviderMethod that can be used to determine how many serializers this provider is caching currently (if it does caching: default implementation does) Exact count depends on what kind of serializers get cached; default implementation caches all serializers, including ones that are eagerly constructed (for optimal access speed)The main use case for this method is to allow conditional flushing of serializer cache, if certain number of entries is reached.
- Specified by:
cachedSerializersCountin classSerializerProvider
-
flushCachedSerializers
public void flushCachedSerializers()
Description copied from class:SerializerProviderMethod that will drop all serializers currently cached by this provider. This can be used to remove memory usage (in case some serializers are only used once or so), or to force re-construction of serializers after configuration changes for mapper than owns the provider.- Specified by:
flushCachedSerializersin classSerializerProvider
-
findValueSerializer
public JsonSerializer<Object> findValueSerializer(Class<?> valueType, BeanProperty property) throws JsonMappingException
Description copied from class:SerializerProviderMethod called to get hold of a serializer for a value of given type; or if no such serializer can be found, a default handler (which may do a best-effort generic serialization or just simply throw an exception when invoked).Note: this method is only called for non-null values; not for keys or null values. For these, check out other accessor methods.
Note that starting with version 1.5, serializers should also be type-aware if they handle polymorphic types. That means that it may be necessary to also use a
TypeSerializerbased on declared (static) type being serializer (whereas actual data may be serialized using dynamic type)- Specified by:
findValueSerializerin classSerializerProvider- Throws:
JsonMappingException- if there are fatal problems with accessing suitable serializer; including that of not finding any serializer
-
findValueSerializer
public JsonSerializer<Object> findValueSerializer(JavaType valueType, BeanProperty property) throws JsonMappingException
This variant was added in 1.5, to allow for efficient access using full structured types, not just classes. This is necessary for accurate handling of external type information, to handle polymorphic types.- Specified by:
findValueSerializerin classSerializerProvider- Throws:
JsonMappingException
-
findTypedValueSerializer
public JsonSerializer<Object> findTypedValueSerializer(Class<?> valueType, boolean cache, BeanProperty property) throws JsonMappingException
Description copied from class:SerializerProviderMethod called to locate regular serializer, matching type serializer, and if both found, wrap them in a serializer that calls both in correct sequence. This method is currently only used for root-level serializer handling to allow for simpler caching. A call can always be replaced by equivalent calls to access serializer and type serializer separately.- Specified by:
findTypedValueSerializerin classSerializerProvider- Parameters:
cache- Whether resulting value serializer should be cached or not; this is just a hintvalueType- Type for purpose of locating a serializer; usually dynamic runtime type, but can also be static declared type, depending on configuration- Throws:
JsonMappingException
-
findTypedValueSerializer
public JsonSerializer<Object> findTypedValueSerializer(JavaType valueType, boolean cache, BeanProperty property) throws JsonMappingException
Description copied from class:SerializerProviderMethod called to locate regular serializer, matching type serializer, and if both found, wrap them in a serializer that calls both in correct sequence. This method is currently only used for root-level serializer handling to allow for simpler caching. A call can always be replaced by equivalent calls to access serializer and type serializer separately.- Specified by:
findTypedValueSerializerin classSerializerProvider- Parameters:
valueType- Declared type of value being serialized (which may not be actual runtime type); used for finding both value serializer and type serializer to use for adding polymorphic type (if any)cache- Whether resulting value serializer should be cached or not; this is just a hint- Throws:
JsonMappingException
-
findKeySerializer
public JsonSerializer<Object> findKeySerializer(JavaType keyType, BeanProperty property) throws JsonMappingException
Description copied from class:SerializerProviderMethod called to get the serializer to use for serializing non-null Map keys. Separation from regularSerializerProvider.findValueSerializer(java.lang.Class<?>, org.codehaus.jackson.map.BeanProperty)method is because actual write method must be different (@link JsonGenerator#writeFieldName}; but also since behavior for some key types may differ.Note that the serializer itself can be called with instances of any Java object, but not nulls.
- Specified by:
findKeySerializerin classSerializerProvider- Throws:
JsonMappingException
-
getNullKeySerializer
public JsonSerializer<Object> getNullKeySerializer()
Description copied from class:SerializerProviderMethod called to get the serializer to use for serializing Map keys that are nulls: this is needed since JSON does not allow any non-String value as key, including null.Typically, returned serializer will either throw an exception, or use an empty String; but other behaviors are possible.
- Specified by:
getNullKeySerializerin classSerializerProvider
-
getNullValueSerializer
public JsonSerializer<Object> getNullValueSerializer()
Description copied from class:SerializerProviderMethod called to get the serializer to use for serializing values (root level, Array members or List field values) that are nulls. Specific accessor is needed because nulls in Java do not contain type information.Typically returned serializer just writes out Json literal null value.
- Specified by:
getNullValueSerializerin classSerializerProvider
-
getUnknownTypeSerializer
public JsonSerializer<Object> getUnknownTypeSerializer(Class<?> unknownType)
Description copied from class:SerializerProviderMethod called to get the serializer to use if provider can not determine an actual type-specific serializer to use; typically when none ofSerializerFactoryinstances are able to construct a serializer.Typically, returned serializer will throw an exception, although alternatively
ToStringSerializercould be returned as well.- Specified by:
getUnknownTypeSerializerin classSerializerProvider- Parameters:
unknownType- Type for which no serializer is found
-
defaultSerializeDateValue
public final void defaultSerializeDateValue(long timestamp, JsonGenerator jgen) throws IOException, JsonProcessingExceptionDescription copied from class:SerializerProviderMethod that will handle serialization of Date(-like) values, usingSerializationConfigsettings to determine expected serialization behavior. Note: date here means "full" date, that is, date AND time, as per Java convention (and not date-only values like in SQL)- Specified by:
defaultSerializeDateValuein classSerializerProvider- Parameters:
timestamp- Millisecond timestamp that defines date, if available;- Throws:
IOExceptionJsonProcessingException
-
defaultSerializeDateValue
public final void defaultSerializeDateValue(Date date, JsonGenerator jgen) throws IOException, JsonProcessingException
Description copied from class:SerializerProviderMethod that will handle serialization of Date(-like) values, usingSerializationConfigsettings to determine expected serialization behavior. Note: date here means "full" date, that is, date AND time, as per Java convention (and not date-only values like in SQL)- Specified by:
defaultSerializeDateValuein classSerializerProvider- Throws:
IOExceptionJsonProcessingException
-
defaultSerializeDateKey
public void defaultSerializeDateKey(long timestamp, JsonGenerator jgen) throws IOException, JsonProcessingExceptionDescription copied from class:SerializerProviderMethod that will handle serialization of Dates used asMapkeys, based onSerializationConfig.Feature.WRITE_DATE_KEYS_AS_TIMESTAMPSvalue (and if using textual representation, configured date format)- Specified by:
defaultSerializeDateKeyin classSerializerProvider- Throws:
IOExceptionJsonProcessingException
-
defaultSerializeDateKey
public void defaultSerializeDateKey(Date date, JsonGenerator jgen) throws IOException, JsonProcessingException
Description copied from class:SerializerProviderMethod that will handle serialization of Dates used asMapkeys, based onSerializationConfig.Feature.WRITE_DATE_KEYS_AS_TIMESTAMPSvalue (and if using textual representation, configured date format)- Specified by:
defaultSerializeDateKeyin classSerializerProvider- Throws:
IOExceptionJsonProcessingException
-
_serializeValue
protected void _serializeValue(JsonGenerator jgen, Object value) throws IOException, JsonProcessingException
Method called on the actual non-blueprint provider instance object, to kick off the serialization.- Throws:
IOExceptionJsonProcessingException
-
_serializeValue
protected void _serializeValue(JsonGenerator jgen, Object value, JavaType rootType) throws IOException, JsonProcessingException
Method called on the actual non-blueprint provider instance object, to kick off the serialization, when root type is explicitly specified and not determined from value.- Throws:
IOExceptionJsonProcessingException
-
_reportIncompatibleRootType
protected void _reportIncompatibleRootType(Object value, JavaType rootType) throws IOException, JsonProcessingException
- Throws:
IOExceptionJsonProcessingException
-
_findExplicitUntypedSerializer
protected JsonSerializer<Object> _findExplicitUntypedSerializer(Class<?> runtimeType, BeanProperty property)
Method that will try to find a serializer, either from cache or by constructing one; but will not return an "unknown" serializer if this can not be done but rather returns null.- Returns:
- Serializer if one can be found, null if not.
-
_createAndCacheUntypedSerializer
protected JsonSerializer<Object> _createAndCacheUntypedSerializer(Class<?> type, BeanProperty property) throws JsonMappingException
Method that will try to construct a value serializer; and if one is successfully created, cache it for reuse.- Throws:
JsonMappingException
-
_createAndCacheUntypedSerializer
protected JsonSerializer<Object> _createAndCacheUntypedSerializer(JavaType type, BeanProperty property) throws JsonMappingException
- Throws:
JsonMappingException- Since:
- 1.5 ]
-
_createUntypedSerializer
protected JsonSerializer<Object> _createUntypedSerializer(JavaType type, BeanProperty property) throws JsonMappingException
- Throws:
JsonMappingException
-
_handleContextualResolvable
protected JsonSerializer<Object> _handleContextualResolvable(JsonSerializer<Object> ser, BeanProperty property) throws JsonMappingException
- Throws:
JsonMappingException- Since:
- 1.8.5
-
-