Class EnumSerializer

All Implemented Interfaces:
JsonFormatVisitable, SchemaAware, ContextualSerializer, Serializable

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

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

See Also:
  • 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).
      Since:
      2.1
    • _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().
      Since:
      2.15
    • _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.
      Since:
      2.16
  • Constructor Details

  • Method Details

    • construct

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

      public JsonSerializer<?> createContextual(SerializerProvider serializers, BeanProperty property) throws JsonMappingException
      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.
      Specified by:
      createContextual in interface ContextualSerializer
      Parameters:
      serializers - Serializer provider to use for accessing config, other serializers
      property - Method or field that represents the property (and is used to access value to serialize). Should be available; but there may be cases where caller cannot provide it and null is passed instead (in which case impls usually pass 'this' serializer as is)
      Returns:
      Serializer to use for serializing values of specified property; may be this instance or a new instance.
      Throws:
      JsonMappingException
    • getEnumValues

      public EnumValues getEnumValues()
    • serialize

      public final void serialize(Enum<?> en, com.fasterxml.jackson.core.JsonGenerator gen, SerializerProvider serializers) throws IOException
      Description copied from class: JsonSerializer
      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.
      gen - Generator used to output resulting Json content
      serializers - Provider that can be used to get serializers for serializing Objects value contains, if any.
      Throws:
      IOException
    • getSchema

      @Deprecated public JsonNode getSchema(SerializerProvider provider, Type typeHint)
      Deprecated.
      Since 2.15
      Description copied from class: StdSerializer
      Default implementation simply claims type is "string"; usually overriden by custom serializers.
      Specified by:
      getSchema in interface SchemaAware
      Overrides:
      getSchema in class StdScalarSerializer<Enum<?>>
      Parameters:
      provider - The serializer provider.
      typeHint - A hint about the type.
      Returns:
      Json-schema for this serializer.
    • acceptJsonFormatVisitor

      public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType typeHint) throws JsonMappingException
      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<?>>
      Parameters:
      typeHint - Type of element (entity like property) being visited
      Throws:
      JsonMappingException
    • _serializeAsIndex

      protected final boolean _serializeAsIndex(SerializerProvider serializers)
    • _isShapeWrittenUsingIndex

      protected static Boolean _isShapeWrittenUsingIndex(Class<?> enumClass, com.fasterxml.jackson.annotation.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.
      Since:
      2.15