Class StdDelegatingSerializer

All Implemented Interfaces:
JsonFormatVisitable

public class StdDelegatingSerializer extends StdSerializer<Object>
Serializer implementation where given Java type is first converted to an intermediate "delegate type" (using a configured Converter, and then this delegate value is serialized by Jackson.

Note that although types may be related, they must not be same; trying to do this will result in an exception.

  • Field Details

    • _property

      protected final BeanProperty _property
    • _converter

      protected final Converter<Object,?> _converter
    • _delegateType

      protected final JavaType _delegateType
      Fully resolved delegate type, with generic information if any available.
    • _delegateSerializer

      protected final ValueSerializer<Object> _delegateSerializer
      Underlying serializer for type T.
    • _dynamicValueSerializers

      protected PropertySerializerMap _dynamicValueSerializers
      If delegate serializer needs to be accessed dynamically (non-final type, static type not forced), this data structure helps with efficient lookups.
      Since:
      3.0
  • Constructor Details

    • StdDelegatingSerializer

      public StdDelegatingSerializer(Converter<?,?> converter)
    • StdDelegatingSerializer

      public StdDelegatingSerializer(Class<T> cls, Converter<T,?> converter)
    • StdDelegatingSerializer

      public StdDelegatingSerializer(Converter<Object,?> converter, JavaType delegateType, ValueSerializer<?> delegateSerializer, BeanProperty prop)
      Since:
      3.0
  • Method Details

    • withDelegate

      protected StdDelegatingSerializer withDelegate(Converter<Object,?> converter, JavaType delegateType, ValueSerializer<?> delegateSerializer, BeanProperty prop)
      Method used for creating resolved contextual instances. Must be overridden when sub-classing.
    • resolve

      public void resolve(SerializationContext ctxt)
      Description copied from class: ValueSerializer
      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.

      Overrides:
      resolve in class ValueSerializer<Object>
      Parameters:
      ctxt - Currently active serialization context.
    • createContextual

      public ValueSerializer<?> createContextual(SerializationContext ctxt, BeanProperty property)
      Description copied from class: ValueSerializer
      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.

      Overrides:
      createContextual in class ValueSerializer<Object>
      Parameters:
      ctxt - Context to use for accessing config, other serializers
      property - 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:
      Serializer to use for serializing values of specified property; may be this instance or a new instance.
    • getConverter

      protected Converter<Object,?> getConverter()
    • getDelegatee

      public ValueSerializer<?> getDelegatee()
      Description copied from class: ValueSerializer
      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.
      Overrides:
      getDelegatee in class ValueSerializer<Object>
      Returns:
      Serializer this serializer delegates calls to, if null; null otherwise.
    • serialize

      public void serialize(Object value, JsonGenerator gen, SerializationContext ctxt) throws JacksonException
      Description copied from class: ValueSerializer
      Method that can be called to ask implementation to serialize values of type this serializer handles.
      Specified by:
      serialize in class StdSerializer<Object>
      Parameters:
      value - Value to serialize; can not be null.
      gen - Generator used to output resulting Json content
      ctxt - Context that can be used to get serializers for serializing Objects value contains, if any.
      Throws:
      JacksonException
    • serializeWithType

      public void serializeWithType(Object value, JsonGenerator gen, SerializationContext ctxt, TypeSerializer typeSer) throws JacksonException
      Description copied from class: ValueSerializer
      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.
      Overrides:
      serializeWithType in class ValueSerializer<Object>
      Parameters:
      value - Value to serialize; can not be null.
      gen - Generator used to output resulting Json content
      ctxt - Context that can be used to get serializers for serializing Objects value contains, if any.
      typeSer - Type serializer to use for including type information
      Throws:
      JacksonException
    • isEmpty

      public boolean isEmpty(SerializationContext ctxt, Object value)
      Description copied from class: ValueSerializer
      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.

      Overrides:
      isEmpty in class ValueSerializer<Object>
    • acceptJsonFormatVisitor

      public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType typeHint)
      Description copied from class: StdSerializer
      Default implementation specifies no format. This behavior is usually overriden by custom serializers.
      Specified by:
      acceptJsonFormatVisitor in interface JsonFormatVisitable
      Overrides:
      acceptJsonFormatVisitor in class StdSerializer<Object>
      typeHint - Type of element (entity like property) being visited
    • convertValue

      protected Object convertValue(SerializationContext ctxt, Object value)
      Method called to convert from source Java value into delegate value (which will be serialized using standard Jackson serializer for delegate type)

      The default implementation uses configured Converter to do conversion.

      Parameters:
      value - Value to convert
      Returns:
      Result of conversion
    • _findSerializer

      protected ValueSerializer<Object> _findSerializer(Object value, SerializationContext ctxt)
      Helper method used for locating serializer to use in dynamic use case, where actual type value gets converted to is not specified beyond basic Object, and where serializer needs to be located dynamically based on actual value type.