Package org.sdase.commons.server.jackson
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
Modifier and TypeClassDescriptionstatic class
A serializer used to writeZonedDateTime
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
ModifierConstructorDescriptionprotected
Iso8601Serializer
(String pattern) Constructor for subclasses that create a newIso8601Serializer
forZonedDateTime
using a custom pattern for formatting. -
Method Summary
Modifier and TypeMethodDescriptionvoid
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
-
Constructor Details
-
Iso8601Serializer
public Iso8601Serializer() -
Iso8601Serializer
Constructor for subclasses that create a newIso8601Serializer
forZonedDateTime
using a custom pattern for formatting.- Parameters:
pattern
- the pattern used for formatting, seeDateTimeFormatter.ofPattern(String)
-
-
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 classcom.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 interfacecom.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatVisitable
- Overrides:
acceptJsonFormatVisitor
in classcom.fasterxml.jackson.databind.ser.std.StdSerializer<ZonedDateTime>
- Throws:
com.fasterxml.jackson.databind.JsonMappingException
-