Class ReferenceTypeSerializer<T>

All Implemented Interfaces:
JsonFormatVisitable
Direct Known Subclasses:
AtomicReferenceSerializer, Jdk8OptionalSerializer

public abstract class ReferenceTypeSerializer<T> extends StdDynamicSerializer<T>
Base implementation for values of ReferenceType. Implements most of functionality, only leaving couple of abstract methods for sub-classes to implement.
  • Field Details

    • MARKER_FOR_EMPTY

      public static final Object MARKER_FOR_EMPTY
    • _referredType

      protected final JavaType _referredType
      Value type
    • _unwrapper

      protected final NameTransformer _unwrapper
      In case of unwrapping, need name transformer.
    • _suppressableValue

      protected final Object _suppressableValue
      Value that indicates suppression mechanism to use for values contained; either "filter" (of which equals() is called), or marker value of MARKER_FOR_EMPTY, or null to indicate no filtering for non-null values. Note that inclusion value for Map instance itself is handled by caller (POJO property that refers to the Map value).
    • _suppressNulls

      protected final boolean _suppressNulls
      Flag that indicates what to do with `null` values, distinct from handling of _suppressableValue
  • Constructor Details

  • Method Details

    • unwrappingSerializer

      public ValueSerializer<T> unwrappingSerializer(NameTransformer transformer)
      Description copied from class: ValueSerializer
      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

      Overrides:
      unwrappingSerializer in class ValueSerializer<T>
      Parameters:
      transformer - Name transformation to use to convert between names of unwrapper properties
    • withResolved

      protected abstract ReferenceTypeSerializer<T> withResolved(BeanProperty prop, TypeSerializer vts, ValueSerializer<?> valueSer, NameTransformer unwrapper)
      Mutant factory method called when changes are needed; should construct newly configured instance with new values as indicated.

      NOTE: caller has verified that there are changes, so implementations need NOT check if a new instance is needed.

    • withContentInclusion

      public abstract ReferenceTypeSerializer<T> withContentInclusion(Object suppressableValue, boolean suppressNulls)
      Mutant factory method called to create a differently constructed instance, specifically with different exclusion rules for contained value.

      NOTE: caller has verified that there are changes, so implementations need NOT check if a new instance is needed.

    • _isValuePresent

      protected abstract boolean _isValuePresent(T value)
      Method called to see if there is a value present or not. Note that value itself may still be `null`, even if present, if referential type allows three states (absent, present-null, present-non-null); some only allow two (absent, present-non-null).
    • _getReferenced

      protected abstract Object _getReferenced(T value)
    • _getReferencedIfPresent

      protected abstract Object _getReferencedIfPresent(T value)
    • 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<T>
      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.
    • _useStatic

      protected boolean _useStatic(SerializationContext serializers, BeanProperty property, JavaType referredType)
    • isEmpty

      public boolean isEmpty(SerializationContext provider, T value) throws JacksonException
      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<T>
      Throws:
      JacksonException
    • isUnwrappingSerializer

      public boolean isUnwrappingSerializer()
      Description copied from class: ValueSerializer
      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.
      Overrides:
      isUnwrappingSerializer in class ValueSerializer<T>
    • getReferredType

      public JavaType getReferredType()
    • serialize

      public void serialize(T ref, JsonGenerator g, SerializationContext provider) 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<T>
      Parameters:
      ref - Value to serialize; can not be null.
      g - Generator used to output resulting Json content
      provider - Context that can be used to get serializers for serializing Objects value contains, if any.
      Throws:
      JacksonException
    • serializeWithType

      public void serializeWithType(T ref, JsonGenerator g, SerializationContext provider, 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<T>
      Parameters:
      ref - Value to serialize; can not be null.
      g - Generator used to output resulting Json content
      provider - 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
    • 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<T>
      typeHint - Type of element (entity like property) being visited