org.codehaus.jackson.map.ser
Class BeanSerializer

java.lang.Object
  extended by org.codehaus.jackson.map.JsonSerializer<T>
      extended by org.codehaus.jackson.map.ser.SerializerBase<Object>
          extended by org.codehaus.jackson.map.ser.BeanSerializer
All Implemented Interfaces:
ResolvableSerializer, SchemaAware

public class BeanSerializer
extends SerializerBase<Object>
implements ResolvableSerializer, SchemaAware

Serializer class that can serialize arbitrary bean objects.

Implementation note: we will post-process resulting serializer, to figure out actual serializers for final types. This must be done from resolve(org.codehaus.jackson.map.SerializerProvider) method, and NOT from constructor; otherwise we could end up with an infinite loop.


Nested Class Summary
 
Nested classes/interfaces inherited from class org.codehaus.jackson.map.JsonSerializer
JsonSerializer.None
 
Field Summary
protected  AnyGetterWriter _anyGetterWriter
          Handler for JsonAnyGetter annotated properties
protected  Class<?> _class
          Value type of this serializer, used for error reporting and debugging.
protected  BeanPropertyWriter[] _filteredProps
          Optional filters used to suppress output of properties that are only to be included in certain views
protected  BeanPropertyWriter[] _props
          Writers used for outputting actual property values
protected static BeanPropertyWriter[] NO_PROPS
           
 
Fields inherited from class org.codehaus.jackson.map.ser.SerializerBase
_handledType
 
Constructor Summary
BeanSerializer(BeanSerializer src, BeanPropertyWriter[] filtered)
          "Copy-constructor" used when creating slightly differing instance(s) of an exisitng serializer
BeanSerializer(Class<?> type, BeanPropertyWriter[] writers)
           
BeanSerializer(Class<?> type, BeanPropertyWriter[] props, BeanPropertyWriter[] filteredProps)
          Alternate constructor used when class being serialized can have dynamically enabled JSON Views
BeanSerializer(Class<?> type, Collection<BeanPropertyWriter> props)
           
 
Method Summary
static BeanSerializer createDummy(Class<?> forType)
          Method for constructing dummy bean deserializer; one that never outputs any properties
 JsonNode getSchema(SerializerProvider provider, Type typeHint)
          Get the representation of the schema to which this serializer will conform.
 void resolve(SerializerProvider provider)
          Method called after SerializerProvider has registered the serializer, but before it has returned it to the caller.
 void serialize(Object bean, JsonGenerator jgen, SerializerProvider provider)
          Main serialization method that will delegate actual output to configured BeanPropertyWriter instances.
protected  void serializeFields(Object bean, JsonGenerator jgen, SerializerProvider provider)
           
 void serializeWithType(Object bean, JsonGenerator jgen, SerializerProvider provider, TypeSerializer typeSer)
          Method that can be called to ask implementation to serialize values of type this serializer handles, using specified type serializer for embedding necessary type information.
 void setAnyGetter(AnyGetterWriter agw)
           
 String toString()
           
 BeanSerializer withFiltered(BeanPropertyWriter[] filtered)
          Method used for constructing a filtered serializer instance, using this serializer as the base.
 
Methods inherited from class org.codehaus.jackson.map.ser.SerializerBase
createObjectNode, createSchemaNode, createSchemaNode, handledType, wrapAndThrow, wrapAndThrow
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

NO_PROPS

protected static final BeanPropertyWriter[] NO_PROPS

_class

protected final Class<?> _class
Value type of this serializer, used for error reporting and debugging.


_props

protected final BeanPropertyWriter[] _props
Writers used for outputting actual property values


_filteredProps

protected final BeanPropertyWriter[] _filteredProps
Optional filters used to suppress output of properties that are only to be included in certain views


_anyGetterWriter

protected AnyGetterWriter _anyGetterWriter
Handler for JsonAnyGetter annotated properties

Since:
1.6
Constructor Detail

BeanSerializer

public BeanSerializer(Class<?> type,
                      BeanPropertyWriter[] writers)
Parameters:
type - Nominal type of values handled by this serializer
writers - Property writers used for actual serialization

BeanSerializer

public BeanSerializer(Class<?> type,
                      BeanPropertyWriter[] props,
                      BeanPropertyWriter[] filteredProps)
Alternate constructor used when class being serialized can have dynamically enabled JSON Views

Parameters:
filteredProps - Filtered property writers to use when there is an active view.

BeanSerializer

public BeanSerializer(Class<?> type,
                      Collection<BeanPropertyWriter> props)

BeanSerializer

public BeanSerializer(BeanSerializer src,
                      BeanPropertyWriter[] filtered)
"Copy-constructor" used when creating slightly differing instance(s) of an exisitng serializer

Method Detail

createDummy

public static BeanSerializer createDummy(Class<?> forType)
Method for constructing dummy bean deserializer; one that never outputs any properties


withFiltered

public BeanSerializer withFiltered(BeanPropertyWriter[] filtered)
Method used for constructing a filtered serializer instance, using this serializer as the base.

Since:
1.4

setAnyGetter

public void setAnyGetter(AnyGetterWriter agw)

serialize

public final void serialize(Object bean,
                            JsonGenerator jgen,
                            SerializerProvider provider)
                     throws IOException,
                            JsonGenerationException
Main serialization method that will delegate actual output to configured BeanPropertyWriter instances.

Specified by:
serialize in class SerializerBase<Object>
Parameters:
bean - Value to serialize; can not be null.
jgen - Generator used to output resulting Json content
provider - Provider that can be used to get serializers for serializing Objects value contains, if any.
Throws:
IOException
JsonGenerationException

serializeWithType

public final void serializeWithType(Object bean,
                                    JsonGenerator jgen,
                                    SerializerProvider provider,
                                    TypeSerializer typeSer)
                             throws IOException,
                                    JsonGenerationException
Description copied from class: JsonSerializer
Method that can be called to ask implementation to serialize values of type this serializer handles, using specified type serializer for embedding necessary type information.

Default implementation will ignore serialization of type information, and just calls JsonSerializer.serialize(T, org.codehaus.jackson.JsonGenerator, org.codehaus.jackson.map.SerializerProvider): serializers that can embed type information should override this to implement actual handling. Most common such handling is done by something like:

  // note: method to call depends on whether this type is serialized as JSON scalar, object or Array!
  typeSer.writeTypePrefixForScalar(value, jgen);
  serialize(value, jgen, provider);
  typeSer.writeTypeSuffixForScalar(value, jgen);

Overrides:
serializeWithType in class JsonSerializer<Object>
Parameters:
bean - Value to serialize; can not be null.
jgen - Generator used to output resulting Json content
provider - Provider that can be used to get serializers for serializing Objects value contains, if any.
typeSer - Type serializer to use for including type information
Throws:
IOException
JsonGenerationException

serializeFields

protected void serializeFields(Object bean,
                               JsonGenerator jgen,
                               SerializerProvider provider)
                        throws IOException,
                               JsonGenerationException
Throws:
IOException
JsonGenerationException

getSchema

public JsonNode getSchema(SerializerProvider provider,
                          Type typeHint)
                   throws JsonMappingException
Description copied from interface: SchemaAware
Get the representation of the schema to which this serializer will conform.

Specified by:
getSchema in interface SchemaAware
Specified by:
getSchema in class SerializerBase<Object>
Parameters:
provider - The serializer provider.
typeHint - A hint about the type.
Returns:
Json-schema for this serializer.
Throws:
JsonMappingException

resolve

public void resolve(SerializerProvider provider)
             throws JsonMappingException
Description copied from interface: ResolvableSerializer
Method called after SerializerProvider has registered the serializer, but before it has returned it to the caller. Called object can then resolve its dependencies to other types, including self-references (direct or indirect).

Specified by:
resolve in interface ResolvableSerializer
Parameters:
provider - Provider that has constructed serializer this method is called on.
Throws:
JsonMappingException

toString

public String toString()
Overrides:
toString in class Object