Class Iso8601Serializer

java.lang.Object
com.fasterxml.jackson.databind.JsonSerializer<T>
com.fasterxml.jackson.databind.ser.std.StdSerializer<ZonedDateTime>
org.sdase.commons.server.jackson.Iso8601Serializer
All Implemented Interfaces:
com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatVisitable, com.fasterxml.jackson.databind.jsonschema.SchemaAware, Serializable
Direct Known Subclasses:
Iso8601Serializer.WithMillis

public class Iso8601Serializer extends com.fasterxml.jackson.databind.ser.std.StdSerializer<ZonedDateTime>
A serializer used to write ZonedDateTime in ISO 8601 datetime format contain date, hours, minutes, seconds and time zone.

The serializer should be used instead of JsonFormat.pattern() because it does not affect the tolerant deserialization Jackson provides by default.

Use the serializer at ZonedDateTime properties to activate it:

   class MyResource {
     @JsonSerialize(using = Iso8601Serializer.class)
     private ZonedDateTime createdAt;
     // writes json as {"createdAt": "2018-11-21T13:16:47Z"} for UTC
     // or {"createdAt": "2018-11-21T13:16:47+01:00"} for CET
     // ...
   }
 }
You can add the serializer to the ObjectMapper to use it as default:
  SimpleModule module = new SimpleModule();
   module.addSerializer(ZonedDateTime.class, this.defaultSerializer);
   objectMapper.registerModule(module);
 

Please note that the Iso8601Serializer is already registered in the default ObjectMapper that is configured by the ObjectMapperConfigurationUtil.

Note that there is a subclass to write including milliseconds: Iso8601Serializer.WithMillis

See Also:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
    A serializer used to write ZonedDateTime in ISO 8601 datetime format contain date, hours, minutes, seconds, milli seconds and time zone.

    Nested classes/interfaces inherited from class com.fasterxml.jackson.databind.JsonSerializer

    com.fasterxml.jackson.databind.JsonSerializer.None
  • Field Summary

    Fields inherited from class com.fasterxml.jackson.databind.ser.std.StdSerializer

    _handledType
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
     
     
    protected
    Constructor for subclasses that create a new Iso8601Serializer for ZonedDateTime using a custom pattern for formatting.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    acceptJsonFormatVisitor(com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatVisitorWrapper visitor, com.fasterxml.jackson.databind.JavaType typeHint)
     
    void
    serialize(ZonedDateTime value, com.fasterxml.jackson.core.JsonGenerator gen, com.fasterxml.jackson.databind.SerializerProvider arg2)
     

    Methods inherited from class com.fasterxml.jackson.databind.ser.std.StdSerializer

    _neitherNull, _nonEmpty, createSchemaNode, createSchemaNode, findAnnotatedContentSerializer, findContextualConvertingSerializer, findConvertingContentSerializer, findFormatFeature, findFormatOverrides, findIncludeOverrides, findPropertyFilter, getSchema, getSchema, handledType, isDefaultSerializer, visitArrayFormat, visitArrayFormat, visitFloatFormat, visitIntFormat, visitIntFormat, visitStringFormat, visitStringFormat, wrapAndThrow, wrapAndThrow

    Methods inherited from class com.fasterxml.jackson.databind.JsonSerializer

    getDelegatee, isEmpty, isEmpty, isUnwrappingSerializer, properties, replaceDelegatee, serializeWithType, unwrappingSerializer, usesObjectId, withFilterId, withIgnoredProperties

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

  • Method Details

    • serialize

      public void serialize(ZonedDateTime value, com.fasterxml.jackson.core.JsonGenerator gen, com.fasterxml.jackson.databind.SerializerProvider arg2) throws IOException
      Specified by:
      serialize in class com.fasterxml.jackson.databind.ser.std.StdSerializer<ZonedDateTime>
      Throws:
      IOException
    • acceptJsonFormatVisitor

      public void acceptJsonFormatVisitor(com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatVisitorWrapper visitor, com.fasterxml.jackson.databind.JavaType typeHint) throws com.fasterxml.jackson.databind.JsonMappingException
      Specified by:
      acceptJsonFormatVisitor in interface com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatVisitable
      Overrides:
      acceptJsonFormatVisitor in class com.fasterxml.jackson.databind.ser.std.StdSerializer<ZonedDateTime>
      Throws:
      com.fasterxml.jackson.databind.JsonMappingException