|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.codehaus.jackson.map.SerializerProvider
org.codehaus.jackson.map.ser.StdSerializerProvider
public class StdSerializerProvider
Default SerializerProvider
implementation. Handles
caching aspects of serializer handling; all construction details
are delegated to SerializerFactory
instance.
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 | |
---|---|
protected DateFormat |
_dateFormat
Lazily acquired and instantiated formatter object: initialized first time it is needed, reused afterwards. |
protected JsonSerializer<Object> |
_keySerializer
Serializer used to output non-null keys of Maps (which will get output as Json Objects). |
protected ReadOnlyClassToSerializerMap |
_knownSerializers
For fast lookups, we will have a local non-shared read-only map that contains serializers previously fetched. |
protected JsonSerializer<Object> |
_nullKeySerializer
Serializer used to (try to) output a null key, due to an entry of Map having null key. |
protected JsonSerializer<Object> |
_nullValueSerializer
Serializer used to output a null value. |
protected SerializerCache |
_serializerCache
|
protected SerializerFactory |
_serializerFactory
|
protected JsonSerializer<Object> |
_unknownTypeSerializer
Serializer that gets called for values of types for which no serializers can be constructed. |
static JsonSerializer<Object> |
DEFAULT_KEY_SERIALIZER
|
static JsonSerializer<Object> |
DEFAULT_NULL_KEY_SERIALIZER
|
static JsonSerializer<Object> |
DEFAULT_UNKNOWN_SERIALIZER
|
Fields inherited from class org.codehaus.jackson.map.SerializerProvider |
---|
_config, _serializationView |
Constructor Summary | |
---|---|
|
StdSerializerProvider()
Constructor for creating master (or "blue-print") provider object, which is only used as the template for constructing per-binding instances. |
protected |
StdSerializerProvider(SerializationConfig config,
StdSerializerProvider src,
SerializerFactory f)
"Copy-constructor", used from createInstance(org.codehaus.jackson.map.SerializationConfig, org.codehaus.jackson.map.SerializerFactory) (or by
sub-classes) |
Method Summary | |
---|---|
protected JsonSerializer<Object> |
_createAndCacheUntypedSerializer(Class<?> type)
Method that will try to construct a value serializer; and if one is succesfully created, cache it for reuse. |
protected JsonSerializer<Object> |
_createAndCacheUntypedSerializer(JavaType type)
|
protected JsonSerializer<Object> |
_createUntypedSerializer(JavaType type)
|
protected JsonSerializer<Object> |
_findExplicitUntypedSerializer(Class<?> runtimeType)
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 void |
_resolveSerializer(ResolvableSerializer ser)
|
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. |
int |
cachedSerializersCount()
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 StdSerializerProvider |
createInstance(SerializationConfig config,
SerializerFactory jsf)
Overridable method, used to create a non-blueprint instances from the blueprint. |
void |
defaultSerializeDateValue(Date date,
JsonGenerator jgen)
Method that will handle serialization of Date(-like) values, using SerializationConfig settings to determine expected serialization
behavior. |
void |
defaultSerializeDateValue(long timestamp,
JsonGenerator jgen)
Method that will handle serialization of Date(-like) values, using SerializationConfig settings to determine expected serialization
behavior. |
JsonSerializer<Object> |
findTypedValueSerializer(Class<?> valueType,
boolean cache)
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)
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)
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)
This variant was added in 1.5, to allow for efficient access using full structured types, not just classes. |
void |
flushCachedSerializers()
Method that will drop all serializers currently cached by this provider. |
JsonSchema |
generateJsonSchema(Class<?> type,
SerializationConfig config,
SerializerFactory jsf)
Generate Json-schema for given type. |
JsonSerializer<Object> |
getKeySerializer()
Method called to get the serializer to use for serializing non-null Map keys. |
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 of SerializerFactory
instances are able to construct a serializer. |
boolean |
hasSerializerFor(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. |
void |
serializeValue(SerializationConfig config,
JsonGenerator jgen,
Object value,
JavaType rootType,
SerializerFactory jsf)
The method to be called by ObjectMapper to
execute recursive serialization, using serializers that
this provider has access to; and using specified root type
for locating first-level serializer. |
void |
serializeValue(SerializationConfig config,
JsonGenerator jgen,
Object value,
SerializerFactory jsf)
The method to be called by ObjectMapper to
execute recursive serialization, using serializers that
this provider has access to. |
void |
setKeySerializer(JsonSerializer<Object> ks)
|
void |
setNullKeySerializer(JsonSerializer<Object> nks)
|
void |
setNullValueSerializer(JsonSerializer<Object> nvs)
|
Methods inherited from class org.codehaus.jackson.map.SerializerProvider |
---|
defaultSerializeField, defaultSerializeValue, getConfig, getSerializationView, isEnabled |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final JsonSerializer<Object> DEFAULT_NULL_KEY_SERIALIZER
public static final JsonSerializer<Object> DEFAULT_KEY_SERIALIZER
public static final JsonSerializer<Object> DEFAULT_UNKNOWN_SERIALIZER
protected final SerializerFactory _serializerFactory
protected final SerializerCache _serializerCache
protected JsonSerializer<Object> _unknownTypeSerializer
The default serializer will simply thrown an exception; a possible
alternative that can be used would be
ToStringSerializer
.
protected JsonSerializer<Object> _keySerializer
protected JsonSerializer<Object> _nullValueSerializer
JsonGenerator.writeNull()
.
protected JsonSerializer<Object> _nullKeySerializer
Map
having 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.
protected final ReadOnlyClassToSerializerMap _knownSerializers
protected DateFormat _dateFormat
Constructor Detail |
---|
public StdSerializerProvider()
protected StdSerializerProvider(SerializationConfig config, StdSerializerProvider src, SerializerFactory f)
createInstance(org.codehaus.jackson.map.SerializationConfig, org.codehaus.jackson.map.SerializerFactory)
(or by
sub-classes)
src
- Blueprint object used as the baseline for this instanceMethod Detail |
---|
protected StdSerializerProvider createInstance(SerializationConfig config, SerializerFactory jsf)
public final void serializeValue(SerializationConfig config, JsonGenerator jgen, Object value, SerializerFactory jsf) throws IOException, JsonGenerationException
SerializerProvider
ObjectMapper
to
execute recursive serialization, using serializers that
this provider has access to.
serializeValue
in class SerializerProvider
jsf
- Underlying factory object used for creating serializers
as needed
IOException
JsonGenerationException
public final void serializeValue(SerializationConfig config, JsonGenerator jgen, Object value, JavaType rootType, SerializerFactory jsf) throws IOException, JsonGenerationException
SerializerProvider
ObjectMapper
to
execute recursive serialization, using serializers that
this provider has access to; and using specified root type
for locating first-level serializer.
serializeValue
in class SerializerProvider
rootType
- Type to use for locating serializer to use, instead of actual
runtime type. Must be actual type, or one of its super types
IOException
JsonGenerationException
public JsonSchema generateJsonSchema(Class<?> type, SerializationConfig config, SerializerFactory jsf) throws JsonMappingException
SerializerProvider
generateJsonSchema
in class SerializerProvider
type
- The type for which to generate schema
JsonMappingException
public boolean hasSerializerFor(SerializationConfig config, Class<?> cls, SerializerFactory jsf)
SerializerProvider
Note that no Exceptions are thrown, including unchecked ones: implementations are to swallow exceptions if necessary.
hasSerializerFor
in class SerializerProvider
protected void _serializeValue(JsonGenerator jgen, Object value) throws IOException, JsonProcessingException
IOException
JsonProcessingException
protected void _serializeValue(JsonGenerator jgen, Object value, JavaType rootType) throws IOException, JsonProcessingException
IOException
JsonProcessingException
public void setKeySerializer(JsonSerializer<Object> ks)
public void setNullValueSerializer(JsonSerializer<Object> nvs)
public void setNullKeySerializer(JsonSerializer<Object> nks)
public int cachedSerializersCount()
SerializerProvider
The main use case for this method is to allow conditional flushing of serializer cache, if certain number of entries is reached.
cachedSerializersCount
in class SerializerProvider
public void flushCachedSerializers()
SerializerProvider
flushCachedSerializers
in class SerializerProvider
public JsonSerializer<Object> findValueSerializer(Class<?> valueType) throws JsonMappingException
SerializerProvider
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 TypeSerializer
based on declared (static) type
being serializer (whereas actual data may be serialized using dynamic
type)
findValueSerializer
in class SerializerProvider
JsonMappingException
- if there are fatal problems with
accessing suitable serializer; including that of not
finding any serializerpublic JsonSerializer<Object> findValueSerializer(JavaType valueType) throws JsonMappingException
findValueSerializer
in class SerializerProvider
JsonMappingException
public JsonSerializer<Object> findTypedValueSerializer(Class<?> valueType, boolean cache) throws JsonMappingException
SerializerProvider
findTypedValueSerializer
in class SerializerProvider
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
JsonMappingException
public JsonSerializer<Object> findTypedValueSerializer(JavaType valueType, boolean cache) throws JsonMappingException
SerializerProvider
findTypedValueSerializer
in class SerializerProvider
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
JsonMappingException
public JsonSerializer<Object> getKeySerializer()
SerializerProvider
SerializerProvider.findValueSerializer(java.lang.Class>)
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.
getKeySerializer
in class SerializerProvider
public JsonSerializer<Object> getNullKeySerializer()
SerializerProvider
Typically, returned serializer will either throw an exception, or use an empty String; but other behaviors are possible.
getNullKeySerializer
in class SerializerProvider
public JsonSerializer<Object> getNullValueSerializer()
SerializerProvider
Typically returned serializer just writes out Json literal null value.
getNullValueSerializer
in class SerializerProvider
public JsonSerializer<Object> getUnknownTypeSerializer(Class<?> unknownType)
SerializerProvider
SerializerFactory
instances are able to construct a serializer.
Typically, returned serializer will throw an exception,
although alternatively ToStringSerializer
could
be returned as well.
getUnknownTypeSerializer
in class SerializerProvider
unknownType
- Type for which no serializer is foundpublic final void defaultSerializeDateValue(long timestamp, JsonGenerator jgen) throws IOException, JsonProcessingException
SerializerProvider
SerializationConfig
settings 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)
defaultSerializeDateValue
in class SerializerProvider
timestamp
- Millisecond timestamp that defines date, if available;
IOException
JsonProcessingException
public final void defaultSerializeDateValue(Date date, JsonGenerator jgen) throws IOException, JsonProcessingException
SerializerProvider
SerializationConfig
settings 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)
defaultSerializeDateValue
in class SerializerProvider
IOException
JsonProcessingException
protected JsonSerializer<Object> _findExplicitUntypedSerializer(Class<?> runtimeType)
protected JsonSerializer<Object> _createAndCacheUntypedSerializer(Class<?> type) throws JsonMappingException
JsonMappingException
protected JsonSerializer<Object> _createAndCacheUntypedSerializer(JavaType type) throws JsonMappingException
JsonMappingException
protected JsonSerializer<Object> _createUntypedSerializer(JavaType type) throws JsonMappingException
JsonMappingException
protected void _resolveSerializer(ResolvableSerializer ser) throws JsonMappingException
JsonMappingException
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |