From ccf9ef7361432d45ad2346650a8eb8dc85c0f4df Mon Sep 17 00:00:00 2001 From: vmeunier Date: Wed, 14 Jul 2021 02:20:19 +0200 Subject: [PATCH 1/5] Serialization : adjust the TimeZone of dates if WRITE_DATES_WITH_CONTEXT_TIME_ZONE is specified (#222) * Serialization : adjust the TimeZone of dates if WRITE_DATES_WITH_CONTEXT_TIME_ZONE is specified Co-authored-by: Vincent MEUNIER --- .../jsr310/ser/InstantSerializerBase.java | 9 ++- .../old/TestZonedDateTimeSerialization.java | 4 + .../jsr310/ser/OffsetDateTimeSerTest.java | 37 +++++++++- .../jsr310/ser/ZonedDateTimeSerTest.java | 73 +++++++++++++++++-- pom.xml | 2 +- 5 files changed, 110 insertions(+), 15 deletions(-) diff --git a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/InstantSerializerBase.java b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/InstantSerializerBase.java index 5cd0d6c3..da3d5011 100644 --- a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/InstantSerializerBase.java +++ b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/InstantSerializerBase.java @@ -16,6 +16,8 @@ package com.fasterxml.jackson.datatype.jsr310.ser; +import static com.fasterxml.jackson.databind.SerializationFeature.WRITE_DATES_WITH_CONTEXT_TIME_ZONE; + import java.io.IOException; import java.time.format.DateTimeFormatter; import java.time.temporal.Temporal; @@ -138,10 +140,9 @@ protected String formatValue(T value, SerializerProvider provider) DateTimeFormatter formatter = (_formatter != null) ? _formatter : defaultFormat; if (formatter != null) { if (formatter.getZone() == null) { // timezone set if annotated on property - // 19-Oct-2020, tatu: As per [modules-java#188], only override with explicitly - // set timezone, to minimize change from pre-2.12. May need to further - // improve in future to maybe introduce more configuration. - if (provider.getConfig().hasExplicitTimeZone()) { + // If the user specified to use the context TimeZone explicitly, and the formatter provided doesn't contain a TZ + // Then we use the TZ specified in the objectMapper + if (provider.getConfig().hasExplicitTimeZone() && provider.isEnabled(WRITE_DATES_WITH_CONTEXT_TIME_ZONE)) { formatter = formatter.withZone(provider.getTimeZone().toZoneId()); } } diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestZonedDateTimeSerialization.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestZonedDateTimeSerialization.java index 90baac0b..e6acc7b5 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestZonedDateTimeSerialization.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestZonedDateTimeSerialization.java @@ -23,6 +23,8 @@ import java.time.Instant; import java.time.ZoneId; import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeFormatterBuilder; import java.time.temporal.ChronoField; import java.time.temporal.ChronoUnit; import java.time.temporal.Temporal; @@ -32,6 +34,8 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.datatype.jsr310.DecimalUtils; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; +import com.fasterxml.jackson.datatype.jsr310.ser.ZonedDateTimeSerializer; import org.junit.After; import org.junit.Before; diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/OffsetDateTimeSerTest.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/OffsetDateTimeSerTest.java index dc6238cf..6351f2c3 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/OffsetDateTimeSerTest.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/OffsetDateTimeSerTest.java @@ -1,12 +1,17 @@ package com.fasterxml.jackson.datatype.jsr310.ser; +import static org.junit.Assert.assertEquals; + import java.time.Instant; import java.time.OffsetDateTime; import java.time.ZoneId; +import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import java.time.temporal.Temporal; import java.util.TimeZone; +import org.junit.Test; + import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; @@ -14,10 +19,6 @@ import com.fasterxml.jackson.datatype.jsr310.MockObjectConfiguration; import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - public class OffsetDateTimeSerTest extends ModuleTestBase { @@ -230,4 +231,32 @@ public void testSerializationWithTypeInfoAndMapperTimeZone() throws Exception assertEquals("The value is not correct.", "[\"" + OffsetDateTime.class.getName() + "\",\"" + FORMATTER.format(date) + "\"]", value); } + + @Test + public void testSerializationAsStringWithDefaultTimeZoneAndContextTimeZoneOn() throws Exception { + OffsetDateTime date = OffsetDateTime.now(Z3); + String value = MAPPER.writer() + .with(TimeZone.getTimeZone(Z2)) + .without(SerializationFeature.WRITE_DATES_WITH_ZONE_ID) + .without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) + .with(SerializationFeature.WRITE_DATES_WITH_CONTEXT_TIME_ZONE) + .writeValueAsString(date); + + // We expect to have the date written with the ZoneId Z2 + assertEquals("The value is incorrect", "\"" + FORMATTER.format(date.atZoneSameInstant(Z2)) + "\"", value); + } + + @Test + public void testSerializationAsStringWithDefaultTimeZoneAndContextTimeZoneOff() throws Exception { + ZonedDateTime date = ZonedDateTime.now(Z3); + String value = MAPPER.writer() + .with(TimeZone.getTimeZone(Z2)) + .without(SerializationFeature.WRITE_DATES_WITH_ZONE_ID) + .without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) + .without(SerializationFeature.WRITE_DATES_WITH_CONTEXT_TIME_ZONE) + .writeValueAsString(date); + + // We expect to have the date written with the ZoneId Z3 + assertEquals("The value is incorrect", "\"" + FORMATTER.format(date) + "\"", value); + } } diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/ZonedDateTimeSerTest.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/ZonedDateTimeSerTest.java index 37af45e5..7c9cca93 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/ZonedDateTimeSerTest.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/ZonedDateTimeSerTest.java @@ -16,6 +16,10 @@ package com.fasterxml.jackson.datatype.jsr310.ser; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + import java.time.Instant; import java.time.ZoneId; import java.time.ZonedDateTime; @@ -26,23 +30,22 @@ import java.util.Locale; import java.util.TimeZone; +import org.junit.Test; + import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.databind.DeserializationFeature; 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.DecimalUtils; import com.fasterxml.jackson.datatype.jsr310.MockObjectConfiguration; import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - public class ZonedDateTimeSerTest extends ModuleTestBase { + private static final DateTimeFormatter FORMATTER_WITHOUT_ZONEID = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss"); + private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ISO_OFFSET_DATE_TIME; private static final ZoneId Z1 = ZoneId.of("America/Chicago"); @@ -270,6 +273,64 @@ public void testSerializationAsStringWithZoneIdOn() throws Exception { assertEquals("The value is incorrect.", "\"" + DateTimeFormatter.ISO_ZONED_DATE_TIME.format(date) + "\"", value); } + @Test + public void testSerializationAsStringWithDefaultTimeZoneAndContextTimeZoneOnAndACustomFormatter() throws Exception { + ZonedDateTime date = ZonedDateTime.now(Z3); + // With a custom DateTimeFormatter without a ZoneId. + String value = MAPPER.registerModule(new SimpleModule().addSerializer(new ZonedDateTimeSerializer(FORMATTER_WITHOUT_ZONEID))).writer() + .with(TimeZone.getTimeZone(Z2)) + .without(SerializationFeature.WRITE_DATES_WITH_ZONE_ID) + .without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) + .with(SerializationFeature.WRITE_DATES_WITH_CONTEXT_TIME_ZONE) + .writeValueAsString(date); + + // We expect to have the date written with the datetime of ZoneId Z2 + assertEquals("The value is incorrect", "\"" + date.withZoneSameInstant(Z2).format(FORMATTER_WITHOUT_ZONEID) + "\"", value); + } + + @Test + public void testSerializationAsStringWithDefaultTimeZoneAndContextTimeZoneOffAndACustomFormatter() throws Exception { + ZonedDateTime date = ZonedDateTime.now(Z3); + // With a custom DateTimeFormatter without a Zone. + String value = MAPPER.registerModule(new SimpleModule().addSerializer(new ZonedDateTimeSerializer(FORMATTER_WITHOUT_ZONEID))).writer() + .with(TimeZone.getTimeZone(Z2)) + .without(SerializationFeature.WRITE_DATES_WITH_ZONE_ID) + .without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) + .without(SerializationFeature.WRITE_DATES_WITH_CONTEXT_TIME_ZONE) + .writeValueAsString(date); + + // We expect to have the date written with the datetime of ZoneId Z3 + assertEquals("The value is incorrect", "\"" + date.format(FORMATTER_WITHOUT_ZONEID) + "\"", value); + } + + @Test + public void testSerializationAsStringWithDefaultTimeZoneAndContextTimeZoneOn() throws Exception { + ZonedDateTime date = ZonedDateTime.now(Z3); + String value = MAPPER.writer() + .with(TimeZone.getTimeZone(Z2)) + .without(SerializationFeature.WRITE_DATES_WITH_ZONE_ID) + .without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) + .with(SerializationFeature.WRITE_DATES_WITH_CONTEXT_TIME_ZONE) + .writeValueAsString(date); + + // We expect to have the date written with the ZoneId Z2 + assertEquals("The value is incorrect", "\"" + DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(date.withZoneSameInstant(Z2)) + "\"", value); + } + + @Test + public void testSerializationAsStringWithDefaultTimeZoneAndContextTimeZoneOff() throws Exception { + ZonedDateTime date = ZonedDateTime.now(Z3); + String value = MAPPER.writer() + .with(TimeZone.getTimeZone(Z2)) + .without(SerializationFeature.WRITE_DATES_WITH_ZONE_ID) + .without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) + .without(SerializationFeature.WRITE_DATES_WITH_CONTEXT_TIME_ZONE) + .writeValueAsString(date); + + // We expect to have the date written with the ZoneId Z3 + assertEquals("The value is incorrect", "\"" + DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(date) + "\"", value); + } + @Test public void testSerializationWithTypeInfo01() throws Exception { diff --git a/pom.xml b/pom.xml index 011cb0bb..5219a759 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ com.fasterxml.jackson jackson-base - 2.13.0-SNAPSHOT + 2.13.0-rc1-SNAPSHOT com.fasterxml.jackson.module jackson-modules-java8 From 577b67ddb6693bd028eaf130b9a2d26c7d755399 Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Tue, 13 Jul 2021 18:34:28 -0700 Subject: [PATCH 2/5] prepare for 2.13.0-rc1 --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 5219a759..94df59b9 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ com.fasterxml.jackson jackson-base - 2.13.0-rc1-SNAPSHOT + 2.13.0-rc1 com.fasterxml.jackson.module jackson-modules-java8 From 78379a8a7f00331d6a3e540f2c42340a36c79427 Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Tue, 13 Jul 2021 18:38:32 -0700 Subject: [PATCH 3/5] [maven-release-plugin] prepare release jackson-modules-java8-2.13.0-rc1 --- datatypes/pom.xml | 2 +- datetime/pom.xml | 2 +- parameter-names/pom.xml | 2 +- pom.xml | 4 ++-- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/datatypes/pom.xml b/datatypes/pom.xml index d4c94306..1939b1be 100644 --- a/datatypes/pom.xml +++ b/datatypes/pom.xml @@ -9,7 +9,7 @@ com.fasterxml.jackson.module jackson-modules-java8 - 2.13.0-rc1-SNAPSHOT + 2.13.0-rc1 com.fasterxml.jackson.datatype jackson-datatype-jdk8 diff --git a/datetime/pom.xml b/datetime/pom.xml index 0c13098f..e0a79390 100644 --- a/datetime/pom.xml +++ b/datetime/pom.xml @@ -9,7 +9,7 @@ com.fasterxml.jackson.module jackson-modules-java8 - 2.13.0-rc1-SNAPSHOT + 2.13.0-rc1 com.fasterxml.jackson.datatype jackson-datatype-jsr310 diff --git a/parameter-names/pom.xml b/parameter-names/pom.xml index 9100274c..c8aba78f 100644 --- a/parameter-names/pom.xml +++ b/parameter-names/pom.xml @@ -9,7 +9,7 @@ com.fasterxml.jackson.module jackson-modules-java8 - 2.13.0-rc1-SNAPSHOT + 2.13.0-rc1 jackson-module-parameter-names Jackson-module-parameter-names diff --git a/pom.xml b/pom.xml index 94df59b9..1a151457 100644 --- a/pom.xml +++ b/pom.xml @@ -8,7 +8,7 @@ com.fasterxml.jackson.module jackson-modules-java8 Jackson modules: Java 8 - 2.13.0-rc1-SNAPSHOT + 2.13.0-rc1 pom Parent pom for Jackson modules needed to support Java 8 features and types @@ -24,7 +24,7 @@ scm:git:git@github.com:FasterXML/jackson-modules-java8.git scm:git:git@github.com:FasterXML/jackson-modules-java8.git http://github.com/FasterXML/jackson-modules-java8 - HEAD + jackson-modules-java8-2.13.0-rc1 https://github.com/FasterXML/jackson-modules-java8/issues From c35b3d4a45f03cddd00a0beab87374eb28a88f04 Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Tue, 13 Jul 2021 18:38:38 -0700 Subject: [PATCH 4/5] [maven-release-plugin] prepare for next development iteration --- datatypes/pom.xml | 2 +- datetime/pom.xml | 2 +- parameter-names/pom.xml | 2 +- pom.xml | 4 ++-- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/datatypes/pom.xml b/datatypes/pom.xml index 1939b1be..24a011d1 100644 --- a/datatypes/pom.xml +++ b/datatypes/pom.xml @@ -9,7 +9,7 @@ com.fasterxml.jackson.module jackson-modules-java8 - 2.13.0-rc1 + 2.13.0-rc2-SNAPSHOT com.fasterxml.jackson.datatype jackson-datatype-jdk8 diff --git a/datetime/pom.xml b/datetime/pom.xml index e0a79390..5bfc48b3 100644 --- a/datetime/pom.xml +++ b/datetime/pom.xml @@ -9,7 +9,7 @@ com.fasterxml.jackson.module jackson-modules-java8 - 2.13.0-rc1 + 2.13.0-rc2-SNAPSHOT com.fasterxml.jackson.datatype jackson-datatype-jsr310 diff --git a/parameter-names/pom.xml b/parameter-names/pom.xml index c8aba78f..9725fccf 100644 --- a/parameter-names/pom.xml +++ b/parameter-names/pom.xml @@ -9,7 +9,7 @@ com.fasterxml.jackson.module jackson-modules-java8 - 2.13.0-rc1 + 2.13.0-rc2-SNAPSHOT jackson-module-parameter-names Jackson-module-parameter-names diff --git a/pom.xml b/pom.xml index 1a151457..78a50eb9 100644 --- a/pom.xml +++ b/pom.xml @@ -8,7 +8,7 @@ com.fasterxml.jackson.module jackson-modules-java8 Jackson modules: Java 8 - 2.13.0-rc1 + 2.13.0-rc2-SNAPSHOT pom Parent pom for Jackson modules needed to support Java 8 features and types @@ -24,7 +24,7 @@ scm:git:git@github.com:FasterXML/jackson-modules-java8.git scm:git:git@github.com:FasterXML/jackson-modules-java8.git http://github.com/FasterXML/jackson-modules-java8 - jackson-modules-java8-2.13.0-rc1 + HEAD https://github.com/FasterXML/jackson-modules-java8/issues From 283536ce56ca956b79f2389b7f7d429edf68bf37 Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Tue, 13 Jul 2021 18:40:34 -0700 Subject: [PATCH 5/5] back to snapshot deps --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 78a50eb9..855fbbf1 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ com.fasterxml.jackson jackson-base - 2.13.0-rc1 + 2.13.0-rc2-SNAPSHOT com.fasterxml.jackson.module jackson-modules-java8