Top Description Inners Constructors Methods
tools.jackson.databind

public abstract Class ValueSerializer<T>

extends Object
implements JsonFormatVisitable
Class Inheritance
All Implemented Interfaces
tools.jackson.databind.jsonFormatVisitors.JsonFormatVisitable
Known Direct Subclasses
tools.jackson.databind.ValueSerializer.None, tools.jackson.databind.ext.javatime.ser.key.ZonedDateTimeKeySerializer, tools.jackson.databind.ser.impl.TypeWrappedSerializer, tools.jackson.databind.ser.std.StdSerializer
Imports
java.util.Iterator, .Set, com.fasterxml.jackson.annotation.JsonFormat, tools.jackson.core.*, tools.jackson.databind.jsonFormatVisitors.JsonFormatVisitable, .JsonFormatVisitorWrapper, tools.jackson.databind.jsontype.TypeSerializer, tools.jackson.databind.ser.PropertyWriter, tools.jackson.databind.util.ClassUtil, .NameTransformer

Abstract class that defines API used by ObjectMapper (and other chained ValueSerializers too) to serialize Objects of arbitrary types into JSON, using provided JsonGenerator. Note that although API is defined here, custom serializer implementations should almost always be based on tools.jackson.databind.ser.std.StdSerializer since it will implement many of optional methods of this class.

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 resolve 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).

Initialization of serializers is handled by two main methods:

  1. resolve: called after instance is configured to be used for specific type, but without yet knowing property it will be used for (or, in case of root values, without property). Method needs to be implemented for serializers that may work on cyclic types, and specifically is implemented by standard POJO serializer (BeanSerializer). It is usually not needed for container types as their type definitions are not cyclic, unlike some POJO types.
  2. createContextual: called on resolved instance (whether newly created, or found via cache), when serializer is to be used for specific property, or as root value serializer (no referring property). It is used to apply annotations from property accessors (getter, field), and may also be used for resolving nested types for container serializers (such as ones for java.util.Collections).
Caching of serializers occurs after resolve is called: cached instances are not contextual.

Note

various serialize methods are never (to be) called with null values -- caller must handle null values, usually by calling SerializationContext#findNullValueSerializer 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.

Note

In Jackson 2.x was named JsonSerializer

Nested and Inner Type Summary

Modifier and TypeClass and Description
public abstract static class
ValueSerializer.None

This marker class is only to be used with annotations, to indicate that no serializer is configured.

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
public ValueSerializer<?>

Returns:

Serializer to use for serializing values of specified property; may be this instance or a new instance.
createContextual
(SerializationContext
Context to use for accessing config, other serializers
ctxt
,
BeanProperty
Property (defined by one or more accessors - field or method - used for accessing logical property value) for which serializer is used to be used; or, `null` for root value (or in cases where caller does not have this information, which is handled as root value case).
property
)

Method called to see if a different (or differently configured) serializer is needed to serialize values of specified property (or, for root values, in which case `null` is passed).

public ValueSerializer<?>

Returns:

Serializer this serializer delegates calls to, if null; null otherwise.
getDelegatee
()

Accessor that can be used to determine if this serializer uses another serializer for actual serialization, by delegating calls.

public Class<?>
handledType()

Method for accessing type of Objects this serializer can handle.

public boolean
isEmpty(SerializationContext ctxt, T value)

Method called to check whether given serializable value is considered "empty" value (for purposes of suppressing serialization of empty values).

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

public Iterator<PropertyWriter>
properties()

Accessor for iterating over logical properties that the type handled by this serializer has, from serialization perspective.

public ValueSerializer<T>
replaceDelegatee(ValueSerializer<?> delegatee)

Method that can be called to try to replace serializer this serializer delegates calls to.

public void
resolve(SerializationContext
Currently active serialization context.
ctxt
)

Method called after SerializationContext has registered the serializer, but before it has returned it to the caller.

public abstract void
serialize(T
Value to serialize; can not be null.
value
,
JsonGenerator
Generator used to output resulting Json content
gen
,
SerializationContext
Context that can be used to get serializers for serializing Objects value contains, if any.
ctxt
)

Method that can be called to ask implementation to serialize values of type this serializer handles.

public void
serializeWithType(T
Value to serialize; can not be null.
value
,
JsonGenerator
Generator used to output resulting Json content
gen
,
SerializationContext
Context that can be used to get serializers for serializing Objects value contains, if any.
ctxt
,
TypeSerializer
Type serializer to use for including type information
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.

public ValueSerializer<T>
unwrappingSerializer(NameTransformer
Name transformation to use to convert between names of unwrapper properties
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).

public boolean
usesObjectId()

Method that can be called to see whether this serializer instance will use Object Id to handle cyclic references.

public ValueSerializer<?>
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 ValueSerializer<?>
withFormatOverrides(SerializationConfig config, JsonFormat.Value
(not null) Override settings, NOT including original format settings (which serializer needs to explicitly retain if needed)
formatOverrides
)

Mutant factory called if there is need to create a serializer with specified format overrides (typically from property on which this serializer would be used, based on type declaration).

public ValueSerializer<?>

Returns:

Serializer instance that without specified set of properties to ignore (if any)
withIgnoredProperties
(Set<String>
Set of property names to ignore for serialization;
ignoredProperties
)

Mutant factory method called to create a new instance after excluding specified set of properties by name, if there is any.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

ValueSerializerback to summary
public ValueSerializer()

Method Detail

acceptJsonFormatVisitorback to summary
public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType type)

Implements tools.jackson.databind.jsonFormatVisitors.JsonFormatVisitable.acceptJsonFormatVisitor.

Default implementation simply calls JsonFormatVisitorWrapper#expectAnyFormat(JavaType).

Annotations
@Override
createContextualback to summary
public ValueSerializer<?> createContextual(SerializationContext ctxt, BeanProperty property)

Method called to see if a different (or differently configured) serializer is needed to serialize values of specified property (or, for root values, in which case `null` is passed). Note that instance that this method is called on is typically shared one and as a result method should NOT modify this instance but rather construct and return a new instance. This instance should only be returned as-is, in case it is already suitable for use.

Note that method is only called once per POJO property, and for the first usage as root value serializer; it is not called for every serialization, as doing that would have significant performance impact; most serializers cache contextual instances for future use.

Parameters
ctxt:SerializationContext

Context to use for accessing config, other serializers

property:BeanProperty

Property (defined by one or more accessors - field or method - used for accessing logical property value) for which serializer is used to be used; or, `null` for root value (or in cases where caller does not have this information, which is handled as root value case).

Returns:ValueSerializer<?>

Serializer to use for serializing values of specified property; may be this instance or a new instance.

getDelegateeback to summary
public ValueSerializer<?> getDelegatee()

Accessor that can be used to determine if this serializer uses another serializer for actual serialization, by delegating calls. If so, will return immediate delegate (which itself may delegate to further serializers); otherwise will return null.

Returns:ValueSerializer<?>

Serializer this serializer delegates calls to, if null; null otherwise.

handledTypeback to summary
public Class<?> handledType()

Method for accessing type of Objects this serializer can handle. Note that this information is not guaranteed to be exact -- it may be a more generic (super-type) -- but it should not be incorrect (return a non-related type).

Note

starting with 3.0, left abstract.

isEmptyback to summary
public boolean isEmpty(SerializationContext ctxt, T value)

Method called to check whether given serializable value is considered "empty" value (for purposes of suppressing serialization of empty values).

Default implementation will consider only null values to be empty.

isUnwrappingSerializerback to summary
public 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.

propertiesback to summary
public Iterator<PropertyWriter> properties()

Accessor for iterating over logical properties that the type handled by this serializer has, from serialization perspective. Actual type of properties, if any, will be tools.jackson.databind.ser.BeanPropertyWriter. Of standard Jackson serializers, only tools.jackson.databind.ser.BeanSerializer exposes properties.

replaceDelegateeback to summary
public ValueSerializer<T> replaceDelegatee(ValueSerializer<?> delegatee)

Method that can be called to try to replace serializer this serializer delegates calls to. If not supported (either this serializer does not delegate anything; or it does not want any changes), should either throw UnsupportedOperationException (if operation does not make sense or is not allowed); or return this serializer as is.

resolveback to summary
public void resolve(SerializationContext ctxt)

Method called after SerializationContext 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).

Note that this method does NOT return serializer, since resolution is not allowed to change actual serializer to use.

Parameters
ctxt:SerializationContext

Currently active serialization context.

serializeback to summary
public abstract void serialize(T value, JsonGenerator gen, SerializationContext ctxt) throws JacksonException

Method that can be called to ask implementation to serialize values of type this serializer handles.

Parameters
value:T

Value to serialize; can not be null.

gen:JsonGenerator

Generator used to output resulting Json content

ctxt:SerializationContext

Context that can be used to get serializers for serializing Objects value contains, if any.

serializeWithTypeback to summary
public void serializeWithType(T value, JsonGenerator gen, SerializationContext ctxt, TypeSerializer typeSer) throws JacksonException

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 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.
Parameters
value:T

Value to serialize; can not be null.

gen:JsonGenerator

Generator used to output resulting Json content

ctxt:SerializationContext

Context that can be used to get serializers for serializing Objects value contains, if any.

typeSer:TypeSerializer

Type serializer to use for including type information

unwrappingSerializerback to summary
public ValueSerializer<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). If no unwrapped serializer can be constructed, will simply return serializer as-is.

Default implementation just returns serializer as-is, indicating that no unwrapped variant exists

Parameters
unwrapper:NameTransformer

Name transformation to use to convert between names of unwrapper properties

usesObjectIdback to summary
public boolean usesObjectId()

Method that can be called to see whether this serializer instance will use Object Id to handle cyclic references.

withFilterIdback to summary
public ValueSerializer<?> 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.

Default implementation simply returns this; sub-classes that do support filtering will need to create and return new instance if filter changes.

withFormatOverridesback to summary
public ValueSerializer<?> withFormatOverrides(SerializationConfig config, JsonFormat.Value formatOverrides)

Mutant factory called if there is need to create a serializer with specified format overrides (typically from property on which this serializer would be used, based on type declaration). Method is called before createContextual but right after serializer is either constructed or fetched from cache.

Method can do one of three things:

  • Return this instance as is: this means that none of overrides has any effect
  • Return an alternate ValueSerializer, suitable for use with specified format
  • Return null to indicate that this serializer instance is not suitable for handling format variation, but does not know how to construct new serializer: caller will typically then call tools.jackson.databind.ser.SerializerFactory with overrides to construct new serializer
One example of second approach is the case where com.fasterxml.jackson.annotation.JsonFormat.Shape#STRING indicates String representation and code can just construct simple "string-like serializer", or variant of itself (similar to how createContextual is often implemented). And third case (returning null) is applicable for cases like format defines com.fasterxml.jackson.annotation.JsonFormat.Shape#POJO, requesting "introspect serializer for POJO regardless of type": tools.jackson.databind.ser.SerializerFactory is needed for full re-introspection, typically.
Parameters
formatOverrides:JsonFormat.Value

(not null) Override settings, NOT including original format settings (which serializer needs to explicitly retain if needed)

Since
3.0
withIgnoredPropertiesback to summary
public ValueSerializer<?> withIgnoredProperties(Set<String> ignoredProperties)

Mutant factory method called to create a new instance after excluding specified set of properties by name, if there is any.

Parameters
ignoredProperties:Set<String>

Set of property names to ignore for serialization;

Returns:ValueSerializer<?>

Serializer instance that without specified set of properties to ignore (if any)

tools.jackson.databind back to summary

public abstract Class ValueSerializer.None

extends ValueSerializer<Object>
Class Inheritance

This marker class is only to be used with annotations, to indicate that no serializer is configured.

Specifically, this class is to be used as the marker for annotation tools.jackson.databind.annotation.JsonSerialize.

Constructor Summary

AccessConstructor and Description
public
None()

Method Summary

Inherited from tools.jackson.databind.ValueSerializer:
acceptJsonFormatVisitorcreateContextualgetDelegateehandledTypeisEmptyisUnwrappingSerializerpropertiesreplaceDelegateeresolveserializeserializeWithTypeunwrappingSerializerusesObjectIdwithFilterIdwithFormatOverrideswithIgnoredProperties

Constructor Detail

Noneback to summary
public None()