Class JsonValueSerializer

All Implemented Interfaces:
JsonFormatVisitable

public class JsonValueSerializer extends StdDynamicSerializer<Object>
Serializer class that can serialize Object that have a JsonValue annotation to indicate that serialization should be done by calling the method annotated, and serializing result it returns.

Implementation note: we will post-process resulting serializer (much like what is done with BeanSerializer) to figure out actual serializers for final types. This must be done from createContextual(tools.jackson.databind.SerializationContext, tools.jackson.databind.BeanProperty) method, and NOT from constructor; otherwise we could end up with an infinite loop.

  • Field Details

    • _accessor

      protected final AnnotatedMember _accessor
      Accessor (field, getter) used to access value to serialize.
    • _valueType

      protected final JavaType _valueType
      Value for annotated accessor.
    • _staticTyping

      protected final boolean _staticTyping
    • _forceTypeInformation

      protected final boolean _forceTypeInformation
      This is a flag that is set in rare (?) cases where this serializer is used for "natural" types (boolean, int, String, double); and where we actually must force type information wrapping, even though one would not normally be added.
    • _ignoredProperties

      protected final Set<String> _ignoredProperties
  • Constructor Details

  • Method Details

    • construct

      public static JsonValueSerializer construct(SerializationConfig config, JavaType nominalType, JavaType valueType, boolean staticTyping, TypeSerializer vts, ValueSerializer<?> ser, AnnotatedMember accessor)
    • withResolved

      public JsonValueSerializer withResolved(BeanProperty property, TypeSerializer vts, ValueSerializer<?> ser, boolean forceTypeInfo)
    • isEmpty

      public boolean isEmpty(SerializationContext ctxt, Object bean)
      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>
    • createContextual

      public ValueSerializer<?> createContextual(SerializationContext ctxt, BeanProperty property)
      We can try to find the actual serializer for value, if we can statically figure out what the result type must be.
      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.
    • serialize

      public void serialize(Object bean, 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:
      bean - 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
    • _findSerializer

      protected ValueSerializer<Object> _findSerializer(SerializationContext ctxt, Object value)
    • _withIgnoreProperties

      protected static ValueSerializer<Object> _withIgnoreProperties(ValueSerializer<?> ser, Set<String> ignoredProperties)
      Internal helper that configures the provided ser to ignore properties specified by JsonIgnoreProperties.
      Parameters:
      ser - Serializer to be configured
      ignoredProperties - Properties to ignore, if any
      Returns:
      Configured serializer with specified properties ignored
    • serializeWithType

      public void serializeWithType(Object bean, JsonGenerator gen, SerializationContext ctxt, TypeSerializer typeSer0) 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:
      bean - 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.
      typeSer0 - 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<Object>
      typeHint - Type of element (entity like property) being visited
    • _acceptJsonFormatVisitorForEnum

      protected boolean _acceptJsonFormatVisitorForEnum(JsonFormatVisitorWrapper visitor, JavaType typeHint, Class<?> enumType)
      Overridable helper method used for special case handling of schema information for Enums.
      Returns:
      True if method handled callbacks; false if not; in latter case caller will send default callbacks
    • isNaturalTypeWithStdHandling

      protected boolean isNaturalTypeWithStdHandling(Class<?> rawType, ValueSerializer<?> ser)