Class EnumSerializer

All Implemented Interfaces:
JsonFormatVisitable

public class EnumSerializer extends StdScalarSerializer<Enum<?>>
Standard serializer used for Enum types.

Based on StdScalarSerializer since the JSON value is scalar (String).

  • Field Details

    • _values

      protected final EnumValues _values
      This map contains pre-resolved values (since there are ways to customize actual String constants to use) to use as serializations.
    • _serializeAsIndex

      protected final Boolean _serializeAsIndex
      Flag that is set if we statically know serialization choice between index and textual format (null if it needs to be dynamically checked).
    • _valuesByEnumNaming

      protected final EnumValues _valuesByEnumNaming
      Map with key as converted property class defined implementation of EnumNamingStrategy and with value as Enum names collected using Enum.name().
    • _valuesByToString

      protected final EnumValues _valuesByToString
      Map that contains pre-resolved values for Enum.toString() to use for serialization, while respecting JsonProperty and EnumFeature.WRITE_ENUMS_TO_LOWERCASE.
  • Constructor Details

  • Method Details

    • construct

      public static EnumSerializer construct(Class<?> enumClass, SerializationConfig config, BeanDescription beanDesc, JsonFormat.Value format)
      Factory method used by BasicSerializerFactory for constructing serializer instance of Enum types.
    • createContextual

      public ValueSerializer<?> createContextual(SerializationContext ctxt, BeanProperty property)
      To support some level of per-property configuration, we will need to make things contextual. We are limited to "textual vs index" choice here, however.
      Overrides:
      createContextual in class ValueSerializer<Enum<?>>
      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.
    • getEnumValues

      public EnumValues getEnumValues()
    • serialize

      public final void serialize(Enum<?> en, tools.jackson.core.JsonGenerator g, SerializationContext ctxt) throws tools.jackson.core.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<Enum<?>>
      Parameters:
      en - Value to serialize; can not be null.
      g - Generator used to output resulting Json content
      ctxt - Context that can be used to get serializers for serializing Objects value contains, if any.
      Throws:
      tools.jackson.core.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 StdScalarSerializer<Enum<?>>
      typeHint - Type of element (entity like property) being visited
    • _serializeAsIndex

      protected final boolean _serializeAsIndex(SerializationContext ctxt)
    • _isShapeWrittenUsingIndex

      protected static Boolean _isShapeWrittenUsingIndex(Class<?> enumClass, JsonFormat.Value format, boolean fromClass, Boolean defaultValue)
      Helper method called to check whether serialization should be done using index (number) or not.
    • constructEnumNamingStrategyValues

      protected static EnumValues constructEnumNamingStrategyValues(SerializationConfig config, Class<Enum<?>> enumClass, AnnotatedClass annotatedClass)
      Factory method used to resolve an instance of EnumValues with EnumNamingStrategy applied for the target class.