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

    • _enumValuesToWrite

      protected final EnumValuesToWrite _enumValuesToWrite
      Container for dynamically resolved serializations for the type.
    • _serializeAsNumber

      protected final Boolean _serializeAsNumber
      If statically known, whether to serialize as numeric index (TRUE) or as textual name (FALSE). null means not statically known: falls back to global EnumFeature.WRITE_ENUMS_USING_INDEX at runtime.

      Note: when TRUE (explicit Shape.NUMBER), numeric @JsonProperty values are used as indexes; non-numeric values are written as-is (as Strings). When null and the global feature is enabled, ordinal is always used.

      Since:
      3.2 (renamed from earlier _serializeAsIndex)
  • 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 numeric" 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

      @Deprecated public EnumValues getEnumValues()
      Deprecated.
    • serialize

      public void serialize(Enum<?> en, JsonGenerator g, 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<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:
      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
    • _serializeAsNumber

      protected final boolean _serializeAsNumber(SerializationContext ctxt)
    • _isShapeWrittenUsingNumber

      protected static Boolean _isShapeWrittenUsingNumber(Class<?> enumClass, JsonFormat.Value format, boolean fromClass, Boolean defaultValue)
      Helper method called to check whether serialization should be done using numeric representation or not.