diff --git a/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/ZonedDateTimeSerializer.java b/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/ZonedDateTimeSerializer.java index df96534b..289ce464 100644 --- a/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/ZonedDateTimeSerializer.java +++ b/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/ZonedDateTimeSerializer.java @@ -14,13 +14,17 @@ public class ZonedDateTimeSerializer extends InstantSerializerBase dt.toInstant().toEpochMilli(), - ZonedDateTime::toEpochSecond, ZonedDateTime::getNano, - // ISO_ZONED_DATE_TIME is not the ISO format, it is an extension of it - DateTimeFormatter.ISO_OFFSET_DATE_TIME); + ZonedDateTime::toEpochSecond, ZonedDateTime::getNano, + formatter); } - protected ZonedDateTimeSerializer(ZonedDateTimeSerializer base, + private ZonedDateTimeSerializer(ZonedDateTimeSerializer base, Boolean useTimestamp, DateTimeFormatter formatter) { super(base, useTimestamp, formatter); } diff --git a/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestZonedDateTimeSerializationWithCustomFormatter.java b/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestZonedDateTimeSerializationWithCustomFormatter.java new file mode 100644 index 00000000..006b5369 --- /dev/null +++ b/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestZonedDateTimeSerializationWithCustomFormatter.java @@ -0,0 +1,50 @@ +package com.fasterxml.jackson.datatype.jsr310; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; +import com.fasterxml.jackson.databind.module.SimpleModule; +import com.fasterxml.jackson.datatype.jsr310.ser.ZonedDateTimeSerializer; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; + +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; +import java.util.ArrayList; +import java.util.Collection; + +import static org.junit.Assert.assertThat; +import static org.junit.internal.matchers.StringContains.containsString; + +@RunWith(Parameterized.class) +public class TestZonedDateTimeSerializationWithCustomFormatter { + private final DateTimeFormatter formatter; + + public TestZonedDateTimeSerializationWithCustomFormatter(DateTimeFormatter formatter) { + this.formatter = formatter; + } + + @Test + public void testSerialization() throws Exception { + ZonedDateTime zonedDateTime = ZonedDateTime.now(); + assertThat(serializeWith(zonedDateTime, formatter), containsString(zonedDateTime.format(formatter))); + } + + private String serializeWith(ZonedDateTime zonedDateTime, DateTimeFormatter formatter) throws Exception { + ObjectMapper mapper = new ObjectMapper().registerModule(new SimpleModule().addSerializer( + new ZonedDateTimeSerializer(formatter))); + mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); + return mapper.writeValueAsString(zonedDateTime); + } + + @Parameters + public static Collection customFormatters() { + Collection formatters = new ArrayList<>(); + formatters.add(new Object[]{DateTimeFormatter.ISO_ZONED_DATE_TIME}); + formatters.add(new Object[]{DateTimeFormatter.ISO_OFFSET_DATE_TIME}); + formatters.add(new Object[]{DateTimeFormatter.ISO_LOCAL_DATE_TIME}); + formatters.add(new Object[]{DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ")}); + return formatters; + } +}