org.codehaus.jackson.map
Class SerializerProvider

java.lang.Object
  extended by org.codehaus.jackson.map.SerializerProvider
Direct Known Subclasses:
StdSerializerProvider

public abstract class SerializerProvider
extends Object

Abstract class that defines API used by ObjectMapper and JsonSerializers to obtain serializers capable of serializing instances of specific types.

Note about usage: for JsonSerializer instances, only accessors for locating other (sub-)serializers are to be used. ObjectMapper, on the other hand, is to initialize recursive serialization process by calling serializeValue(org.codehaus.jackson.map.SerializationConfig, org.codehaus.jackson.JsonGenerator, java.lang.Object, org.codehaus.jackson.map.SerializerFactory).


Field Summary
protected  SerializationConfig _config
           
 
Constructor Summary
protected SerializerProvider(SerializationConfig config)
           
 
Method Summary
abstract  void defaultSerializeDateValue(Date date, JsonGenerator jgen)
          Method that will handle serialization of Date(-like) values, using SerializationConfig settings to determine expected serialization behavior.
abstract  void defaultSerializeDateValue(long timestamp, JsonGenerator jgen)
          Method that will handle serialization of Date(-like) values, using SerializationConfig settings to determine expected serialization behavior.
 void defaultSerializeField(String fieldName, Object value, JsonGenerator jgen)
          Convenience method that will serialize given field with specified value.
 void defaultSerializeValue(Object value, JsonGenerator jgen)
          Convenience method that will serialize given value (which can be null) using standard serializer locating functionality.
abstract  JsonSerializer<Object> findValueSerializer(Class<?> type)
          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).
abstract  JsonSchema generateJsonSchema(Class<?> type, SerializationConfig config, SerializerFactory jsf)
          Generate Json-schema for given type.
 SerializationConfig getConfig()
           
abstract  JsonSerializer<Object> getKeySerializer()
          Method called to get the serializer to use for serializing non-null Map keys.
abstract  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.
abstract  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.
abstract  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.
abstract  boolean hasSerializerFor(SerializationConfig cfg, 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.
 boolean isEnabled(SerializationConfig.Feature f)
           
abstract  void serializeValue(SerializationConfig cfg, 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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_config

protected final SerializationConfig _config
Constructor Detail

SerializerProvider

protected SerializerProvider(SerializationConfig config)
Method Detail

serializeValue

public abstract void serializeValue(SerializationConfig cfg,
                                    JsonGenerator jgen,
                                    Object value,
                                    SerializerFactory jsf)
                             throws IOException,
                                    JsonGenerationException
The method to be called by ObjectMapper to execute recursive serialization, using serializers that this provider has access to.

Parameters:
jsf - Underlying factory object used for creating serializers as needed
Throws:
IOException
JsonGenerationException

generateJsonSchema

public abstract JsonSchema generateJsonSchema(Class<?> type,
                                              SerializationConfig config,
                                              SerializerFactory jsf)
                                       throws JsonMappingException
Generate Json-schema for given type.

Parameters:
type - The type for which to generate schema
Throws:
JsonMappingException

hasSerializerFor

public abstract boolean hasSerializerFor(SerializationConfig cfg,
                                         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.

Note that no Exceptions are thrown, including unchecked ones: implementations are to swallow exceptions if necessary.


getConfig

public final SerializationConfig getConfig()

isEnabled

public final boolean isEnabled(SerializationConfig.Feature f)

findValueSerializer

public abstract JsonSerializer<Object> findValueSerializer(Class<?> type)
                                                    throws JsonMappingException
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).

Note: this method is only called for non-null values; not for keys or null values. For these, check out other accessor methods.

Throws:
JsonMappingException - if there are fatal problems with accessing suitable serializer; including that of not finding any serializer

getKeySerializer

public abstract JsonSerializer<Object> getKeySerializer()
Method called to get the serializer to use for serializing non-null Map keys. Separation from regular 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.


getNullKeySerializer

public abstract 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.

Typically, returned serializer will either throw an exception, or use an empty String; but other behaviors are possible.


getNullValueSerializer

public abstract 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. Specific accessor is needed because nulls in Java do not contain type information.

Typically returned serializer just writes out Json literal null value.


getUnknownTypeSerializer

public abstract 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.

Typically, returned serializer will throw an exception, although alternatively ToStringSerializer could be returned as well.

Parameters:
unknownType - Type for which no serializer is found

defaultSerializeValue

public final void defaultSerializeValue(Object value,
                                        JsonGenerator jgen)
                                 throws IOException,
                                        JsonProcessingException
Convenience method that will serialize given value (which can be null) using standard serializer locating functionality. It can be called for all values including field and Map values, but usually field values are best handled calling defaultSerializeField(java.lang.String, java.lang.Object, org.codehaus.jackson.JsonGenerator) instead.

Throws:
IOException
JsonProcessingException

defaultSerializeField

public final void defaultSerializeField(String fieldName,
                                        Object value,
                                        JsonGenerator jgen)
                                 throws IOException,
                                        JsonProcessingException
Convenience method that will serialize given field with specified value. Value may be null. Serializer is done using the usual null) using standard serializer locating functionality.

Throws:
IOException
JsonProcessingException

defaultSerializeDateValue

public abstract void defaultSerializeDateValue(long timestamp,
                                               JsonGenerator jgen)
                                        throws IOException,
                                               JsonProcessingException
Method that will handle serialization of Date(-like) values, using SerializationConfig settings to determine expected serialization behavior.

Throws:
IOException
JsonProcessingException

defaultSerializeDateValue

public abstract void defaultSerializeDateValue(Date date,
                                               JsonGenerator jgen)
                                        throws IOException,
                                               JsonProcessingException
Method that will handle serialization of Date(-like) values, using SerializationConfig settings to determine expected serialization behavior.

Throws:
IOException
JsonProcessingException