public abstract class JsonSerializer<T> extends Object implements JsonFormatVisitable
ObjectMapper
(and
other chained JsonSerializer
s too) to serialize Objects of
arbitrary types into JSON, using provided JsonGenerator
.
StdSerializer
instead
of this class, since it will implement many of optional
methods of this class.
NOTE: various serialize
methods are never (to be) called
with null values -- caller must handle null values, usually
by calling SerializerProvider.findNullValueSerializer(com.fasterxml.jackson.databind.BeanProperty)
to obtain
serializer to use.
This also means that custom serializers cannot be directly used to change
the output to produce when serializing null values.
If serializer is an aggregate one -- meaning it delegates handling of some
of its contents by using other serializer(s) -- it typically also needs
to implement ResolvableSerializer
,
which can locate secondary serializers needed. This is important to allow dynamic
overrides of serializers; separate call interface is needed to separate
resolution of secondary serializers (which may have cyclic link back
to serializer itself, directly or indirectly).
In addition, to support per-property annotations (to configure aspects
of serialization on per-property basis), serializers may want
to implement
ContextualSerializer
,
which allows specialization of serializers: call to
ContextualSerializer.createContextual(com.fasterxml.jackson.databind.SerializerProvider, com.fasterxml.jackson.databind.BeanProperty)
is passed information on property, and can create a newly configured
serializer for handling that particular property.
If both
ResolvableSerializer
and
ContextualSerializer
are implemented, resolution of serializers occurs before
contextualization.
Modifier and Type | Class and Description |
---|---|
static class |
JsonSerializer.None
This marker class is only to be used with annotations, to
indicate that no serializer is configured.
|
Constructor and Description |
---|
JsonSerializer() |
Modifier and Type | Method and Description |
---|---|
void |
acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor,
JavaType type)
Default implementation simply calls
JsonFormatVisitorWrapper.expectAnyFormat(JavaType) . |
JsonSerializer<?> |
getDelegatee()
Accessor that can be used to determine if this serializer uses
another serializer for actual serialization, by delegating
calls.
|
Class<T> |
handledType()
Method for accessing type of Objects this serializer can handle.
|
boolean |
isEmpty(SerializerProvider provider,
T value)
Method called to check whether given serializable value is
considered "empty" value (for purposes of suppressing serialization
of empty values).
|
boolean |
isEmpty(T value)
Deprecated.
Since 2.5 Use
isEmpty(SerializerProvider, Object) instead;
will be removed from 3.0 |
boolean |
isUnwrappingSerializer()
Accessor for checking whether this serializer is an
"unwrapping" serializer; this is necessary to know since
it may also require caller to suppress writing of the
leading property name.
|
Iterator<PropertyWriter> |
properties()
Accessor for iterating over logical properties that the type
handled by this serializer has, from serialization perspective.
|
JsonSerializer<T> |
replaceDelegatee(JsonSerializer<?> delegatee)
Method that can be called to try to replace serializer this serializer
delegates calls to.
|
abstract void |
serialize(T value,
com.fasterxml.jackson.core.JsonGenerator gen,
SerializerProvider serializers)
Method that can be called to ask implementation to serialize
values of type this serializer handles.
|
void |
serializeWithType(T value,
com.fasterxml.jackson.core.JsonGenerator gen,
SerializerProvider serializers,
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.
|
JsonSerializer<T> |
unwrappingSerializer(NameTransformer unwrapper)
Method that will return serializer instance that produces
"unwrapped" serialization, if applicable for type being
serialized (which is the case for some serializers
that produce JSON Objects as output).
|
boolean |
usesObjectId()
Method that can be called to see whether this serializer instance
will use Object Id to handle cyclic references.
|
JsonSerializer<?> |
withFilterId(Object filterId)
Mutant factory method that is called if contextual configuration indicates that
a specific filter (as specified by
filterId ) is to be used for
serialization. |
public JsonSerializer<T> unwrappingSerializer(NameTransformer unwrapper)
Default implementation just returns serializer as-is, indicating that no unwrapped variant exists
unwrapper
- Name transformation to use to convert between names
of unwrapper propertiespublic JsonSerializer<T> replaceDelegatee(JsonSerializer<?> delegatee)
UnsupportedOperationException
(if operation does not
make sense or is not allowed); or return this serializer as is.public JsonSerializer<?> withFilterId(Object filterId)
filterId
) is to be used for
serialization.
Default implementation simply returns this
; sub-classes that do support
filtering will need to create and return new instance if filter changes.
public abstract void serialize(T value, com.fasterxml.jackson.core.JsonGenerator gen, SerializerProvider serializers) throws IOException
value
- Value to serialize; can not be null.gen
- Generator used to output resulting Json contentserializers
- Provider that can be used to get serializers for
serializing Objects value contains, if any.IOException
public void serializeWithType(T value, com.fasterxml.jackson.core.JsonGenerator gen, SerializerProvider serializers, TypeSerializer typeSer) throws IOException
Default implementation will throw UnsupportedOperationException
to indicate that proper type handling needs to be implemented.
For simple datatypes written as a single scalar value (JSON String, Number, Boolean), implementation would look like:
// note: method to call depends on whether this type is serialized as JSON scalar, object or Array! typeSer.writeTypePrefixForScalar(value, gen); serialize(value, gen, provider); typeSer.writeTypeSuffixForScalar(value, gen);and implementations for type serialized as JSON Arrays or Objects would differ slightly, as
START-ARRAY
/END-ARRAY
and
START-OBJECT
/END-OBJECT
pairs
need to be properly handled with respect to serializing of contents.value
- Value to serialize; can not be null.gen
- Generator used to output resulting Json contentserializers
- Provider that can be used to get serializers for
serializing Objects value contains, if any.typeSer
- Type serializer to use for including type informationIOException
public Class<T> handledType()
Default implementation will return null, which essentially means
same as returning Object.class
would; that is, that
nothing is known about handled type.
@Deprecated public boolean isEmpty(T value)
isEmpty(SerializerProvider, Object)
instead;
will be removed from 3.0Default implementation will consider only null values to be empty.
public boolean isEmpty(SerializerProvider provider, T value)
Default implementation will consider only null values to be empty.
NOTE: replaces isEmpty(Object)
, which was deprecated in 2.5
public boolean usesObjectId()
public boolean isUnwrappingSerializer()
public JsonSerializer<?> getDelegatee()
public Iterator<PropertyWriter> properties()
BeanPropertyWriter
.
Of standard Jackson serializers, only BeanSerializer
exposes properties.public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType type) throws JsonMappingException
JsonFormatVisitorWrapper.expectAnyFormat(JavaType)
.acceptJsonFormatVisitor
in interface JsonFormatVisitable
type
- Type of element (entity like property) being visitedJsonMappingException
Copyright © 2008–2022 FasterXML. All rights reserved.