com.fasterxml.jackson.databind.jsontype

## Class TypeSerializer

- java.lang.Object
- - com.fasterxml.jackson.databind.jsontype.TypeSerializer

Direct Known Subclasses:
  TypeSerializerBase

    public abstract class TypeSerializer
    extends Object

Interface for serializing type information regarding instances of specified base type (super class), so that exact subtype can be properly deserialized later on. These instances are to be called by regular `JsonSerializer`s using proper contextual calls, to add type information using mechanism type serializer was configured with.

NOTE: version 2.9 contains significant attempt at simplifying interface, as well as giving format implementation (via `JsonGenerator`) more control over actual serialization details.

### Constructor Summary

Constructors
Constructor and Description
`TypeSerializer()`

### Method Summary

Methods
`protected void` `_writeLegacySuffix(JsonGenerator g, WritableTypeId typeId)` Helper method needed for backwards compatibility: since original type id can not be routed through completely, we have to reverse-engineer likely setting before calling suffix.
`abstract TypeSerializer` `forProperty(BeanProperty prop)` Method called to create contextual version, to be used for values of given property.
`abstract String` `getPropertyName()` Name of property that contains type information, if property-based inclusion is used.
`abstract TypeIdResolver` `getTypeIdResolver()` Accessor for object that handles conversions between types and matching type ids.
`abstract JsonTypeInfo.As` `getTypeInclusion()` Accessor for type information inclusion method that serializer uses; indicates how type information is embedded in resulting JSON.
`WritableTypeId` `typeId(Object value, Class<?> typeForId, JsonToken valueShape)`
`WritableTypeId` `typeId(Object value, JsonToken valueShape)` Factory method for constructing type id value object to pass to `writeTypePrefix(com.fasterxml.jackson.core.JsonGenerator, com.fasterxml.jackson.core.type.WritableTypeId)`.
`WritableTypeId` `typeId(Object value, JsonToken valueShape, Object id)`
`void` `writeCustomTypePrefixForArray(Object value, JsonGenerator g, String typeId)` Deprecated.
`void` `writeCustomTypePrefixForObject(Object value, JsonGenerator g, String typeId)` Deprecated.
`void` `writeCustomTypePrefixForScalar(Object value, JsonGenerator g, String typeId)` Deprecated.
`void` `writeCustomTypeSuffixForArray(Object value, JsonGenerator g, String typeId)` Deprecated.
`void` `writeCustomTypeSuffixForObject(Object value, JsonGenerator g, String typeId)` Deprecated.
`void` `writeCustomTypeSuffixForScalar(Object value, JsonGenerator g, String typeId)` Deprecated.
`abstract WritableTypeId` `writeTypePrefix(JsonGenerator g, WritableTypeId typeId)` Method called to write initial part of type information for given value, along with possible wrapping to use: details are specified by `typeId` argument.
`void` `writeTypePrefixForArray(Object value, JsonGenerator g)` Deprecated.
`void` `writeTypePrefixForArray(Object value, JsonGenerator g, Class<?> type)` Deprecated.
`void` `writeTypePrefixForObject(Object value, JsonGenerator g)` Deprecated.
`void` `writeTypePrefixForObject(Object value, JsonGenerator g, Class<?> type)` Deprecated.
`void` `writeTypePrefixForScalar(Object value, JsonGenerator g)` Deprecated.
`void` `writeTypePrefixForScalar(Object value, JsonGenerator g, Class<?> type)` Deprecated.
`abstract WritableTypeId` `writeTypeSuffix(JsonGenerator g, WritableTypeId typeId)`
`void` `writeTypeSuffixForArray(Object value, JsonGenerator g)` Deprecated.
`void` `writeTypeSuffixForObject(Object value, JsonGenerator g)` Deprecated.
`void` `writeTypeSuffixForScalar(Object value, JsonGenerator g)` Deprecated.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Constructor Detail

#### TypeSerializer

    public TypeSerializer()

### Method Detail

#### forProperty

    public abstract TypeSerializer forProperty(BeanProperty prop)

Method called to create contextual version, to be used for values of given property. This may be the type itself (as is the case for bean properties), or values contained (for `Collection` or `Map` valued properties).

Since:
  2.0

#### getTypeInclusion

    public abstract JsonTypeInfo.As getTypeInclusion()

Accessor for type information inclusion method that serializer uses; indicates how type information is embedded in resulting JSON.

#### getPropertyName

    public abstract String getPropertyName()

Name of property that contains type information, if property-based inclusion is used.

#### getTypeIdResolver

    public abstract TypeIdResolver getTypeIdResolver()

Accessor for object that handles conversions between types and matching type ids.

#### typeId

    public WritableTypeId typeId(Object value,
                        JsonToken valueShape)

Factory method for constructing type id value object to pass to `writeTypePrefix(com.fasterxml.jackson.core.JsonGenerator, com.fasterxml.jackson.core.type.WritableTypeId)`.

#### typeId

    public WritableTypeId typeId(Object value,
                        JsonToken valueShape,
                        Object id)

#### typeId

    public WritableTypeId typeId(Object value,
                        Class<?> typeForId,
                        JsonToken valueShape)

#### writeTypePrefix

    public abstract WritableTypeId writeTypePrefix(JsonGenerator g,
                                 WritableTypeId typeId)
                                            throws IOException

Method called to write initial part of type information for given value, along with possible wrapping to use: details are specified by `typeId` argument.

Parameters:
  `g` - Generator to use for outputting type id and possible wrapping
  `typeId` - Details of what type id is to be written, how.
Throws:
  `IOException`
Since:
  2.9

#### writeTypeSuffix

    public abstract WritableTypeId writeTypeSuffix(JsonGenerator g,
                                 WritableTypeId typeId)
                                            throws IOException

Throws:
  `IOException`
Since:
  2.9

#### writeTypePrefixForScalar

    @Deprecated
    public void writeTypePrefixForScalar(Object value,
                                           JsonGenerator g)
                                  throws IOException

Deprecated.
Method called to write initial part of type information for given value, when it will be output as scalar JSON value (not as JSON Object or Array). This means that the context after call cannot be that of JSON Object; it may be Array or root context.

Parameters:
  `value` - Value that will be serialized, for which type information is to be written
  `g` - Generator to use for writing type information
Throws:
  `IOException`

#### writeTypePrefixForObject

    @Deprecated
    public void writeTypePrefixForObject(Object value,
                                           JsonGenerator g)
                                  throws IOException

Deprecated.
Method called to write initial part of type information for given value, when it will be output as JSON Object value (not as JSON Array or scalar). This means that context after call must be JSON Object, meaning that caller can then proceed to output field entries.

Parameters:
  `value` - Value that will be serialized, for which type information is to be written
  `g` - Generator to use for writing type information
Throws:
  `IOException`

#### writeTypePrefixForArray

    @Deprecated
    public void writeTypePrefixForArray(Object value,
                                          JsonGenerator g)
                                 throws IOException

Deprecated.
Method called to write initial part of type information for given value, when it will be output as JSON Array value (not as JSON Object or scalar). This means that context after call must be JSON Array, that is, there must be an open START_ARRAY to write contents in.

Parameters:
  `value` - Value that will be serialized, for which type information is to be written
  `g` - Generator to use for writing type information
Throws:
  `IOException`

#### writeTypeSuffixForScalar

    @Deprecated
    public void writeTypeSuffixForScalar(Object value,
                                           JsonGenerator g)
                                  throws IOException

Deprecated.
Method called after value has been serialized, to close any scopes opened by earlier matching call to `writeTypePrefixForScalar(java.lang.Object, com.fasterxml.jackson.core.JsonGenerator)`. Actual action to take may depend on various factors, but has to match with action `writeTypePrefixForScalar(java.lang.Object, com.fasterxml.jackson.core.JsonGenerator)` did (close array or object; or do nothing).

Throws:
  `IOException`

#### writeTypeSuffixForObject

    @Deprecated
    public void writeTypeSuffixForObject(Object value,
                                           JsonGenerator g)
                                  throws IOException

Deprecated.
Method called after value has been serialized, to close any scopes opened by earlier matching call to `writeTypePrefixForObject(java.lang.Object, com.fasterxml.jackson.core.JsonGenerator)`. It needs to write closing END_OBJECT marker, and any other decoration that needs to be matched.

Throws:
  `IOException`

#### writeTypeSuffixForArray

    @Deprecated
    public void writeTypeSuffixForArray(Object value,
                                          JsonGenerator g)
                                 throws IOException

Deprecated.
Method called after value has been serialized, to close any scopes opened by earlier matching call to `writeTypeSuffixForScalar(java.lang.Object, com.fasterxml.jackson.core.JsonGenerator)`. It needs to write closing END_ARRAY marker, and any other decoration that needs to be matched.

Throws:
  `IOException`

#### writeTypePrefixForScalar

    @Deprecated
    public void writeTypePrefixForScalar(Object value,
                                           JsonGenerator g,
                                           Class<?> type)
                                  throws IOException

Deprecated.
Alternative version of the prefix-for-scalar method, which is given actual type to use (instead of using exact type of the value); typically a super type of actual value type

Throws:
  `IOException`

#### writeTypePrefixForObject

    @Deprecated
    public void writeTypePrefixForObject(Object value,
                                           JsonGenerator g,
                                           Class<?> type)
                                  throws IOException

Deprecated.
Alternative version of the prefix-for-object method, which is given actual type to use (instead of using exact type of the value); typically a super type of actual value type

Throws:
  `IOException`

#### writeTypePrefixForArray

    @Deprecated
    public void writeTypePrefixForArray(Object value,
                                          JsonGenerator g,
                                          Class<?> type)
                                 throws IOException

Deprecated.
Alternative version of the prefix-for-array method, which is given actual type to use (instead of using exact type of the value); typically a super type of actual value type

Throws:
  `IOException`

#### writeCustomTypePrefixForScalar

    @Deprecated
    public void writeCustomTypePrefixForScalar(Object value,
                                                 JsonGenerator g,
                                                 String typeId)
                                        throws IOException

Deprecated.

Throws:
  `IOException`

#### writeCustomTypePrefixForObject

    @Deprecated
    public void writeCustomTypePrefixForObject(Object value,
                                                 JsonGenerator g,
                                                 String typeId)
                                        throws IOException

Deprecated.

Throws:
  `IOException`

#### writeCustomTypePrefixForArray

    @Deprecated
    public void writeCustomTypePrefixForArray(Object value,
                                                JsonGenerator g,
                                                String typeId)
                                       throws IOException

Deprecated.

Throws:
  `IOException`

#### writeCustomTypeSuffixForScalar

    @Deprecated
    public void writeCustomTypeSuffixForScalar(Object value,
                                                 JsonGenerator g,
                                                 String typeId)
                                        throws IOException

Deprecated.

Throws:
  `IOException`

#### writeCustomTypeSuffixForObject

    @Deprecated
    public void writeCustomTypeSuffixForObject(Object value,
                                                 JsonGenerator g,
                                                 String typeId)
                                        throws IOException

Deprecated.

Throws:
  `IOException`

#### writeCustomTypeSuffixForArray

    @Deprecated
    public void writeCustomTypeSuffixForArray(Object value,
                                                JsonGenerator g,
                                                String typeId)
                                       throws IOException

Deprecated.

Throws:
  `IOException`

#### _writeLegacySuffix

    protected final void _writeLegacySuffix(JsonGenerator g,
                          WritableTypeId typeId)
                                     throws IOException

Helper method needed for backwards compatibility: since original type id can not be routed through completely, we have to reverse-engineer likely setting before calling suffix.

Throws:
  `IOException`
Since:
  2.9