Class TypeSerializer
java.lang.Object
com.fasterxml.jackson.databind.jsontype.TypeSerializer
- Direct Known Subclasses:
TypeSerializerBase
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. Minor changes are required to change
call pattern so that return value of "prefix" write needs to be passed to "suffix"
write.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected final void
_writeLegacySuffix
(com.fasterxml.jackson.core.JsonGenerator g, com.fasterxml.jackson.core.type.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
Name of property that contains type information, if property-based inclusion is used.abstract TypeIdResolver
Accessor for object that handles conversions between types and matching type ids.abstract com.fasterxml.jackson.annotation.JsonTypeInfo.As
Accessor for type information inclusion method that serializer uses; indicates how type information is embedded in resulting JSON.com.fasterxml.jackson.core.type.WritableTypeId
Factory method for constructing type id value object to pass towriteTypePrefix(com.fasterxml.jackson.core.JsonGenerator, com.fasterxml.jackson.core.type.WritableTypeId)
.com.fasterxml.jackson.core.type.WritableTypeId
com.fasterxml.jackson.core.type.WritableTypeId
void
writeCustomTypePrefixForArray
(Object value, com.fasterxml.jackson.core.JsonGenerator g, String typeId) Deprecated.void
writeCustomTypePrefixForObject
(Object value, com.fasterxml.jackson.core.JsonGenerator g, String typeId) Deprecated.Since 2.9 usewriteTypePrefix(JsonGenerator, WritableTypeId)
insteadvoid
writeCustomTypePrefixForScalar
(Object value, com.fasterxml.jackson.core.JsonGenerator g, String typeId) Deprecated.Since 2.9 usewriteTypePrefix(JsonGenerator, WritableTypeId)
insteadvoid
writeCustomTypeSuffixForArray
(Object value, com.fasterxml.jackson.core.JsonGenerator g, String typeId) Deprecated.Since 2.9 usewriteTypeSuffix(JsonGenerator, WritableTypeId)
insteadvoid
writeCustomTypeSuffixForObject
(Object value, com.fasterxml.jackson.core.JsonGenerator g, String typeId) Deprecated.Since 2.9 usewriteTypeSuffix(JsonGenerator, WritableTypeId)
insteadvoid
writeCustomTypeSuffixForScalar
(Object value, com.fasterxml.jackson.core.JsonGenerator g, String typeId) Deprecated.Since 2.9 usewriteTypeSuffix(JsonGenerator, WritableTypeId)
insteadabstract com.fasterxml.jackson.core.type.WritableTypeId
writeTypePrefix
(com.fasterxml.jackson.core.JsonGenerator g, com.fasterxml.jackson.core.type.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, com.fasterxml.jackson.core.JsonGenerator g) Deprecated.Since 2.9 usewriteTypePrefix(JsonGenerator, WritableTypeId)
insteadvoid
writeTypePrefixForArray
(Object value, com.fasterxml.jackson.core.JsonGenerator g, Class<?> type) Deprecated.Since 2.9 usewriteTypePrefix(JsonGenerator, WritableTypeId)
insteadvoid
writeTypePrefixForObject
(Object value, com.fasterxml.jackson.core.JsonGenerator g) Deprecated.Since 2.9 usewriteTypePrefix(JsonGenerator, WritableTypeId)
insteadvoid
writeTypePrefixForObject
(Object value, com.fasterxml.jackson.core.JsonGenerator g, Class<?> type) Deprecated.Since 2.9 usewriteTypePrefix(JsonGenerator, WritableTypeId)
insteadvoid
writeTypePrefixForScalar
(Object value, com.fasterxml.jackson.core.JsonGenerator g) Deprecated.Since 2.9 usewriteTypePrefix(JsonGenerator, WritableTypeId)
insteadvoid
writeTypePrefixForScalar
(Object value, com.fasterxml.jackson.core.JsonGenerator g, Class<?> type) Deprecated.Since 2.9 usewriteTypePrefix(JsonGenerator, WritableTypeId)
insteadabstract com.fasterxml.jackson.core.type.WritableTypeId
writeTypeSuffix
(com.fasterxml.jackson.core.JsonGenerator g, com.fasterxml.jackson.core.type.WritableTypeId typeId) Method that should be called afterwriteTypePrefix(JsonGenerator, WritableTypeId)
and matching value write have been called, passingWritableTypeId
returned.void
writeTypeSuffixForArray
(Object value, com.fasterxml.jackson.core.JsonGenerator g) Deprecated.Since 2.9 usewriteTypeSuffix(JsonGenerator, WritableTypeId)
insteadvoid
writeTypeSuffixForObject
(Object value, com.fasterxml.jackson.core.JsonGenerator g) Deprecated.Since 2.9 usewriteTypeSuffix(JsonGenerator, WritableTypeId)
insteadvoid
writeTypeSuffixForScalar
(Object value, com.fasterxml.jackson.core.JsonGenerator g) Deprecated.Since 2.9 usewriteTypeSuffix(JsonGenerator, WritableTypeId)
instead
-
Constructor Details
-
TypeSerializer
public TypeSerializer()
-
-
Method Details
-
forProperty
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 (forCollection
orMap
valued properties).- Since:
- 2.0
-
getTypeInclusion
public abstract com.fasterxml.jackson.annotation.JsonTypeInfo.As getTypeInclusion()Accessor for type information inclusion method that serializer uses; indicates how type information is embedded in resulting JSON. -
getPropertyName
Name of property that contains type information, if property-based inclusion is used. -
getTypeIdResolver
Accessor for object that handles conversions between types and matching type ids. -
typeId
public com.fasterxml.jackson.core.type.WritableTypeId typeId(Object value, com.fasterxml.jackson.core.JsonToken valueShape) Factory method for constructing type id value object to pass towriteTypePrefix(com.fasterxml.jackson.core.JsonGenerator, com.fasterxml.jackson.core.type.WritableTypeId)
. -
typeId
-
typeId
-
writeTypePrefix
public abstract com.fasterxml.jackson.core.type.WritableTypeId writeTypePrefix(com.fasterxml.jackson.core.JsonGenerator g, com.fasterxml.jackson.core.type.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. Note that for structured types (Object, Array), this call will add necessary start token so it should NOT be explicitly written, unlike with non-type-id value writes.See
writeTypeSuffix(JsonGenerator, WritableTypeId)
for a complete example of typical usage.- Parameters:
g
- Generator to use for outputting type id and possible wrappingtypeId
- Details of what type id is to be written, how.- Throws:
IOException
- Since:
- 2.9
-
writeTypeSuffix
public abstract com.fasterxml.jackson.core.type.WritableTypeId writeTypeSuffix(com.fasterxml.jackson.core.JsonGenerator g, com.fasterxml.jackson.core.type.WritableTypeId typeId) throws IOException Method that should be called afterwriteTypePrefix(JsonGenerator, WritableTypeId)
and matching value write have been called, passingWritableTypeId
returned. Usual idiom is:// Indicator generator that type identifier may be needed; generator may write // one as suggested, modify information, or take some other action // NOTE! For Object/Array types, this will ALSO write start marker! WritableTypeId typeIdDef = typeSer.writeTypePrefix(gen, typeSer.typeId(value, JsonToken.START_OBJECT)); // serializing actual value for which TypeId may have been written... like // NOTE: do NOT write START_OBJECT before OR END_OBJECT after: g.writeStringField("message", "Hello, world!" // matching type suffix call to let generator chance to add suffix, if any // NOTE! For Object/Array types, this will ALSO write end marker! typeSer.writeTypeSuffix(gen, typeIdDef);
- Throws:
IOException
- Since:
- 2.9
-
writeTypePrefixForScalar
@Deprecated public void writeTypePrefixForScalar(Object value, com.fasterxml.jackson.core.JsonGenerator g) throws IOException Deprecated.Since 2.9 usewriteTypePrefix(JsonGenerator, WritableTypeId)
insteadDEPRECATED: now equivalent to:writeTypePrefix(g, typeId(value, JsonToken.VALUE_STRING));
. SeewriteTypePrefix(com.fasterxml.jackson.core.JsonGenerator, com.fasterxml.jackson.core.type.WritableTypeId)
for more info.- Throws:
IOException
-
writeTypePrefixForObject
@Deprecated public void writeTypePrefixForObject(Object value, com.fasterxml.jackson.core.JsonGenerator g) throws IOException Deprecated.Since 2.9 usewriteTypePrefix(JsonGenerator, WritableTypeId)
insteadDEPRECATED: now equivalent to:writeTypePrefix(g, typeId(value, JsonToken.START_OBJECT));
. SeewriteTypePrefix(com.fasterxml.jackson.core.JsonGenerator, com.fasterxml.jackson.core.type.WritableTypeId)
for more info.- Throws:
IOException
-
writeTypePrefixForArray
@Deprecated public void writeTypePrefixForArray(Object value, com.fasterxml.jackson.core.JsonGenerator g) throws IOException Deprecated.Since 2.9 usewriteTypePrefix(JsonGenerator, WritableTypeId)
insteadDEPRECATED: now equivalent to:writeTypePrefix(g, typeId(value, JsonToken.START_ARRAY));
. SeewriteTypePrefix(com.fasterxml.jackson.core.JsonGenerator, com.fasterxml.jackson.core.type.WritableTypeId)
for more info.- Throws:
IOException
-
writeTypeSuffixForScalar
@Deprecated public void writeTypeSuffixForScalar(Object value, com.fasterxml.jackson.core.JsonGenerator g) throws IOException Deprecated.Since 2.9 usewriteTypeSuffix(JsonGenerator, WritableTypeId)
insteadDEPRECATED: now equivalent to:writeTypeSuffix(g, typeId(value, JsonToken.VALUE_STRING));
. SeewriteTypeSuffix(com.fasterxml.jackson.core.JsonGenerator, com.fasterxml.jackson.core.type.WritableTypeId)
for more info.- Throws:
IOException
-
writeTypeSuffixForObject
@Deprecated public void writeTypeSuffixForObject(Object value, com.fasterxml.jackson.core.JsonGenerator g) throws IOException Deprecated.Since 2.9 usewriteTypeSuffix(JsonGenerator, WritableTypeId)
insteadDEPRECATED: now equivalent to:writeTypeSuffix(g, typeId(value, JsonToken.START_OBJECT));
. SeewriteTypeSuffix(com.fasterxml.jackson.core.JsonGenerator, com.fasterxml.jackson.core.type.WritableTypeId)
for more info.- Throws:
IOException
-
writeTypeSuffixForArray
@Deprecated public void writeTypeSuffixForArray(Object value, com.fasterxml.jackson.core.JsonGenerator g) throws IOException Deprecated.Since 2.9 usewriteTypeSuffix(JsonGenerator, WritableTypeId)
insteadDEPRECATED: now equivalent to:writeTypeSuffix(g, typeId(value, JsonToken.START_ARRAY));
. SeewriteTypeSuffix(com.fasterxml.jackson.core.JsonGenerator, com.fasterxml.jackson.core.type.WritableTypeId)
for more info.- Throws:
IOException
-
writeTypePrefixForScalar
@Deprecated public void writeTypePrefixForScalar(Object value, com.fasterxml.jackson.core.JsonGenerator g, Class<?> type) throws IOException Deprecated.Since 2.9 usewriteTypePrefix(JsonGenerator, WritableTypeId)
insteadDEPRECATED: now equivalent to:writeTypePrefix(g, typeId(value, type, JsonToken.VALUE_STRING));
. SeewriteTypePrefix(com.fasterxml.jackson.core.JsonGenerator, com.fasterxml.jackson.core.type.WritableTypeId)
for more info.- Throws:
IOException
-
writeTypePrefixForObject
@Deprecated public void writeTypePrefixForObject(Object value, com.fasterxml.jackson.core.JsonGenerator g, Class<?> type) throws IOException Deprecated.Since 2.9 usewriteTypePrefix(JsonGenerator, WritableTypeId)
insteadDEPRECATED: now equivalent to:writeTypePrefix(g, typeId(value, type, JsonToken.START_OBJECT));
. SeewriteTypePrefix(com.fasterxml.jackson.core.JsonGenerator, com.fasterxml.jackson.core.type.WritableTypeId)
for more info.- Throws:
IOException
-
writeTypePrefixForArray
@Deprecated public void writeTypePrefixForArray(Object value, com.fasterxml.jackson.core.JsonGenerator g, Class<?> type) throws IOException Deprecated.Since 2.9 usewriteTypePrefix(JsonGenerator, WritableTypeId)
insteadDEPRECATED: now equivalent to:writeTypePrefix(g, typeId(value, type, JsonToken.START_ARRAY));
. SeewriteTypePrefix(com.fasterxml.jackson.core.JsonGenerator, com.fasterxml.jackson.core.type.WritableTypeId)
for more info.- Throws:
IOException
-
writeCustomTypePrefixForScalar
@Deprecated public void writeCustomTypePrefixForScalar(Object value, com.fasterxml.jackson.core.JsonGenerator g, String typeId) throws IOException Deprecated.Since 2.9 usewriteTypePrefix(JsonGenerator, WritableTypeId)
insteadDEPRECATED: now equivalent to:writeTypePrefix(g, typeId(value, JsonToken.VALUE_STRING, typeId));
. SeewriteTypePrefix(com.fasterxml.jackson.core.JsonGenerator, com.fasterxml.jackson.core.type.WritableTypeId)
for more info.- Throws:
IOException
-
writeCustomTypePrefixForObject
@Deprecated public void writeCustomTypePrefixForObject(Object value, com.fasterxml.jackson.core.JsonGenerator g, String typeId) throws IOException Deprecated.Since 2.9 usewriteTypePrefix(JsonGenerator, WritableTypeId)
insteadDEPRECATED: now equivalent to:writeTypePrefix(g, typeId(value, JsonToken.START_OBJECT, typeId));
. SeewriteTypePrefix(com.fasterxml.jackson.core.JsonGenerator, com.fasterxml.jackson.core.type.WritableTypeId)
for more info.- Throws:
IOException
-
writeCustomTypePrefixForArray
@Deprecated public void writeCustomTypePrefixForArray(Object value, com.fasterxml.jackson.core.JsonGenerator g, String typeId) throws IOException Deprecated.Since 2.9 usewriteTypePrefix(JsonGenerator, WritableTypeId)
insteadDEPRECATED: now equivalent to:writeTypePrefix(g, typeId(value, JsonToken.START_ARRAY, typeId));
. SeewriteTypePrefix(com.fasterxml.jackson.core.JsonGenerator, com.fasterxml.jackson.core.type.WritableTypeId)
for more info.- Throws:
IOException
-
writeCustomTypeSuffixForScalar
@Deprecated public void writeCustomTypeSuffixForScalar(Object value, com.fasterxml.jackson.core.JsonGenerator g, String typeId) throws IOException Deprecated.Since 2.9 usewriteTypeSuffix(JsonGenerator, WritableTypeId)
instead- Throws:
IOException
-
writeCustomTypeSuffixForObject
@Deprecated public void writeCustomTypeSuffixForObject(Object value, com.fasterxml.jackson.core.JsonGenerator g, String typeId) throws IOException Deprecated.Since 2.9 usewriteTypeSuffix(JsonGenerator, WritableTypeId)
instead- Throws:
IOException
-
writeCustomTypeSuffixForArray
@Deprecated public void writeCustomTypeSuffixForArray(Object value, com.fasterxml.jackson.core.JsonGenerator g, String typeId) throws IOException Deprecated.Since 2.9 usewriteTypeSuffix(JsonGenerator, WritableTypeId)
instead- Throws:
IOException
-
_writeLegacySuffix
protected final void _writeLegacySuffix(com.fasterxml.jackson.core.JsonGenerator g, com.fasterxml.jackson.core.type.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
-
writeTypePrefix(JsonGenerator, WritableTypeId)
instead