From 40def86093ba47ea5912e29bac0e8d10a44ade0e Mon Sep 17 00:00:00 2001 From: Zoltan Kiss Date: Sat, 13 Dec 2014 19:35:11 +0000 Subject: [PATCH 01/11] #13 Adding support for overriding string conversion when WRITE_DATES_AS_TIMESTAMPS is disabled --- .../jsr310/ser/InstantSerializerBase.java | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/InstantSerializerBase.java b/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/InstantSerializerBase.java index 593d14b1..4a950f0f 100644 --- a/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/InstantSerializerBase.java +++ b/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/InstantSerializerBase.java @@ -23,6 +23,7 @@ import java.io.IOException; import java.time.temporal.Temporal; +import java.util.function.Function; import java.util.function.ToIntFunction; import java.util.function.ToLongFunction; @@ -37,13 +38,23 @@ public class InstantSerializerBase extends JSR310SerializerB private final ToIntFunction getNanoseconds; + private final Function toIsoString; + protected InstantSerializerBase(Class supportedType, ToLongFunction getEpochMillis, ToLongFunction getEpochSeconds, ToIntFunction getNanoseconds) + { + this(supportedType, getEpochMillis, getEpochSeconds, getNanoseconds, null); + } + + protected InstantSerializerBase(Class supportedType, ToLongFunction getEpochMillis, + ToLongFunction getEpochSeconds, ToIntFunction getNanoseconds, + Function toIsoString) { super(supportedType); this.getEpochMillis = getEpochMillis; this.getEpochSeconds = getEpochSeconds; this.getNanoseconds = getNanoseconds; + this.toIsoString = toIsoString; } @Override @@ -66,7 +77,14 @@ public void serialize(T instant, JsonGenerator generator, SerializerProvider pro } else { - generator.writeString(instant.toString()); + if(toIsoString == null) + { + generator.writeString(instant.toString()); + } + else + { + generator.writeString(toIsoString.apply(instant)); + } } } } From 092762f786da058091a62a0c4caf823227277124 Mon Sep 17 00:00:00 2001 From: Zoltan Kiss Date: Sat, 13 Dec 2014 19:36:41 +0000 Subject: [PATCH 02/11] #13 ZonedDateTimeSerializer provides function to generate ISO-8601 timestamp --- .../jackson/datatype/jsr310/ser/ZonedDateTimeSerializer.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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 bc64188f..26928199 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 @@ -1,6 +1,7 @@ package com.fasterxml.jackson.datatype.jsr310.ser; import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; public class ZonedDateTimeSerializer extends InstantSerializerBase { @@ -8,6 +9,7 @@ public class ZonedDateTimeSerializer extends InstantSerializerBase dt.toInstant().toEpochMilli(), - ZonedDateTime::toEpochSecond, ZonedDateTime::getNano); + ZonedDateTime::toEpochSecond, ZonedDateTime::getNano, + dt -> DateTimeFormatter.ISO_ZONED_DATE_TIME.format(dt)); } } From 1d5730eba6a96a759fa931f11c3178d5623c3fd4 Mon Sep 17 00:00:00 2001 From: Zoltan Kiss Date: Sat, 13 Dec 2014 23:40:06 +0000 Subject: [PATCH 03/11] #13 toIsoString is always set --- .../datatype/jsr310/ser/InstantSerializerBase.java | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/InstantSerializerBase.java b/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/InstantSerializerBase.java index 4a950f0f..188020e5 100644 --- a/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/InstantSerializerBase.java +++ b/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/InstantSerializerBase.java @@ -43,7 +43,7 @@ public class InstantSerializerBase extends JSR310SerializerB protected InstantSerializerBase(Class supportedType, ToLongFunction getEpochMillis, ToLongFunction getEpochSeconds, ToIntFunction getNanoseconds) { - this(supportedType, getEpochMillis, getEpochSeconds, getNanoseconds, null); + this(supportedType, getEpochMillis, getEpochSeconds, getNanoseconds, t -> t.toString()); } protected InstantSerializerBase(Class supportedType, ToLongFunction getEpochMillis, @@ -77,14 +77,7 @@ public void serialize(T instant, JsonGenerator generator, SerializerProvider pro } else { - if(toIsoString == null) - { - generator.writeString(instant.toString()); - } - else - { - generator.writeString(toIsoString.apply(instant)); - } + generator.writeString(toIsoString.apply(instant)); } } } From 73ab7a6daa359a725a1afa2727082b19e6716447 Mon Sep 17 00:00:00 2001 From: Zoltan Kiss Date: Sat, 13 Dec 2014 23:57:14 +0000 Subject: [PATCH 04/11] #13 Tests expect ISO-8601 formatted dates --- .../jsr310/ser/OffsetDateTimeSerializer.java | 4 ++- .../jsr310/TestInstantSerialization.java | 20 ++++++----- .../TestOffsetDateTimeSerialization.java | 36 ++++++++++--------- .../TestZonedDateTimeSerialization.java | 36 ++++++++++--------- 4 files changed, 55 insertions(+), 41 deletions(-) diff --git a/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/OffsetDateTimeSerializer.java b/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/OffsetDateTimeSerializer.java index fa601326..caa24c3e 100644 --- a/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/OffsetDateTimeSerializer.java +++ b/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/OffsetDateTimeSerializer.java @@ -1,6 +1,7 @@ package com.fasterxml.jackson.datatype.jsr310.ser; import java.time.OffsetDateTime; +import java.time.format.DateTimeFormatter; public class OffsetDateTimeSerializer extends InstantSerializerBase { @@ -8,6 +9,7 @@ public class OffsetDateTimeSerializer extends InstantSerializerBase dt.toInstant().toEpochMilli(), - OffsetDateTime::toEpochSecond, OffsetDateTime::getNano); + OffsetDateTime::toEpochSecond, OffsetDateTime::getNano, + d -> DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(d)); } } diff --git a/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestInstantSerialization.java b/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestInstantSerialization.java index 910a3df7..d57408d2 100644 --- a/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestInstantSerialization.java +++ b/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestInstantSerialization.java @@ -19,10 +19,12 @@ import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; + import org.junit.Before; import org.junit.Test; import java.time.Instant; +import java.time.format.DateTimeFormatter; import java.time.temporal.ChronoUnit; import java.time.temporal.Temporal; @@ -30,6 +32,8 @@ public class TestInstantSerialization { + private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ISO_INSTANT; + private ObjectMapper mapper; @Before @@ -126,7 +130,7 @@ public void testSerializationAsString01() throws Exception String value = this.mapper.writeValueAsString(date); assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", '"' + date.toString() + '"', value); + assertEquals("The value is not correct.", '"' + FORMATTER.format(date) + '"', value); } @Test @@ -138,7 +142,7 @@ public void testSerializationAsString02() throws Exception String value = this.mapper.writeValueAsString(date); assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", '"' + date.toString() + '"', value); + assertEquals("The value is not correct.", '"' + FORMATTER.format(date) + '"', value); } @Test @@ -150,7 +154,7 @@ public void testSerializationAsString03() throws Exception String value = this.mapper.writeValueAsString(date); assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", '"' + date.toString() + '"', value); + assertEquals("The value is not correct.", '"' + FORMATTER.format(date) + '"', value); } @Test @@ -192,7 +196,7 @@ public void testSerializationWithTypeInfo03() throws Exception assertNotNull("The value should not be null.", value); assertEquals("The value is not correct.", - "[\"" + Instant.class.getName() + "\",\"" + date.toString() + "\"]", value); + "[\"" + Instant.class.getName() + "\",\"" + FORMATTER.format(date) + "\"]", value); } @Test @@ -309,7 +313,7 @@ public void testDeserializationAsString01() throws Exception { Instant date = Instant.ofEpochSecond(0L); - Instant value = this.mapper.readValue('"' + date.toString() + '"', Instant.class); + Instant value = this.mapper.readValue('"' + FORMATTER.format(date) + '"', Instant.class); assertNotNull("The value should not be null.", value); assertEquals("The value is not correct.", date, value); @@ -320,7 +324,7 @@ public void testDeserializationAsString02() throws Exception { Instant date = Instant.ofEpochSecond(123456789L, 183917322); - Instant value = this.mapper.readValue('"' + date.toString() + '"', Instant.class); + Instant value = this.mapper.readValue('"' + FORMATTER.format(date) + '"', Instant.class); assertNotNull("The value should not be null.", value); assertEquals("The value is not correct.", date, value); @@ -331,7 +335,7 @@ public void testDeserializationAsString03() throws Exception { Instant date = Instant.now(); - Instant value = this.mapper.readValue('"' + date.toString() + '"', Instant.class); + Instant value = this.mapper.readValue('"' + FORMATTER.format(date) + '"', Instant.class); assertNotNull("The value should not be null.", value); assertEquals("The value is not correct.", date, value); @@ -391,7 +395,7 @@ public void testDeserializationWithTypeInfo04() throws Exception this.mapper.addMixInAnnotations(Temporal.class, MockObjectConfiguration.class); Temporal value = this.mapper.readValue( - "[\"" + Instant.class.getName() + "\",\"" + date.toString() + "\"]", Temporal.class + "[\"" + Instant.class.getName() + "\",\"" + FORMATTER.format(date) + "\"]", Temporal.class ); assertNotNull("The value should not be null.", value); diff --git a/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestOffsetDateTimeSerialization.java b/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestOffsetDateTimeSerialization.java index 20a07f6c..c5fd6b87 100644 --- a/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestOffsetDateTimeSerialization.java +++ b/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestOffsetDateTimeSerialization.java @@ -19,6 +19,7 @@ import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; + import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -27,6 +28,7 @@ import java.time.OffsetDateTime; import java.time.ZoneId; import java.time.ZoneOffset; +import java.time.format.DateTimeFormatter; import java.time.temporal.ChronoField; import java.time.temporal.ChronoUnit; import java.time.temporal.Temporal; @@ -36,6 +38,8 @@ public class TestOffsetDateTimeSerialization { + private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ISO_OFFSET_DATE_TIME; + private static final ZoneId Z1 = ZoneId.of("America/Chicago"); private static final ZoneId Z2 = ZoneId.of("America/Anchorage"); @@ -144,7 +148,7 @@ public void testSerializationAsString01() throws Exception String value = this.mapper.writeValueAsString(date); assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", '"' + date.toString() + '"', value); + assertEquals("The value is not correct.", '"' + FORMATTER.format(date) + '"', value); } @Test @@ -156,7 +160,7 @@ public void testSerializationAsString02() throws Exception String value = this.mapper.writeValueAsString(date); assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", '"' + date.toString() + '"', value); + assertEquals("The value is not correct.", '"' + FORMATTER.format(date) + '"', value); } @Test @@ -168,7 +172,7 @@ public void testSerializationAsString03() throws Exception String value = this.mapper.writeValueAsString(date); assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", '"' + date.toString() + '"', value); + assertEquals("The value is not correct.", '"' + FORMATTER.format(date) + '"', value); } @Test @@ -212,7 +216,7 @@ public void testSerializationWithTypeInfo03() throws Exception assertNotNull("The value should not be null.", value); assertEquals("The value is not correct.", - "[\"" + OffsetDateTime.class.getName() + "\",\"" + date.toString() + "\"]", value); + "[\"" + OffsetDateTime.class.getName() + "\",\"" + FORMATTER.format(date) + "\"]", value); } @Test @@ -468,7 +472,7 @@ public void testDeserializationAsString01WithoutTimeZone() throws Exception OffsetDateTime date = OffsetDateTime.ofInstant(Instant.ofEpochSecond(0L), Z1); this.mapper.configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, true); - OffsetDateTime value = this.mapper.readValue('"' + date.toString() + '"', OffsetDateTime.class); + OffsetDateTime value = this.mapper.readValue('"' + FORMATTER.format(date) + '"', OffsetDateTime.class); assertNotNull("The value should not be null.", value); assertIsEqual(date, value); @@ -482,7 +486,7 @@ public void testDeserializationAsString01WithTimeZone() throws Exception this.mapper.configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, true); this.mapper.setTimeZone(TimeZone.getDefault()); - OffsetDateTime value = this.mapper.readValue('"' + date.toString() + '"', OffsetDateTime.class); + OffsetDateTime value = this.mapper.readValue('"' + FORMATTER.format(date) + '"', OffsetDateTime.class); assertNotNull("The value should not be null.", value); assertIsEqual(date, value); @@ -496,7 +500,7 @@ public void testDeserializationAsString01WithTimeZoneTurnedOff() throws Exceptio this.mapper.configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, false); this.mapper.setTimeZone(TimeZone.getDefault()); - OffsetDateTime value = this.mapper.readValue('"' + date.toString() + '"', OffsetDateTime.class); + OffsetDateTime value = this.mapper.readValue('"' + FORMATTER.format(date) + '"', OffsetDateTime.class); assertNotNull("The value should not be null.", value); assertIsEqual(date, value); @@ -509,7 +513,7 @@ public void testDeserializationAsString02WithoutTimeZone() throws Exception OffsetDateTime date = OffsetDateTime.ofInstant(Instant.ofEpochSecond(123456789L, 183917322), Z2); this.mapper.configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, true); - OffsetDateTime value = this.mapper.readValue('"' + date.toString() + '"', OffsetDateTime.class); + OffsetDateTime value = this.mapper.readValue('"' + FORMATTER.format(date) + '"', OffsetDateTime.class); assertNotNull("The value should not be null.", value); assertIsEqual(date, value); @@ -523,7 +527,7 @@ public void testDeserializationAsString02WithTimeZone() throws Exception this.mapper.configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, true); this.mapper.setTimeZone(TimeZone.getDefault()); - OffsetDateTime value = this.mapper.readValue('"' + date.toString() + '"', OffsetDateTime.class); + OffsetDateTime value = this.mapper.readValue('"' + FORMATTER.format(date) + '"', OffsetDateTime.class); assertNotNull("The value should not be null.", value); assertIsEqual(date, value); @@ -537,7 +541,7 @@ public void testDeserializationAsString02WithTimeZoneTurnedOff() throws Exceptio this.mapper.configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, false); this.mapper.setTimeZone(TimeZone.getDefault()); - OffsetDateTime value = this.mapper.readValue('"' + date.toString() + '"', OffsetDateTime.class); + OffsetDateTime value = this.mapper.readValue('"' + FORMATTER.format(date) + '"', OffsetDateTime.class); assertNotNull("The value should not be null.", value); assertIsEqual(date, value); @@ -550,7 +554,7 @@ public void testDeserializationAsString03WithoutTimeZone() throws Exception OffsetDateTime date = OffsetDateTime.now(Z3); this.mapper.configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, true); - OffsetDateTime value = this.mapper.readValue('"' + date.toString() + '"', OffsetDateTime.class); + OffsetDateTime value = this.mapper.readValue('"' + FORMATTER.format(date) + '"', OffsetDateTime.class); assertNotNull("The value should not be null.", value); assertIsEqual(date, value); @@ -564,7 +568,7 @@ public void testDeserializationAsString03WithTimeZone() throws Exception this.mapper.configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, true); this.mapper.setTimeZone(TimeZone.getDefault()); - OffsetDateTime value = this.mapper.readValue('"' + date.toString() + '"', OffsetDateTime.class); + OffsetDateTime value = this.mapper.readValue('"' + FORMATTER.format(date) + '"', OffsetDateTime.class); assertNotNull("The value should not be null.", value); assertIsEqual(date, value); @@ -578,7 +582,7 @@ public void testDeserializationAsString03WithTimeZoneTurnedOff() throws Exceptio this.mapper.configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, false); this.mapper.setTimeZone(TimeZone.getDefault()); - OffsetDateTime value = this.mapper.readValue('"' + date.toString() + '"', OffsetDateTime.class); + OffsetDateTime value = this.mapper.readValue('"' + FORMATTER.format(date) + '"', OffsetDateTime.class); assertNotNull("The value should not be null.", value); assertIsEqual(date, value); @@ -696,7 +700,7 @@ public void testDeserializationWithTypeInfo04WithoutTimeZone() throws Exception this.mapper.configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, true); this.mapper.addMixInAnnotations(Temporal.class, MockObjectConfiguration.class); Temporal value = this.mapper.readValue( - "[\"" + OffsetDateTime.class.getName() + "\",\"" + date.toString() + "\"]", Temporal.class + "[\"" + OffsetDateTime.class.getName() + "\",\"" + FORMATTER.format(date) + "\"]", Temporal.class ); assertNotNull("The value should not be null.", value); @@ -714,7 +718,7 @@ public void testDeserializationWithTypeInfo04WithTimeZone() throws Exception this.mapper.setTimeZone(TimeZone.getDefault()); this.mapper.addMixInAnnotations(Temporal.class, MockObjectConfiguration.class); Temporal value = this.mapper.readValue( - "[\"" + OffsetDateTime.class.getName() + "\",\"" + date.toString() + "\"]", Temporal.class + "[\"" + OffsetDateTime.class.getName() + "\",\"" + FORMATTER.format(date) + "\"]", Temporal.class ); assertNotNull("The value should not be null.", value); @@ -732,7 +736,7 @@ public void testDeserializationWithTypeInfo04WithTimeZoneTurnedOff() throws Exce this.mapper.setTimeZone(TimeZone.getDefault()); this.mapper.addMixInAnnotations(Temporal.class, MockObjectConfiguration.class); Temporal value = this.mapper.readValue( - "[\"" + OffsetDateTime.class.getName() + "\",\"" + date.toString() + "\"]", Temporal.class + "[\"" + OffsetDateTime.class.getName() + "\",\"" + FORMATTER.format(date) + "\"]", Temporal.class ); assertNotNull("The value should not be null.", value); diff --git a/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestZonedDateTimeSerialization.java b/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestZonedDateTimeSerialization.java index 421f4f41..21f80beb 100644 --- a/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestZonedDateTimeSerialization.java +++ b/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestZonedDateTimeSerialization.java @@ -19,6 +19,7 @@ import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; + import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -26,6 +27,7 @@ import java.time.Instant; import java.time.ZoneId; import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; import java.time.temporal.ChronoField; import java.time.temporal.ChronoUnit; import java.time.temporal.Temporal; @@ -35,6 +37,8 @@ public class TestZonedDateTimeSerialization { + private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ISO_ZONED_DATE_TIME; + private static final ZoneId Z1 = ZoneId.of("America/Chicago"); private static final ZoneId Z2 = ZoneId.of("America/Anchorage"); @@ -145,7 +149,7 @@ public void testSerializationAsString01() throws Exception String value = this.mapper.writeValueAsString(date); assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", '"' + date.toString() + '"', value); + assertEquals("The value is not correct.", '"' + FORMATTER.format(date) + '"', value); } @Test @@ -157,7 +161,7 @@ public void testSerializationAsString02() throws Exception String value = this.mapper.writeValueAsString(date); assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", '"' + date.toString() + '"', value); + assertEquals("The value is not correct.", '"' + FORMATTER.format(date) + '"', value); } @Test @@ -169,7 +173,7 @@ public void testSerializationAsString03() throws Exception String value = this.mapper.writeValueAsString(date); assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", '"' + date.toString() + '"', value); + assertEquals("The value is not correct.", '"' + FORMATTER.format(date) + '"', value); } @Test @@ -213,7 +217,7 @@ public void testSerializationWithTypeInfo03() throws Exception assertNotNull("The value should not be null.", value); assertEquals("The value is not correct.", - "[\"" + ZonedDateTime.class.getName() + "\",\"" + date.toString() + "\"]", value); + "[\"" + ZonedDateTime.class.getName() + "\",\"" + FORMATTER.format(date) + "\"]", value); } @Test @@ -469,7 +473,7 @@ public void testDeserializationAsString01WithoutTimeZone() throws Exception ZonedDateTime date = ZonedDateTime.ofInstant(Instant.ofEpochSecond(0L), Z1); this.mapper.configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, true); - ZonedDateTime value = this.mapper.readValue('"' + date.toString() + '"', ZonedDateTime.class); + ZonedDateTime value = this.mapper.readValue('"' + FORMATTER.format(date) + '"', ZonedDateTime.class); assertNotNull("The value should not be null.", value); assertIsEqual(date, value); @@ -483,7 +487,7 @@ public void testDeserializationAsString01WithTimeZone() throws Exception this.mapper.configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, true); this.mapper.setTimeZone(TimeZone.getDefault()); - ZonedDateTime value = this.mapper.readValue('"' + date.toString() + '"', ZonedDateTime.class); + ZonedDateTime value = this.mapper.readValue('"' + FORMATTER.format(date) + '"', ZonedDateTime.class); assertNotNull("The value should not be null.", value); assertIsEqual(date, value); @@ -497,7 +501,7 @@ public void testDeserializationAsString01WithTimeZoneTurnedOff() throws Exceptio this.mapper.configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, false); this.mapper.setTimeZone(TimeZone.getDefault()); - ZonedDateTime value = this.mapper.readValue('"' + date.toString() + '"', ZonedDateTime.class); + ZonedDateTime value = this.mapper.readValue('"' + FORMATTER.format(date) + '"', ZonedDateTime.class); assertNotNull("The value should not be null.", value); assertIsEqual(date, value); @@ -510,7 +514,7 @@ public void testDeserializationAsString02WithoutTimeZone() throws Exception ZonedDateTime date = ZonedDateTime.ofInstant(Instant.ofEpochSecond(123456789L, 183917322), Z2); this.mapper.configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, true); - ZonedDateTime value = this.mapper.readValue('"' + date.toString() + '"', ZonedDateTime.class); + ZonedDateTime value = this.mapper.readValue('"' + FORMATTER.format(date) + '"', ZonedDateTime.class); assertNotNull("The value should not be null.", value); assertIsEqual(date, value); @@ -524,7 +528,7 @@ public void testDeserializationAsString02WithTimeZone() throws Exception this.mapper.configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, true); this.mapper.setTimeZone(TimeZone.getDefault()); - ZonedDateTime value = this.mapper.readValue('"' + date.toString() + '"', ZonedDateTime.class); + ZonedDateTime value = this.mapper.readValue('"' + FORMATTER.format(date) + '"', ZonedDateTime.class); assertNotNull("The value should not be null.", value); assertIsEqual(date, value); @@ -538,7 +542,7 @@ public void testDeserializationAsString02WithTimeZoneTurnedOff() throws Exceptio this.mapper.configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, false); this.mapper.setTimeZone(TimeZone.getDefault()); - ZonedDateTime value = this.mapper.readValue('"' + date.toString() + '"', ZonedDateTime.class); + ZonedDateTime value = this.mapper.readValue('"' + FORMATTER.format(date) + '"', ZonedDateTime.class); assertNotNull("The value should not be null.", value); assertIsEqual(date, value); @@ -551,7 +555,7 @@ public void testDeserializationAsString03WithoutTimeZone() throws Exception ZonedDateTime date = ZonedDateTime.now(Z3); this.mapper.configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, true); - ZonedDateTime value = this.mapper.readValue('"' + date.toString() + '"', ZonedDateTime.class); + ZonedDateTime value = this.mapper.readValue('"' + FORMATTER.format(date) + '"', ZonedDateTime.class); assertNotNull("The value should not be null.", value); assertIsEqual(date, value); @@ -565,7 +569,7 @@ public void testDeserializationAsString03WithTimeZone() throws Exception this.mapper.configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, true); this.mapper.setTimeZone(TimeZone.getDefault()); - ZonedDateTime value = this.mapper.readValue('"' + date.toString() + '"', ZonedDateTime.class); + ZonedDateTime value = this.mapper.readValue('"' + FORMATTER.format(date) + '"', ZonedDateTime.class); assertNotNull("The value should not be null.", value); assertIsEqual(date, value); @@ -579,7 +583,7 @@ public void testDeserializationAsString03WithTimeZoneTurnedOff() throws Exceptio this.mapper.configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, false); this.mapper.setTimeZone(TimeZone.getDefault()); - ZonedDateTime value = this.mapper.readValue('"' + date.toString() + '"', ZonedDateTime.class); + ZonedDateTime value = this.mapper.readValue('"' + FORMATTER.format(date) + '"', ZonedDateTime.class); assertNotNull("The value should not be null.", value); assertIsEqual(date, value); @@ -697,7 +701,7 @@ public void testDeserializationWithTypeInfo04WithoutTimeZone() throws Exception this.mapper.configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, true); this.mapper.addMixInAnnotations(Temporal.class, MockObjectConfiguration.class); Temporal value = this.mapper.readValue( - "[\"" + ZonedDateTime.class.getName() + "\",\"" + date.toString() + "\"]", Temporal.class + "[\"" + ZonedDateTime.class.getName() + "\",\"" + FORMATTER.format(date) + "\"]", Temporal.class ); assertNotNull("The value should not be null.", value); @@ -715,7 +719,7 @@ public void testDeserializationWithTypeInfo04WithTimeZone() throws Exception this.mapper.setTimeZone(TimeZone.getDefault()); this.mapper.addMixInAnnotations(Temporal.class, MockObjectConfiguration.class); Temporal value = this.mapper.readValue( - "[\"" + ZonedDateTime.class.getName() + "\",\"" + date.toString() + "\"]", Temporal.class + "[\"" + ZonedDateTime.class.getName() + "\",\"" + FORMATTER.format(date) + "\"]", Temporal.class ); assertNotNull("The value should not be null.", value); @@ -733,7 +737,7 @@ public void testDeserializationWithTypeInfo04WithTimeZoneTurnedOff() throws Exce this.mapper.setTimeZone(TimeZone.getDefault()); this.mapper.addMixInAnnotations(Temporal.class, MockObjectConfiguration.class); Temporal value = this.mapper.readValue( - "[\"" + ZonedDateTime.class.getName() + "\",\"" + date.toString() + "\"]", Temporal.class + "[\"" + ZonedDateTime.class.getName() + "\",\"" + FORMATTER.format(date) + "\"]", Temporal.class ); assertNotNull("The value should not be null.", value); From 69c941c87086d2bed40cc5b809f481ef53c39eb6 Mon Sep 17 00:00:00 2001 From: Zoltan Kiss Date: Sun, 14 Dec 2014 00:42:39 +0000 Subject: [PATCH 05/11] #13 Coding style fix --- .../datatype/jsr310/ser/InstantSerializerBase.java | 8 ++++---- .../datatype/jsr310/ser/OffsetDateTimeSerializer.java | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/InstantSerializerBase.java b/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/InstantSerializerBase.java index 188020e5..308f3564 100644 --- a/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/InstantSerializerBase.java +++ b/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/InstantSerializerBase.java @@ -41,14 +41,14 @@ public class InstantSerializerBase extends JSR310SerializerB private final Function toIsoString; protected InstantSerializerBase(Class supportedType, ToLongFunction getEpochMillis, - ToLongFunction getEpochSeconds, ToIntFunction getNanoseconds) + ToLongFunction getEpochSeconds, ToIntFunction getNanoseconds) { this(supportedType, getEpochMillis, getEpochSeconds, getNanoseconds, t -> t.toString()); } protected InstantSerializerBase(Class supportedType, ToLongFunction getEpochMillis, - ToLongFunction getEpochSeconds, ToIntFunction getNanoseconds, - Function toIsoString) + ToLongFunction getEpochSeconds, ToIntFunction getNanoseconds, + Function toIsoString) { super(supportedType); this.getEpochMillis = getEpochMillis; @@ -77,7 +77,7 @@ public void serialize(T instant, JsonGenerator generator, SerializerProvider pro } else { - generator.writeString(toIsoString.apply(instant)); + generator.writeString(this.toIsoString.apply(instant)); } } } diff --git a/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/OffsetDateTimeSerializer.java b/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/OffsetDateTimeSerializer.java index caa24c3e..62d62ab1 100644 --- a/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/OffsetDateTimeSerializer.java +++ b/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/OffsetDateTimeSerializer.java @@ -10,6 +10,6 @@ public class OffsetDateTimeSerializer extends InstantSerializerBase dt.toInstant().toEpochMilli(), OffsetDateTime::toEpochSecond, OffsetDateTime::getNano, - d -> DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(d)); + dt -> DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(dt)); } } From 5d88a0c3fbfca024be1fda9c2ceaecbe81ae6769 Mon Sep 17 00:00:00 2001 From: Zoltan Kiss Date: Thu, 18 Dec 2014 10:52:54 +0000 Subject: [PATCH 06/11] #13 Using offset date time formatter in zoned date time serializer as well --- .../jackson/datatype/jsr310/ser/ZonedDateTimeSerializer.java | 3 ++- .../datatype/jsr310/TestZonedDateTimeSerialization.java | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) 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 26928199..f5ba39a7 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 @@ -10,6 +10,7 @@ public class ZonedDateTimeSerializer extends InstantSerializerBase dt.toInstant().toEpochMilli(), ZonedDateTime::toEpochSecond, ZonedDateTime::getNano, - dt -> DateTimeFormatter.ISO_ZONED_DATE_TIME.format(dt)); + // ISO_ZONED_DATE_TIME is not the ISO format, it is an extension of it + dt -> DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(dt)); } } diff --git a/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestZonedDateTimeSerialization.java b/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestZonedDateTimeSerialization.java index 21f80beb..35b0a06d 100644 --- a/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestZonedDateTimeSerialization.java +++ b/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestZonedDateTimeSerialization.java @@ -37,7 +37,7 @@ public class TestZonedDateTimeSerialization { - private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ISO_ZONED_DATE_TIME; + private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ISO_OFFSET_DATE_TIME; private static final ZoneId Z1 = ZoneId.of("America/Chicago"); From 15935582839495e6fa4532c59f9815ccbd6d99b6 Mon Sep 17 00:00:00 2001 From: Zoltan Kiss Date: Fri, 2 Jan 2015 12:53:59 +0000 Subject: [PATCH 07/11] #13 Using fixed offset as zone in deserialization tests --- .../jsr310/TestZonedDateTimeSerialization.java | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestZonedDateTimeSerialization.java b/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestZonedDateTimeSerialization.java index 35b0a06d..fc95faac 100644 --- a/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestZonedDateTimeSerialization.java +++ b/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestZonedDateTimeSerialization.java @@ -47,6 +47,8 @@ public class TestZonedDateTimeSerialization private static final ZoneId GMT = ZoneId.of("GMT"); + private static final ZoneId FIX_OFFSET = ZoneId.of("-08:00"); + private ObjectMapper mapper; @Before @@ -497,7 +499,7 @@ public void testDeserializationAsString01WithTimeZone() throws Exception @Test public void testDeserializationAsString01WithTimeZoneTurnedOff() throws Exception { - ZonedDateTime date = ZonedDateTime.ofInstant(Instant.ofEpochSecond(0L), Z1); + ZonedDateTime date = ZonedDateTime.ofInstant(Instant.ofEpochSecond(0L), FIX_OFFSET); this.mapper.configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, false); this.mapper.setTimeZone(TimeZone.getDefault()); @@ -505,7 +507,7 @@ public void testDeserializationAsString01WithTimeZoneTurnedOff() throws Exceptio assertNotNull("The value should not be null.", value); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", Z1, value.getZone()); + assertEquals("The time zone is not correct.", FIX_OFFSET, value.getZone()); } @Test @@ -538,7 +540,7 @@ public void testDeserializationAsString02WithTimeZone() throws Exception @Test public void testDeserializationAsString02WithTimeZoneTurnedOff() throws Exception { - ZonedDateTime date = ZonedDateTime.ofInstant(Instant.ofEpochSecond(123456789L, 183917322), Z2); + ZonedDateTime date = ZonedDateTime.ofInstant(Instant.ofEpochSecond(123456789L, 183917322), FIX_OFFSET); this.mapper.configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, false); this.mapper.setTimeZone(TimeZone.getDefault()); @@ -546,7 +548,7 @@ public void testDeserializationAsString02WithTimeZoneTurnedOff() throws Exceptio assertNotNull("The value should not be null.", value); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", Z2, value.getZone()); + assertEquals("The time zone is not correct.", FIX_OFFSET, value.getZone()); } @Test @@ -579,7 +581,7 @@ public void testDeserializationAsString03WithTimeZone() throws Exception @Test public void testDeserializationAsString03WithTimeZoneTurnedOff() throws Exception { - ZonedDateTime date = ZonedDateTime.now(Z3); + ZonedDateTime date = ZonedDateTime.now(FIX_OFFSET); this.mapper.configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, false); this.mapper.setTimeZone(TimeZone.getDefault()); @@ -587,7 +589,7 @@ public void testDeserializationAsString03WithTimeZoneTurnedOff() throws Exceptio assertNotNull("The value should not be null.", value); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", Z3, value.getZone()); + assertEquals("The time zone is not correct.", FIX_OFFSET, value.getZone()); } @Test @@ -731,7 +733,7 @@ public void testDeserializationWithTypeInfo04WithTimeZone() throws Exception @Test public void testDeserializationWithTypeInfo04WithTimeZoneTurnedOff() throws Exception { - ZonedDateTime date = ZonedDateTime.now(Z3); + ZonedDateTime date = ZonedDateTime.now(FIX_OFFSET); this.mapper.configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, false); this.mapper.setTimeZone(TimeZone.getDefault()); @@ -743,7 +745,7 @@ public void testDeserializationWithTypeInfo04WithTimeZoneTurnedOff() throws Exce assertNotNull("The value should not be null.", value); assertTrue("The value should be an ZonedDateTime.", value instanceof ZonedDateTime); assertIsEqual(date, (ZonedDateTime) value); - assertEquals("The time zone is not correct.", Z3, ((ZonedDateTime) value).getZone()); + assertEquals("The time zone is not correct.", FIX_OFFSET, ((ZonedDateTime) value).getZone()); } private static void assertIsEqual(ZonedDateTime expected, ZonedDateTime actual) From 5f036ff0f8cf0967be5433b205bba76f7b24b1df Mon Sep 17 00:00:00 2001 From: Zoltan Kiss Date: Wed, 29 Apr 2015 20:30:06 +0100 Subject: [PATCH 08/11] #13 Review comments --- .../jackson/datatype/jsr310/ser/InstantSerializerBase.java | 2 +- .../jackson/datatype/jsr310/ser/OffsetDateTimeSerializer.java | 2 +- .../jackson/datatype/jsr310/ser/ZonedDateTimeSerializer.java | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/InstantSerializerBase.java b/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/InstantSerializerBase.java index f83c6321..4e553912 100644 --- a/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/InstantSerializerBase.java +++ b/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/InstantSerializerBase.java @@ -45,7 +45,7 @@ public class InstantSerializerBase extends JSR310SerializerB protected InstantSerializerBase(Class supportedType, ToLongFunction getEpochMillis, ToLongFunction getEpochSeconds, ToIntFunction getNanoseconds) { - this(supportedType, getEpochMillis, getEpochSeconds, getNanoseconds, t -> t.toString()); + this(supportedType, getEpochMillis, getEpochSeconds, getNanoseconds, Object::toString); } protected InstantSerializerBase(Class supportedType, ToLongFunction getEpochMillis, diff --git a/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/OffsetDateTimeSerializer.java b/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/OffsetDateTimeSerializer.java index 04be3a60..733a016b 100644 --- a/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/OffsetDateTimeSerializer.java +++ b/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/OffsetDateTimeSerializer.java @@ -12,6 +12,6 @@ public class OffsetDateTimeSerializer extends InstantSerializerBase dt.toInstant().toEpochMilli(), OffsetDateTime::toEpochSecond, OffsetDateTime::getNano, - dt -> DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(dt)); + DateTimeFormatter.ISO_OFFSET_DATE_TIME::format); } } 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 74c702b0..d6a6ce1c 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 @@ -13,6 +13,6 @@ protected ZonedDateTimeSerializer() { super(ZonedDateTime.class, dt -> dt.toInstant().toEpochMilli(), ZonedDateTime::toEpochSecond, ZonedDateTime::getNano, // ISO_ZONED_DATE_TIME is not the ISO format, it is an extension of it - dt -> DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(dt)); + DateTimeFormatter.ISO_OFFSET_DATE_TIME::format); } } From 108fc7d235689a06765756bc76e125d195aef4f1 Mon Sep 17 00:00:00 2001 From: Zoltan Kiss Date: Fri, 29 May 2015 22:46:44 +0100 Subject: [PATCH 09/11] #13 New module class, new serializer serializing the old way --- .../jackson/datatype/jsr310/JSR310Module.java | 14 +- .../datatype/jsr310/JavaTimeModule.java | 244 ++++++++++++++++++ .../ZonedDateTimeWithZoneIdSerializer.java | 20 ++ 3 files changed, 273 insertions(+), 5 deletions(-) create mode 100644 src/main/java/com/fasterxml/jackson/datatype/jsr310/JavaTimeModule.java create mode 100644 src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/ZonedDateTimeWithZoneIdSerializer.java diff --git a/src/main/java/com/fasterxml/jackson/datatype/jsr310/JSR310Module.java b/src/main/java/com/fasterxml/jackson/datatype/jsr310/JSR310Module.java index 60afbb32..8f5fd4e6 100644 --- a/src/main/java/com/fasterxml/jackson/datatype/jsr310/JSR310Module.java +++ b/src/main/java/com/fasterxml/jackson/datatype/jsr310/JSR310Module.java @@ -49,6 +49,7 @@ import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer; import com.fasterxml.jackson.datatype.jsr310.deser.OffsetTimeDeserializer; import com.fasterxml.jackson.datatype.jsr310.deser.YearDeserializer; +import com.fasterxml.jackson.datatype.jsr310.deser.YearMonthDeserializer; import com.fasterxml.jackson.datatype.jsr310.deser.key.DurationKeyDeserializer; import com.fasterxml.jackson.datatype.jsr310.deser.key.InstantKeyDeserializer; import com.fasterxml.jackson.datatype.jsr310.deser.key.LocalDateKeyDeserializer; @@ -63,7 +64,6 @@ import com.fasterxml.jackson.datatype.jsr310.deser.key.ZoneIdKeyDeserializer; import com.fasterxml.jackson.datatype.jsr310.deser.key.ZoneOffsetKeyDeserializer; import com.fasterxml.jackson.datatype.jsr310.deser.key.ZonedDateTimeKeyDeserializer; -import com.fasterxml.jackson.datatype.jsr310.deser.YearMonthDeserializer; import com.fasterxml.jackson.datatype.jsr310.ser.DurationSerializer; import com.fasterxml.jackson.datatype.jsr310.ser.InstantSerializer; import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer; @@ -71,9 +71,9 @@ import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer; import com.fasterxml.jackson.datatype.jsr310.ser.OffsetDateTimeSerializer; import com.fasterxml.jackson.datatype.jsr310.ser.OffsetTimeSerializer; -import com.fasterxml.jackson.datatype.jsr310.ser.YearSerializer; import com.fasterxml.jackson.datatype.jsr310.ser.YearMonthSerializer; -import com.fasterxml.jackson.datatype.jsr310.ser.ZonedDateTimeSerializer; +import com.fasterxml.jackson.datatype.jsr310.ser.YearSerializer; +import com.fasterxml.jackson.datatype.jsr310.ser.ZonedDateTimeWithZoneIdSerializer; import com.fasterxml.jackson.datatype.jsr310.ser.key.ZonedDateTimeKeySerializer; /** @@ -119,10 +119,14 @@ *
  • {@link LocalDate}, {@link LocalTime}, {@link LocalDateTime}, and {@link OffsetTime}, which cannot portably be * converted to timestamps and are instead represented as arrays when WRITE_DATES_AS_TIMESTAMPS is enabled.
  • * + *

    + * This module is soon to be deprecated: the module has a backwards incompatible behavioural change: default preference is to serialize + * temporal values strictly according to ISO-8601 standards. Please start using {@link JavaTimeModule} instead. * * @author Nick Williams * @since 2.2.0 * @see com.fasterxml.jackson.datatype.jsr310.ser.key.Jsr310NullKeySerializer + * @see com.fasterxml.jackson.datatype.jsr310.JavaTimeModule */ public final class JSR310Module extends SimpleModule { @@ -161,10 +165,10 @@ public JSR310Module() addSerializer(Period.class, new ToStringSerializer(Period.class)); addSerializer(Year.class, YearSerializer.INSTANCE); addSerializer(YearMonth.class, YearMonthSerializer.INSTANCE); - addSerializer(ZonedDateTime.class, ZonedDateTimeSerializer.INSTANCE); + addSerializer(ZonedDateTime.class, ZonedDateTimeWithZoneIdSerializer.INSTANCE); // note: actual concrete type is `ZoneRegion`, but that's not visible: addSerializer(ZoneId.class, new ToStringSerializer(ZoneId.class)); - + addSerializer(ZoneOffset.class, new ToStringSerializer(ZoneOffset.class)); // key serializers diff --git a/src/main/java/com/fasterxml/jackson/datatype/jsr310/JavaTimeModule.java b/src/main/java/com/fasterxml/jackson/datatype/jsr310/JavaTimeModule.java new file mode 100644 index 00000000..4ba35ba6 --- /dev/null +++ b/src/main/java/com/fasterxml/jackson/datatype/jsr310/JavaTimeModule.java @@ -0,0 +1,244 @@ +/* + * Copyright 2013 FasterXML.com + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. You may obtain + * a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the license for the specific language governing permissions and + * limitations under the license. + */ + +package com.fasterxml.jackson.datatype.jsr310; + +import java.time.Duration; +import java.time.Instant; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; +import java.time.MonthDay; +import java.time.OffsetDateTime; +import java.time.OffsetTime; +import java.time.Period; +import java.time.Year; +import java.time.YearMonth; +import java.time.ZoneId; +import java.time.ZoneOffset; +import java.time.ZonedDateTime; + +import com.fasterxml.jackson.core.Version; +import com.fasterxml.jackson.databind.BeanDescription; +import com.fasterxml.jackson.databind.DeserializationConfig; +import com.fasterxml.jackson.databind.deser.ValueInstantiator; +import com.fasterxml.jackson.databind.deser.ValueInstantiators; +import com.fasterxml.jackson.databind.deser.std.StdValueInstantiator; +import com.fasterxml.jackson.databind.introspect.AnnotatedClass; +import com.fasterxml.jackson.databind.introspect.AnnotatedMethod; +import com.fasterxml.jackson.databind.module.SimpleModule; +import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; +import com.fasterxml.jackson.datatype.jsr310.deser.DurationDeserializer; +import com.fasterxml.jackson.datatype.jsr310.deser.InstantDeserializer; +import com.fasterxml.jackson.datatype.jsr310.deser.JSR310StringParsableDeserializer; +import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer; +import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer; +import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer; +import com.fasterxml.jackson.datatype.jsr310.deser.OffsetTimeDeserializer; +import com.fasterxml.jackson.datatype.jsr310.deser.YearDeserializer; +import com.fasterxml.jackson.datatype.jsr310.deser.YearMonthDeserializer; +import com.fasterxml.jackson.datatype.jsr310.deser.key.DurationKeyDeserializer; +import com.fasterxml.jackson.datatype.jsr310.deser.key.InstantKeyDeserializer; +import com.fasterxml.jackson.datatype.jsr310.deser.key.LocalDateKeyDeserializer; +import com.fasterxml.jackson.datatype.jsr310.deser.key.LocalDateTimeKeyDeserializer; +import com.fasterxml.jackson.datatype.jsr310.deser.key.LocalTimeKeyDeserializer; +import com.fasterxml.jackson.datatype.jsr310.deser.key.MonthDayKeyDeserializer; +import com.fasterxml.jackson.datatype.jsr310.deser.key.OffsetDateTimeKeyDeserializer; +import com.fasterxml.jackson.datatype.jsr310.deser.key.OffsetTimeKeyDeserializer; +import com.fasterxml.jackson.datatype.jsr310.deser.key.PeriodKeyDeserializer; +import com.fasterxml.jackson.datatype.jsr310.deser.key.YearKeyDeserializer; +import com.fasterxml.jackson.datatype.jsr310.deser.key.YearMothKeyDeserializer; +import com.fasterxml.jackson.datatype.jsr310.deser.key.ZoneIdKeyDeserializer; +import com.fasterxml.jackson.datatype.jsr310.deser.key.ZoneOffsetKeyDeserializer; +import com.fasterxml.jackson.datatype.jsr310.deser.key.ZonedDateTimeKeyDeserializer; +import com.fasterxml.jackson.datatype.jsr310.ser.DurationSerializer; +import com.fasterxml.jackson.datatype.jsr310.ser.InstantSerializer; +import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer; +import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer; +import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer; +import com.fasterxml.jackson.datatype.jsr310.ser.OffsetDateTimeSerializer; +import com.fasterxml.jackson.datatype.jsr310.ser.OffsetTimeSerializer; +import com.fasterxml.jackson.datatype.jsr310.ser.YearMonthSerializer; +import com.fasterxml.jackson.datatype.jsr310.ser.YearSerializer; +import com.fasterxml.jackson.datatype.jsr310.ser.ZonedDateTimeSerializer; +import com.fasterxml.jackson.datatype.jsr310.ser.key.ZonedDateTimeKeySerializer; + +// TODO ObjectMapper.findAndRegisterModules() is unsupported at the moment by this module + +/** + * Class that registers capability of serializing {@code java.time} objects with the Jackson core. + * + *

    + * ObjectMapper mapper = new ObjectMapper();
    + * mapper.registerModule(new JavaTimeModule());
    + * 
    + * + * Most {@code java.time} types are serialized as numbers (integers or decimals as appropriate) if the + * {@link com.fasterxml.jackson.databind.SerializationFeature#WRITE_DATES_AS_TIMESTAMPS} feature is enabled, and otherwise are serialized in + * standard ISO-8601 string representation. ISO-8601 specifies formats + * for representing offset dates and times, zoned dates and times, local dates and times, periods, durations, zones, and more. All + * {@code java.time} types have built-in translation to and from ISO-8601 formats. + *

    + * Granularity of timestamps is controlled through the companion features + * {@link com.fasterxml.jackson.databind.SerializationFeature#WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS} and + * {@link com.fasterxml.jackson.databind.DeserializationFeature#READ_DATE_TIMESTAMPS_AS_NANOSECONDS}. For serialization, timestamps are + * written as fractional numbers (decimals), where the number is seconds and the decimal is fractional seconds, if + * {@code WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS} is enabled (it is by default), with resolution as fine as nanoseconds depending on the + * underlying JDK implementation. If {@code WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS} is disabled, timestamps are written as a whole number of + * milliseconds. At deserialization time, decimal numbers are always read as fractional second timestamps with up-to-nanosecond resolution, + * since the meaning of the decimal is unambiguous. The more ambiguous integer types are read as fractional seconds without a decimal point + * if {@code READ_DATE_TIMESTAMPS_AS_NANOSECONDS} is enabled (it is by default), and otherwise they are read as milliseconds. + *

    + * Some exceptions to this standard serialization/deserialization rule: + *

      + *
    • {@link Period}, which always results in an ISO-8601 format because Periods must be represented in years, months, and/or days.
    • + *
    • {@link Year}, which only contains a year and cannot be represented with a timestamp.
    • + *
    • {@link YearMonth}, which only contains a year and a month and cannot be represented with a timestamp.
    • + *
    • {@link MonthDay}, which only contains a month and a day and cannot be represented with a timestamp.
    • + *
    • {@link ZoneId} and {@link ZoneOffset}, which do not actually store dates and times but are supported with this module nonetheless.
    • + *
    • {@link LocalDate}, {@link LocalTime}, {@link LocalDateTime}, and {@link OffsetTime}, which cannot portably be converted to timestamps + * and are instead represented as arrays when WRITE_DATES_AS_TIMESTAMPS is enabled.
    • + *
    + * + * @author Nick Williams + * @author Zoltan Kiss + * @since 2.6.0 + * @see com.fasterxml.jackson.datatype.jsr310.ser.key.Jsr310NullKeySerializer + */ +public final class JavaTimeModule extends SimpleModule +{ + private static final long serialVersionUID = 1L; + + public JavaTimeModule() + { + super(Version.unknownVersion()); // !!! TEST + // super(PackageVersion.VERSION); + + // first deserializers + addDeserializer(Duration.class, DurationDeserializer.INSTANCE); + addDeserializer(Instant.class, InstantDeserializer.INSTANT); + addDeserializer(LocalDateTime.class, LocalDateTimeDeserializer.INSTANCE); + addDeserializer(LocalDate.class, LocalDateDeserializer.INSTANCE); + addDeserializer(LocalTime.class, LocalTimeDeserializer.INSTANCE); + addDeserializer(MonthDay.class, JSR310StringParsableDeserializer.MONTH_DAY); + addDeserializer(OffsetDateTime.class, InstantDeserializer.OFFSET_DATE_TIME); + addDeserializer(OffsetTime.class, OffsetTimeDeserializer.INSTANCE); + addDeserializer(Period.class, JSR310StringParsableDeserializer.PERIOD); + addDeserializer(Year.class, YearDeserializer.INSTANCE); + addDeserializer(YearMonth.class, YearMonthDeserializer.INSTANCE); + addDeserializer(ZonedDateTime.class, InstantDeserializer.ZONED_DATE_TIME); + addDeserializer(ZoneId.class, JSR310StringParsableDeserializer.ZONE_ID); + addDeserializer(ZoneOffset.class, JSR310StringParsableDeserializer.ZONE_OFFSET); + + // then serializers: + addSerializer(Duration.class, DurationSerializer.INSTANCE); + addSerializer(Instant.class, InstantSerializer.INSTANCE); + addSerializer(LocalDateTime.class, LocalDateTimeSerializer.INSTANCE); + addSerializer(LocalDate.class, LocalDateSerializer.INSTANCE); + addSerializer(LocalTime.class, LocalTimeSerializer.INSTANCE); + addSerializer(MonthDay.class, new ToStringSerializer(MonthDay.class)); + addSerializer(OffsetDateTime.class, OffsetDateTimeSerializer.INSTANCE); + addSerializer(OffsetTime.class, OffsetTimeSerializer.INSTANCE); + addSerializer(Period.class, new ToStringSerializer(Period.class)); + addSerializer(Year.class, YearSerializer.INSTANCE); + addSerializer(YearMonth.class, YearMonthSerializer.INSTANCE); + addSerializer(ZonedDateTime.class, ZonedDateTimeSerializer.INSTANCE); + // note: actual concrete type is `ZoneRegion`, but that's not visible: + addSerializer(ZoneId.class, new ToStringSerializer(ZoneId.class)); + + addSerializer(ZoneOffset.class, new ToStringSerializer(ZoneOffset.class)); + + // key serializers + addKeySerializer(ZonedDateTime.class, ZonedDateTimeKeySerializer.INSTANCE); + + // key deserializers + addKeyDeserializer(Duration.class, DurationKeyDeserializer.INSTANCE); + addKeyDeserializer(Instant.class, InstantKeyDeserializer.INSTANCE); + addKeyDeserializer(LocalDateTime.class, LocalDateTimeKeyDeserializer.INSTANCE); + addKeyDeserializer(LocalDate.class, LocalDateKeyDeserializer.INSTANCE); + addKeyDeserializer(LocalTime.class, LocalTimeKeyDeserializer.INSTANCE); + addKeyDeserializer(MonthDay.class, MonthDayKeyDeserializer.INSTANCE); + addKeyDeserializer(OffsetDateTime.class, OffsetDateTimeKeyDeserializer.INSTANCE); + addKeyDeserializer(OffsetTime.class, OffsetTimeKeyDeserializer.INSTANCE); + addKeyDeserializer(Period.class, PeriodKeyDeserializer.INSTANCE); + addKeyDeserializer(Year.class, YearKeyDeserializer.INSTANCE); + addKeyDeserializer(YearMonth.class, YearMothKeyDeserializer.INSTANCE); + addKeyDeserializer(ZonedDateTime.class, ZonedDateTimeKeyDeserializer.INSTANCE); + addKeyDeserializer(ZoneId.class, ZoneIdKeyDeserializer.INSTANCE); + addKeyDeserializer(ZoneOffset.class, ZoneOffsetKeyDeserializer.INSTANCE); + } + + @Override + public void setupModule(SetupContext context) { + super.setupModule(context); + context.addValueInstantiators(new ValueInstantiators.Base() { + @Override + public ValueInstantiator findValueInstantiator(DeserializationConfig config, + BeanDescription beanDesc, ValueInstantiator defaultInstantiator) + { + Class raw = beanDesc.getBeanClass(); + // 15-May-2015, tatu: In theory not safe, but in practice we do need to do "fuzzy" matching + // because we will (for now) be getting a subtype, but in future may want to downgrade + // to the common base type. Even more, serializer may purposefully force use of base type. + // So... in practice it really should always work, in the end. :) + if (ZoneId.class.isAssignableFrom(raw)) { + // let's assume we should be getting "empty" StdValueInstantiator here: + if (defaultInstantiator instanceof StdValueInstantiator) { + StdValueInstantiator inst = (StdValueInstantiator) defaultInstantiator; + // one further complication: we need ZoneId info, not sub-class + AnnotatedClass ac; + if (raw == ZoneId.class) { + ac = beanDesc.getClassInfo(); + } else { + // we don't need Annotations, so constructing directly is fine here + // even if it's not generally recommended + ac = AnnotatedClass.construct(ZoneId.class, null, null); + } + if (!inst.canCreateFromString()) { + AnnotatedMethod factory = _findFactory(ac, "of", String.class); + if (factory != null) { + inst.configureFromStringCreator(factory); + } + // otherwise... should we indicate an error? + } + // return ZoneIdInstantiator.construct(config, beanDesc, defaultInstantiator); + } + } + return defaultInstantiator; + } + }); + } + + // For + protected AnnotatedMethod _findFactory(AnnotatedClass cls, String name, Class... argTypes) + { + final int argCount = argTypes.length; + for (AnnotatedMethod method : cls.getStaticMethods()) { + if (!name.equals(method.getName()) + || (method.getParameterCount() != argCount)) { + continue; + } + for (int i = 0; i < argCount; ++i) { + Class argType = method.getParameter(i).getRawType(); + if (!argType.isAssignableFrom(argTypes[i])) { + continue; + } + } + return method; + } + return null; + } +} diff --git a/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/ZonedDateTimeWithZoneIdSerializer.java b/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/ZonedDateTimeWithZoneIdSerializer.java new file mode 100644 index 00000000..bf67f768 --- /dev/null +++ b/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/ZonedDateTimeWithZoneIdSerializer.java @@ -0,0 +1,20 @@ +package com.fasterxml.jackson.datatype.jsr310.ser; + +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; + +// TODO deprecate this: SerializationFeature config should be respected, default behaviour should be to +// serialize according to ISO-8601 format +public class ZonedDateTimeWithZoneIdSerializer extends InstantSerializerBase +{ + private static final long serialVersionUID = 1L; + + public static final ZonedDateTimeWithZoneIdSerializer INSTANCE = new ZonedDateTimeWithZoneIdSerializer(); + + protected ZonedDateTimeWithZoneIdSerializer() { + super(ZonedDateTime.class, dt -> dt.toInstant().toEpochMilli(), + ZonedDateTime::toEpochSecond, ZonedDateTime::getNano, + // Serialize in a backwards compatible way: with zone id + DateTimeFormatter.ISO_ZONED_DATE_TIME::format); + } +} From 8543a976a30c00c4f426f9f9f7f20e85d60741bd Mon Sep 17 00:00:00 2001 From: Zoltan Kiss Date: Fri, 29 May 2015 22:59:37 +0100 Subject: [PATCH 10/11] #13 Duplicating old tests to check backwards compatibility --- .../jsr310/old/MockObjectConfiguration.java | 24 + .../datatype/jsr310/old/ModuleTestBase.java | 12 + .../datatype/jsr310/old/PolymorphicTest.java | 34 + .../datatype/jsr310/old/TestDecimalUtils.java | 123 +++ .../old/TestDurationDeserialization.java | 189 +++++ .../old/TestDurationKeySerialization.java | 59 ++ .../jsr310/old/TestDurationSerialization.java | 157 ++++ .../datatype/jsr310/old/TestFeatures.java | 47 ++ .../old/TestInstantKeySerialization.java | 77 ++ .../jsr310/old/TestInstantSerialization.java | 408 ++++++++++ .../old/TestLocalDateKeySerialization.java | 58 ++ .../old/TestLocalDateSerialization.java | 164 ++++ .../TestLocalDateTimeKeySerialization.java | 84 ++ .../old/TestLocalDateTimeSerialization.java | 359 ++++++++ .../old/TestLocalTimeKeySerialization.java | 78 ++ .../old/TestLocalTimeSerialization.java | 366 +++++++++ .../old/TestMonthDayKeySerialization.java | 56 ++ .../jsr310/old/TestMonthDaySerialization.java | 118 +++ .../jsr310/old/TestNullKeySerialization.java | 51 ++ .../TestOffsetDateTimeKeySerialization.java | 96 +++ .../old/TestOffsetDateTimeSerialization.java | 767 ++++++++++++++++++ .../old/TestOffsetTimeKeySerialization.java | 95 +++ .../old/TestOffsetTimeSerialization.java | 369 +++++++++ .../old/TestPeriodKeySerialization.java | 75 ++ .../jsr310/old/TestPeriodSerialization.java | 120 +++ .../jsr310/old/TestYearKeySerialization.java | 53 ++ .../old/TestYearMonthKeySerialization.java | 54 ++ .../old/TestYearMonthSerialization.java | 213 +++++ .../jsr310/old/TestYearSerialization.java | 111 +++ .../old/TestZoneIdKeySerialization.java | 94 +++ .../jsr310/old/TestZoneIdSerialization.java | 108 +++ .../old/TestZoneOffsetKeySerialization.java | 75 ++ .../old/TestZoneOffsetSerialization.java | 131 +++ .../TestZonedDateTimeKeySerialization.java | 106 +++ .../old/TestZonedDateTimeSerialization.java | 754 +++++++++++++++++ 35 files changed, 5685 insertions(+) create mode 100644 src/test/java/com/fasterxml/jackson/datatype/jsr310/old/MockObjectConfiguration.java create mode 100644 src/test/java/com/fasterxml/jackson/datatype/jsr310/old/ModuleTestBase.java create mode 100644 src/test/java/com/fasterxml/jackson/datatype/jsr310/old/PolymorphicTest.java create mode 100644 src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestDecimalUtils.java create mode 100644 src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestDurationDeserialization.java create mode 100644 src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestDurationKeySerialization.java create mode 100644 src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestDurationSerialization.java create mode 100644 src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestFeatures.java create mode 100644 src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestInstantKeySerialization.java create mode 100644 src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestInstantSerialization.java create mode 100644 src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestLocalDateKeySerialization.java create mode 100644 src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestLocalDateSerialization.java create mode 100644 src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestLocalDateTimeKeySerialization.java create mode 100644 src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestLocalDateTimeSerialization.java create mode 100644 src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestLocalTimeKeySerialization.java create mode 100644 src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestLocalTimeSerialization.java create mode 100644 src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestMonthDayKeySerialization.java create mode 100644 src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestMonthDaySerialization.java create mode 100644 src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestNullKeySerialization.java create mode 100644 src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestOffsetDateTimeKeySerialization.java create mode 100644 src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestOffsetDateTimeSerialization.java create mode 100644 src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestOffsetTimeKeySerialization.java create mode 100644 src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestOffsetTimeSerialization.java create mode 100644 src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestPeriodKeySerialization.java create mode 100644 src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestPeriodSerialization.java create mode 100644 src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestYearKeySerialization.java create mode 100644 src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestYearMonthKeySerialization.java create mode 100644 src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestYearMonthSerialization.java create mode 100644 src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestYearSerialization.java create mode 100644 src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestZoneIdKeySerialization.java create mode 100644 src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestZoneIdSerialization.java create mode 100644 src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestZoneOffsetKeySerialization.java create mode 100644 src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestZoneOffsetSerialization.java create mode 100644 src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestZonedDateTimeKeySerialization.java create mode 100644 src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestZonedDateTimeSerialization.java diff --git a/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/MockObjectConfiguration.java b/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/MockObjectConfiguration.java new file mode 100644 index 00000000..599bdc59 --- /dev/null +++ b/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/MockObjectConfiguration.java @@ -0,0 +1,24 @@ +/* + * Copyright 2013 FasterXML.com + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. You may obtain + * a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the license for the specific language governing permissions and + * limitations under the license. + */ + +package com.fasterxml.jackson.datatype.jsr310.old; + +import com.fasterxml.jackson.annotation.JsonTypeInfo; + +@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.WRAPPER_ARRAY, property = "@class") +public interface MockObjectConfiguration +{ +} diff --git a/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/ModuleTestBase.java b/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/ModuleTestBase.java new file mode 100644 index 00000000..e076fa95 --- /dev/null +++ b/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/ModuleTestBase.java @@ -0,0 +1,12 @@ +package com.fasterxml.jackson.datatype.jsr310.old; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.datatype.jsr310.JSR310Module; + +public class ModuleTestBase +{ + protected ObjectMapper newMapper() { + return new ObjectMapper() + .registerModule(new JSR310Module()); + } +} diff --git a/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/PolymorphicTest.java b/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/PolymorphicTest.java new file mode 100644 index 00000000..76a70600 --- /dev/null +++ b/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/PolymorphicTest.java @@ -0,0 +1,34 @@ +package com.fasterxml.jackson.datatype.jsr310.old; + +import java.time.ZoneId; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +import com.fasterxml.jackson.databind.ObjectMapper; + +public class PolymorphicTest extends ModuleTestBase +{ + private final ObjectMapper TYPING_MAPPER = newMapper(); + { + TYPING_MAPPER.enableDefaultTyping(); + } + + // for [datatype-jsr310#24] + @Test + public void testZoneIdAsIs() throws Exception + { + ZoneId exp = ZoneId.of("America/Chicago"); + String json = TYPING_MAPPER.writeValueAsString(exp); + assertEquals(exp, TYPING_MAPPER.readValue(json, ZoneId.class)); + } + + @Test + public void testZoneWithForcedBaseType() throws Exception + { + ZoneId exp = ZoneId.of("America/Chicago"); + String json = TYPING_MAPPER.writerFor(ZoneId.class).writeValueAsString(exp); + assertEquals(exp, TYPING_MAPPER.readValue(json, ZoneId.class)); + } +} diff --git a/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestDecimalUtils.java b/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestDecimalUtils.java new file mode 100644 index 00000000..46b33c83 --- /dev/null +++ b/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestDecimalUtils.java @@ -0,0 +1,123 @@ +package com.fasterxml.jackson.datatype.jsr310.old; + +import static org.junit.Assert.assertEquals; + +import java.math.BigDecimal; + +import com.fasterxml.jackson.datatype.jsr310.DecimalUtils; +import org.junit.Test; + +public class TestDecimalUtils +{ + @Test + public void testToDecimal01() + { + String decimal = DecimalUtils.toDecimal(0, 0); + + assertEquals("The returned decimal is not correct.", "0.000000000", decimal); + } + + @Test + public void testToDecimal02() + { + String decimal = DecimalUtils.toDecimal(15, 72); + + assertEquals("The returned decimal is not correct.", "15.000000072", decimal); + } + + @Test + public void testToDecimal03() + { + String decimal = DecimalUtils.toDecimal(19827342231L, 192837465); + + assertEquals("The returned decimal is not correct.", "19827342231.192837465", decimal); + } + + @Test + public void testToDecimal04() + { + String decimal = DecimalUtils.toDecimal(19827342231L, 0); + + assertEquals("The returned decimal is not correct.", "19827342231.000000000", decimal); + } + + @Test + public void testToDecimal05() + { + String decimal = DecimalUtils.toDecimal(19827342231L, 999999999); + + assertEquals("The returned decimal is not correct.", "19827342231.999999999", decimal); + } + + @Test + public void testExtractNanosecondDecimal01() + { + BigDecimal value = new BigDecimal("0"); + + long seconds = value.longValue(); + assertEquals("The second part is not correct.", 0L, seconds); + + int nanoseconds = DecimalUtils.extractNanosecondDecimal(value, seconds); + assertEquals("The nanosecond part is not correct.", 0, nanoseconds); + } + + @Test + public void testExtractNanosecondDecimal02() + { + BigDecimal value = new BigDecimal("15.000000072"); + + long seconds = value.longValue(); + assertEquals("The second part is not correct.", 15L, seconds); + + int nanoseconds = DecimalUtils.extractNanosecondDecimal(value, seconds); + assertEquals("The nanosecond part is not correct.", 72, nanoseconds); + } + + @Test + public void testExtractNanosecondDecimal03() + { + BigDecimal value = new BigDecimal("15.72"); + + long seconds = value.longValue(); + assertEquals("The second part is not correct.", 15L, seconds); + + int nanoseconds = DecimalUtils.extractNanosecondDecimal(value, seconds); + assertEquals("The nanosecond part is not correct.", 720000000, nanoseconds); + } + + @Test + public void testExtractNanosecondDecimal04() + { + BigDecimal value = new BigDecimal("19827342231.192837465"); + + long seconds = value.longValue(); + assertEquals("The second part is not correct.", 19827342231L, seconds); + + int nanoseconds = DecimalUtils.extractNanosecondDecimal(value, seconds); + assertEquals("The nanosecond part is not correct.", 192837465, nanoseconds); + } + + @Test + public void testExtractNanosecondDecimal05() + { + BigDecimal value = new BigDecimal("19827342231"); + + long seconds = value.longValue(); + assertEquals("The second part is not correct.", 19827342231L, seconds); + + int nanoseconds = DecimalUtils.extractNanosecondDecimal(value, seconds); + assertEquals("The nanosecond part is not correct.", 0, nanoseconds); + } + + @Test + public void testExtractNanosecondDecimal06() + { + BigDecimal value = new BigDecimal("19827342231.999999999"); + + long seconds = value.longValue(); + assertEquals("The second part is not correct.", 19827342231L, seconds); + + int nanoseconds = DecimalUtils.extractNanosecondDecimal(value, seconds); + assertEquals("The nanosecond part is not correct.", 999999999, nanoseconds); + } +} diff --git a/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestDurationDeserialization.java b/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestDurationDeserialization.java new file mode 100644 index 00000000..967011e7 --- /dev/null +++ b/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestDurationDeserialization.java @@ -0,0 +1,189 @@ +package com.fasterxml.jackson.datatype.jsr310.old; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import java.time.Duration; +import java.time.temporal.TemporalAmount; + +import org.junit.Test; + +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.ObjectReader; + +public class TestDurationDeserialization extends ModuleTestBase +{ + private final ObjectReader READER = newMapper().readerFor(Duration.class); + + @Test + public void testDeserializationAsFloat01() throws Exception + { + Duration value = READER.with(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS) + .readValue("60.0"); + + assertNotNull("The value should not be null.", value); + Duration exp = Duration.ofSeconds(60L, 0); + assertEquals("The value is not correct.", exp, value); + } + + @Test + public void testDeserializationAsFloat02() throws Exception + { + Duration value = READER.without(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS) + .readValue("60.0"); + + assertNotNull("The value should not be null.", value); + Duration exp = Duration.ofSeconds(60L, 0); + assertEquals("The value is not correct.", exp, value); + } + + @Test + public void testDeserializationAsFloat03() throws Exception + { + Duration value = READER.with(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS) + .readValue("13498.000008374"); + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", Duration.ofSeconds(13498L, 8374), value); + } + + @Test + public void testDeserializationAsFloat04() throws Exception + { + Duration value = READER.without(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS) + .readValue("13498.000008374"); + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", Duration.ofSeconds(13498L, 8374), value); + } + + @Test + public void testDeserializationAsInt01() throws Exception + { + Duration value = READER.with(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS) + .readValue("60"); + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", Duration.ofSeconds(60L, 0), value); + } + + @Test + public void testDeserializationAsInt02() throws Exception + { + Duration value = READER.without(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS) + .readValue("60000"); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", Duration.ofSeconds(60L, 0), value); + } + + @Test + public void testDeserializationAsInt03() throws Exception + { + Duration value = READER.with(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS) + .readValue("13498"); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", Duration.ofSeconds(13498L, 0), value); + } + + @Test + public void testDeserializationAsInt04() throws Exception + { + Duration value = READER.without(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS) + .readValue("13498000"); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", Duration.ofSeconds(13498L, 0), value); + } + + @Test + public void testDeserializationAsString01() throws Exception + { + Duration exp = Duration.ofSeconds(60L, 0); + Duration value = READER.readValue('"' + exp.toString() + '"'); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", exp, value); + } + + @Test + public void testDeserializationAsString02() throws Exception + { + Duration exp = Duration.ofSeconds(13498L, 8374); + Duration value = READER.readValue('"' + exp.toString() + '"'); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", exp, value); + } + + @Test + public void testDeserializationAsString03() throws Exception + { + Duration value = READER.readValue("\" \""); + assertNull("The value should be null.", value); + } + + @Test + public void testDeserializationWithTypeInfo01() throws Exception + { + Duration duration = Duration.ofSeconds(13498L, 8374); + + String prefix = "[\"" + Duration.class.getName() + "\","; + + ObjectMapper mapper = newMapper(); + mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false); + mapper.addMixIn(TemporalAmount.class, MockObjectConfiguration.class); + TemporalAmount value = mapper.readValue(prefix + "13498.000008374]", TemporalAmount.class); + + assertNotNull("The value should not be null.", value); + assertTrue("The value should be a Duration.", value instanceof Duration); + assertEquals("The value is not correct.", duration, value); + } + + @Test + public void testDeserializationWithTypeInfo02() throws Exception + { + String prefix = "[\"" + Duration.class.getName() + "\","; + + ObjectMapper mapper = newMapper(); + mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, true); + mapper.addMixIn(TemporalAmount.class, MockObjectConfiguration.class); + TemporalAmount value = mapper.readValue(prefix + "13498]", TemporalAmount.class); + + assertNotNull("The value should not be null.", value); + assertTrue("The value should be a Duration.", value instanceof Duration); + assertEquals("The value is not correct.", Duration.ofSeconds(13498L), value); + } + + @Test + public void testDeserializationWithTypeInfo03() throws Exception + { + String prefix = "[\"" + Duration.class.getName() + "\","; + + ObjectMapper mapper = newMapper(); + mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false); + mapper.addMixIn(TemporalAmount.class, MockObjectConfiguration.class); + TemporalAmount value = mapper.readValue(prefix + "13498837]", TemporalAmount.class); + + assertNotNull("The value should not be null.", value); + assertTrue("The value should be a Duration.", value instanceof Duration); + assertEquals("The value is not correct.", Duration.ofSeconds(13498L, 837000000), value); + } + + @Test + public void testDeserializationWithTypeInfo04() throws Exception + { + Duration duration = Duration.ofSeconds(13498L, 8374); + + String prefix = "[\"" + Duration.class.getName() + "\","; + + ObjectMapper mapper = newMapper(); + mapper.addMixIn(TemporalAmount.class, MockObjectConfiguration.class); + TemporalAmount value = mapper.readValue(prefix + '"' + duration.toString() + "\"]", TemporalAmount.class); + + assertNotNull("The value should not be null.", value); + assertTrue("The value should be a Duration.", value instanceof Duration); + assertEquals("The value is not correct.", duration, value); + } +} diff --git a/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestDurationKeySerialization.java b/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestDurationKeySerialization.java new file mode 100644 index 00000000..0fa7d0d8 --- /dev/null +++ b/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestDurationKeySerialization.java @@ -0,0 +1,59 @@ +package com.fasterxml.jackson.datatype.jsr310.old; + +import static org.junit.Assert.assertEquals; + +import java.time.Duration; +import java.util.HashMap; +import java.util.Map; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.datatype.jsr310.JSR310Module; +import org.junit.Before; +import org.junit.Test; + +public class TestDurationKeySerialization { + + private static final TypeReference> TYPE_REF = new TypeReference>() { + }; + private static final Duration DURATION = Duration.ofMinutes(13).plusSeconds(37).plusNanos(120 * 1000 * 1000); + private static final String DURATION_STRING = "PT13M37.12S"; + + private ObjectMapper om; + private Map map; + + @Before + public void setUp() { + this.om = new ObjectMapper(); + om.registerModule(new JSR310Module()); + map = new HashMap<>(); + } + + /* + * ObjectMapper configuration is not respected at deserialization and serialization at the moment. + */ + + @Test + public void testSerialization() throws Exception { + map.put(DURATION, "test"); + + String value = om.writeValueAsString(map); + + assertEquals("Value is not correct", map(DURATION_STRING, "test"), value); + } + + @Test + public void testDeserialization() throws Exception { + Map value = om.readValue( + map(DURATION_STRING, "test"), + TYPE_REF); + + map.put(DURATION, "test"); + assertEquals("Value is not correct", map, value); + } + + private String map(String key, String value) { + return String.format("{\"%s\":\"%s\"}", key, value); + } + +} diff --git a/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestDurationSerialization.java b/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestDurationSerialization.java new file mode 100644 index 00000000..e4fd1841 --- /dev/null +++ b/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestDurationSerialization.java @@ -0,0 +1,157 @@ +package com.fasterxml.jackson.datatype.jsr310.old; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.ObjectWriter; +import com.fasterxml.jackson.databind.SerializationFeature; + +import org.junit.Before; +import org.junit.Test; + +import java.time.Duration; +import java.time.temporal.TemporalAmount; + +import static org.junit.Assert.*; + +public class TestDurationSerialization extends ModuleTestBase +{ + private ObjectWriter WRITER; + + @Before + public void setUp() + { + WRITER = newMapper().writer(); + } + + @Test + public void testSerializationAsTimestampNanoseconds01() throws Exception + { + Duration duration = Duration.ofSeconds(60L, 0); + String value = WRITER + .with(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) + .with(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS) + .writeValueAsString(duration); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", "60.000000000", value); + } + + @Test + public void testSerializationAsTimestampNanoseconds02() throws Exception + { + Duration duration = Duration.ofSeconds(13498L, 8374); + String value = WRITER + .with(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) + .with(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS) + .writeValueAsString(duration); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", "13498.000008374", value); + } + + @Test + public void testSerializationAsTimestampMilliseconds01() throws Exception + { + Duration duration = Duration.ofSeconds(60L, 0); + String value = WRITER + .with(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) + .without(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS) + .writeValueAsString(duration); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", "60000", value); + } + + @Test + public void testSerializationAsTimestampMilliseconds02() throws Exception + { + Duration duration = Duration.ofSeconds(13498L, 8374); + String value = WRITER + .with(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) + .without(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS) + .writeValueAsString(duration); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", "13498000", value); + } + + @Test + public void testSerializationAsTimestampMilliseconds03() throws Exception + { + Duration duration = Duration.ofSeconds(13498L, 837481723); + String value = WRITER + .with(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) + .without(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS) + .writeValueAsString(duration); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", "13498837", value); + } + + @Test + public void testSerializationAsString01() throws Exception + { + Duration duration = Duration.ofSeconds(60L, 0); + String value = WRITER + .without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) + .writeValueAsString(duration); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", '"' + duration.toString() + '"', value); + } + + @Test + public void testSerializationAsString02() throws Exception + { + Duration duration = Duration.ofSeconds(13498L, 8374); + String value = WRITER + .without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) + .writeValueAsString(duration); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", '"' + duration.toString() + '"', value); + } + + @Test + public void testSerializationWithTypeInfo01() throws Exception + { + ObjectMapper mapper = newMapper(); // need new to add mix-ins: + mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true); + mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, true); + mapper.addMixIn(TemporalAmount.class, MockObjectConfiguration.class); + Duration duration = Duration.ofSeconds(13498L, 8374); + String value = mapper.writeValueAsString(duration); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", + "[\"" + Duration.class.getName() + "\",13498.000008374]", value); + } + + @Test + public void testSerializationWithTypeInfo02() throws Exception + { + ObjectMapper mapper = newMapper(); // need new to add mix-ins: + mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true); + mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false); + mapper.addMixIn(TemporalAmount.class, MockObjectConfiguration.class); + Duration duration = Duration.ofSeconds(13498L, 837481723); + String value = mapper.writeValueAsString(duration); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", + "[\"" + Duration.class.getName() + "\",13498837]", value); + } + + @Test + public void testSerializationWithTypeInfo03() throws Exception + { + ObjectMapper mapper = newMapper(); // need new to add mix-ins: + mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); + mapper.addMixIn(TemporalAmount.class, MockObjectConfiguration.class); + Duration duration = Duration.ofSeconds(13498L, 8374); + String value = mapper.writeValueAsString(duration); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", + "[\"" + Duration.class.getName() + "\",\"" + duration.toString() + "\"]", value); + } +} diff --git a/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestFeatures.java b/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestFeatures.java new file mode 100644 index 00000000..724806ae --- /dev/null +++ b/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestFeatures.java @@ -0,0 +1,47 @@ +/* + * Copyright 2013 FasterXML.com + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. You may obtain + * a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the license for the specific language governing permissions and + * limitations under the license. + */ + +package com.fasterxml.jackson.datatype.jsr310.old; + +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.SerializationFeature; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class TestFeatures +{ + @Test + public void testWriteDateTimestampsAsNanosecondsSettingEnabledByDefault() + { + assertTrue("Write date timestamps as nanoseconds setting should be enabled by default.", + SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS.enabledByDefault()); + } + + @Test + public void testReadDateTimestampsAsNanosecondsSettingEnabledByDefault() + { + assertTrue("Read date timestamps as nanoseconds setting should be enabled by default.", + DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS.enabledByDefault()); + } + + @Test + public void testAdjustDatesToContextTimeZoneSettingEnabledByDefault() + { + assertTrue("Adjust dates to context time zone setting should be enabled by default.", + DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE.enabledByDefault()); + } +} diff --git a/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestInstantKeySerialization.java b/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestInstantKeySerialization.java new file mode 100644 index 00000000..cddb816d --- /dev/null +++ b/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestInstantKeySerialization.java @@ -0,0 +1,77 @@ +package com.fasterxml.jackson.datatype.jsr310.old; + +import static org.junit.Assert.assertEquals; + +import java.time.Instant; +import java.util.HashMap; +import java.util.Map; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.datatype.jsr310.JSR310Module; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class TestInstantKeySerialization { + + private static final TypeReference> TYPE_REF = new TypeReference>() { + }; + private static final Instant INSTANT_0 = Instant.ofEpochMilli(0); + private static final String INSTANT_0_STRING = "1970-01-01T00:00:00Z"; + private static final Instant INSTANT = Instant.ofEpochSecond(1426325213l, 590000000l); + private static final String INSTANT_STRING = "2015-03-14T09:26:53.590Z"; + + private ObjectMapper om; + private Map map; + + @Before + public void setUp() { + this.om = new ObjectMapper(); + om.registerModule(new JSR310Module()); + map = new HashMap<>(); + } + + /* + * ObjectMapper configuration is not respected at deserialization and serialization at the moment. + */ + + @Test + public void testSerialization0() throws Exception { + map.put(INSTANT_0, "test"); + + String value = om.writeValueAsString(map); + + Assert.assertEquals("Value is incorrect", map(INSTANT_0_STRING, "test"), value); + } + + @Test + public void testSerialization1() throws Exception { + map.put(INSTANT, "test"); + + String value = om.writeValueAsString(map); + + assertEquals("Value is incorrect", map(INSTANT_STRING, "test"), value); + } + + @Test + public void testDeserialization0() throws Exception { + Map value = om.readValue(map(INSTANT_0_STRING, "test"), TYPE_REF); + + map.put(INSTANT_0, "test"); + assertEquals("Value is incorrect", map, value); + } + + @Test + public void testDeserialization1() throws Exception { + Map value = om.readValue(map(INSTANT_STRING, "test"), TYPE_REF); + + map.put(INSTANT, "test"); + assertEquals("Value is incorrect", map, value); + } + + private String map(String key, String value) { + return String.format("{\"%s\":\"%s\"}", key, value); + } + +} diff --git a/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestInstantSerialization.java b/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestInstantSerialization.java new file mode 100644 index 00000000..2da5888d --- /dev/null +++ b/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestInstantSerialization.java @@ -0,0 +1,408 @@ +/* + * Copyright 2013 FasterXML.com + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. You may obtain + * a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the license for the specific language governing permissions and + * limitations under the license. + */ + +package com.fasterxml.jackson.datatype.jsr310.old; + +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.format.DateTimeFormatter; +import java.time.temporal.ChronoUnit; +import java.time.temporal.Temporal; + +import com.fasterxml.jackson.databind.DeserializationFeature; +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.JSR310Module; +import org.junit.Before; +import org.junit.Test; + +public class TestInstantSerialization +{ + private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ISO_INSTANT; + + private ObjectMapper mapper; + + @Before + public void setUp() + { + this.mapper = new ObjectMapper(); + this.mapper.registerModule(new JSR310Module()); + } + + @Test + public void testSerializationAsTimestamp01Nanoseconds() throws Exception + { + Instant date = Instant.ofEpochSecond(0L); + + this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true); + this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, true); + String value = this.mapper.writeValueAsString(date); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", "0.000000000", value); + } + + @Test + public void testSerializationAsTimestamp01Milliseconds() throws Exception + { + Instant date = Instant.ofEpochSecond(0L); + + this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true); + this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false); + String value = this.mapper.writeValueAsString(date); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", "0", value); + } + + @Test + public void testSerializationAsTimestamp02Nanoseconds() throws Exception + { + Instant date = Instant.ofEpochSecond(123456789L, 183917322); + + this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true); + this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, true); + String value = this.mapper.writeValueAsString(date); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", "123456789.183917322", value); + } + + @Test + public void testSerializationAsTimestamp02Milliseconds() throws Exception + { + Instant date = Instant.ofEpochSecond(123456789L, 183917322); + + this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true); + this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false); + String value = this.mapper.writeValueAsString(date); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", "123456789183", value); + } + + @Test + public void testSerializationAsTimestamp03Nanoseconds() throws Exception + { + Instant date = Instant.now(); + + this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true); + this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, true); + String value = this.mapper.writeValueAsString(date); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", DecimalUtils.toDecimal(date.getEpochSecond(), date.getNano()), value); + } + + @Test + public void testSerializationAsTimestamp03Milliseconds() throws Exception + { + Instant date = Instant.now(); + + this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true); + this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false); + String value = this.mapper.writeValueAsString(date); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", Long.toString(date.toEpochMilli()), value); + } + + @Test + public void testSerializationAsString01() throws Exception + { + Instant date = Instant.ofEpochSecond(0L); + + this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); + String value = this.mapper.writeValueAsString(date); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", '"' + FORMATTER.format(date) + '"', value); + } + + @Test + public void testSerializationAsString02() throws Exception + { + Instant date = Instant.ofEpochSecond(123456789L, 183917322); + + this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); + String value = this.mapper.writeValueAsString(date); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", '"' + FORMATTER.format(date) + '"', value); + } + + @Test + public void testSerializationAsString03() throws Exception + { + Instant date = Instant.now(); + + this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); + String value = this.mapper.writeValueAsString(date); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", '"' + FORMATTER.format(date) + '"', value); + } + + @Test + public void testSerializationWithTypeInfo01() throws Exception + { + Instant date = Instant.ofEpochSecond(123456789L, 183917322); + + this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true); + this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, true); + this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); + String value = this.mapper.writeValueAsString(date); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", "[\"" + Instant.class.getName() + "\",123456789.183917322]", value); + } + + @Test + public void testSerializationWithTypeInfo02() throws Exception + { + Instant date = Instant.ofEpochSecond(123456789L, 183917322); + + this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true); + this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false); + this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); + String value = this.mapper.writeValueAsString(date); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", "[\"" + Instant.class.getName() + "\",123456789183]", value); + } + + @Test + public void testSerializationWithTypeInfo03() throws Exception + { + Instant date = Instant.now(); + + this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); + this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); + String value = this.mapper.writeValueAsString(date); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", + "[\"" + Instant.class.getName() + "\",\"" + FORMATTER.format(date) + "\"]", value); + } + + @Test + public void testDeserializationAsFloat01() throws Exception + { + Instant date = Instant.ofEpochSecond(0L); + + Instant value = this.mapper.readValue("0.000000000", Instant.class); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", date, value); + } + + @Test + public void testDeserializationAsFloat02() throws Exception + { + Instant date = Instant.ofEpochSecond(123456789L, 183917322); + + Instant value = this.mapper.readValue("123456789.183917322", Instant.class); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", date, value); + } + + @Test + public void testDeserializationAsFloat03() throws Exception + { + Instant date = Instant.now(); + + Instant value = this.mapper.readValue( + DecimalUtils.toDecimal(date.getEpochSecond(), date.getNano()), Instant.class + ); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", date, value); + } + + @Test + public void testDeserializationAsInt01Nanoseconds() throws Exception + { + Instant date = Instant.ofEpochSecond(0L); + + this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, true); + Instant value = this.mapper.readValue("0", Instant.class); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", date, value); + } + + @Test + public void testDeserializationAsInt01Milliseconds() throws Exception + { + Instant date = Instant.ofEpochSecond(0L); + + this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false); + Instant value = this.mapper.readValue("0", Instant.class); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", date, value); + } + + @Test + public void testDeserializationAsInt02Nanoseconds() throws Exception + { + Instant date = Instant.ofEpochSecond(123456789L, 0); + + this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, true); + Instant value = this.mapper.readValue("123456789", Instant.class); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", date, value); + } + + @Test + public void testDeserializationAsInt02Milliseconds() throws Exception + { + Instant date = Instant.ofEpochSecond(123456789L, 422000000); + + this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false); + Instant value = this.mapper.readValue("123456789422", Instant.class); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", date, value); + } + + @Test + public void testDeserializationAsInt03Nanoseconds() throws Exception + { + Instant date = Instant.now(); + date = date.minus(date.getNano(), ChronoUnit.NANOS); + + this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, true); + Instant value = this.mapper.readValue(Long.toString(date.getEpochSecond()), Instant.class); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", date, value); + } + + @Test + public void testDeserializationAsInt03Milliseconds() throws Exception + { + Instant date = Instant.now(); + date = date.minus(date.getNano(), ChronoUnit.NANOS); + + this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false); + Instant value = this.mapper.readValue(Long.toString(date.toEpochMilli()), Instant.class); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", date, value); + } + + @Test + public void testDeserializationAsString01() throws Exception + { + Instant date = Instant.ofEpochSecond(0L); + + Instant value = this.mapper.readValue('"' + FORMATTER.format(date) + '"', Instant.class); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", date, value); + } + + @Test + public void testDeserializationAsString02() throws Exception + { + Instant date = Instant.ofEpochSecond(123456789L, 183917322); + + Instant value = this.mapper.readValue('"' + FORMATTER.format(date) + '"', Instant.class); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", date, value); + } + + @Test + public void testDeserializationAsString03() throws Exception + { + Instant date = Instant.now(); + + Instant value = this.mapper.readValue('"' + FORMATTER.format(date) + '"', Instant.class); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", date, value); + } + + @Test + public void testDeserializationWithTypeInfo01() throws Exception + { + Instant date = Instant.ofEpochSecond(123456789L, 183917322); + + this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); + Temporal value = this.mapper.readValue( + "[\"" + Instant.class.getName() + "\",123456789.183917322]", Temporal.class + ); + + assertNotNull("The value should not be null.", value); + assertTrue("The value should be an Instant.", value instanceof Instant); + assertEquals("The value is not correct.", date, value); + } + + @Test + public void testDeserializationWithTypeInfo02() throws Exception + { + Instant date = Instant.ofEpochSecond(123456789L, 0); + + this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, true); + this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); + Temporal value = this.mapper.readValue( + "[\"" + Instant.class.getName() + "\",123456789]", Temporal.class + ); + + assertNotNull("The value should not be null.", value); + assertTrue("The value should be an Instant.", value instanceof Instant); + assertEquals("The value is not correct.", date, value); + } + + @Test + public void testDeserializationWithTypeInfo03() throws Exception + { + Instant date = Instant.ofEpochSecond(123456789L, 422000000); + + this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false); + this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); + Temporal value = this.mapper.readValue( + "[\"" + Instant.class.getName() + "\",123456789422]", Temporal.class + ); + + assertNotNull("The value should not be null.", value); + assertTrue("The value should be an Instant.", value instanceof Instant); + assertEquals("The value is not correct.", date, value); + } + + @Test + public void testDeserializationWithTypeInfo04() throws Exception + { + Instant date = Instant.now(); + + this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); + Temporal value = this.mapper.readValue( + "[\"" + Instant.class.getName() + "\",\"" + FORMATTER.format(date) + "\"]", Temporal.class + ); + + assertNotNull("The value should not be null.", value); + assertTrue("The value should be an Instant.", value instanceof Instant); + assertEquals("The value is not correct.", date, value); + } +} diff --git a/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestLocalDateKeySerialization.java b/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestLocalDateKeySerialization.java new file mode 100644 index 00000000..8e26482d --- /dev/null +++ b/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestLocalDateKeySerialization.java @@ -0,0 +1,58 @@ +package com.fasterxml.jackson.datatype.jsr310.old; + +import static org.junit.Assert.assertEquals; + +import java.time.LocalDate; +import java.util.HashMap; +import java.util.Map; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.datatype.jsr310.JSR310Module; +import org.junit.Before; +import org.junit.Test; + +public class TestLocalDateKeySerialization { + + private static final TypeReference> TYPE_REF = new TypeReference>() { + }; + private static final LocalDate DATE = LocalDate.of(2015, 3, 14); + private static final String DATE_STRING = "2015-03-14"; + + private ObjectMapper om; + private Map map; + + @Before + public void setUp() { + this.om = new ObjectMapper(); + om.registerModule(new JSR310Module()); + map = new HashMap<>(); + } + + /* + * ObjectMapper configuration is not respected at deserialization and serialization at the moment. + */ + + @Test + public void testSerialization() throws Exception { + map.put(DATE, "test"); + + String value = om.writeValueAsString(map); + + assertEquals("Incorrect value", map(DATE_STRING, "test"), value); + } + + @Test + public void testDeserialization() throws Exception { + Map value = om.readValue( + map(DATE_STRING, "test"), + TYPE_REF); + + map.put(DATE, "test"); + assertEquals("Incorrect value", map, value); + } + + private String map(String key, String value) { + return String.format("{\"%s\":\"%s\"}", key, value); + } +} diff --git a/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestLocalDateSerialization.java b/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestLocalDateSerialization.java new file mode 100644 index 00000000..11016d30 --- /dev/null +++ b/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestLocalDateSerialization.java @@ -0,0 +1,164 @@ +/* + * Copyright 2013 FasterXML.com + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. You may obtain + * a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the license for the specific language governing permissions and + * limitations under the license. + */ + +package com.fasterxml.jackson.datatype.jsr310.old; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.time.LocalDate; +import java.time.Month; +import java.time.temporal.Temporal; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; +import com.fasterxml.jackson.datatype.jsr310.JSR310Module; +import org.junit.Before; +import org.junit.Test; + +public class TestLocalDateSerialization +{ + private ObjectMapper mapper; + + @Before + public void setUp() + { + this.mapper = new ObjectMapper(); + this.mapper.registerModule(new JSR310Module()); + } + + @Test + public void testSerializationAsTimestamp01() throws Exception + { + LocalDate date = LocalDate.of(1986, Month.JANUARY, 17); + + this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true); + String value = this.mapper.writeValueAsString(date); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", "[1986,1,17]", value); + } + + @Test + public void testSerializationAsTimestamp02() throws Exception + { + LocalDate date = LocalDate.of(2013, Month.AUGUST, 21); + + this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true); + String value = this.mapper.writeValueAsString(date); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", "[2013,8,21]", value); + } + + @Test + public void testSerializationAsString01() throws Exception + { + LocalDate date = LocalDate.of(1986, Month.JANUARY, 17); + + this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); + String value = this.mapper.writeValueAsString(date); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", '"' + date.toString() + '"', value); + } + + @Test + public void testSerializationAsString02() throws Exception + { + LocalDate date = LocalDate.of(2013, Month.AUGUST, 21); + + this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); + String value = this.mapper.writeValueAsString(date); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", '"' + date.toString() + '"', value); + } + + @Test + public void testSerializationWithTypeInfo01() throws Exception + { + LocalDate date = LocalDate.of(2005, Month.NOVEMBER, 5); + + this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); + this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); + String value = this.mapper.writeValueAsString(date); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", + "[\"" + LocalDate.class.getName() + "\",\"" + date.toString() + "\"]", value); + } + + @Test + public void testDeserializationAsTimestamp01() throws Exception + { + LocalDate date = LocalDate.of(1986, Month.JANUARY, 17); + + LocalDate value = this.mapper.readValue("[1986,1,17]", LocalDate.class); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", date, value); + } + + @Test + public void testDeserializationAsTimestamp02() throws Exception + { + LocalDate date = LocalDate.of(2013, Month.AUGUST, 21); + + LocalDate value = this.mapper.readValue("[2013,8,21]", LocalDate.class); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", date, value); + } + + @Test + public void testDeserializationAsString01() throws Exception + { + LocalDate date = LocalDate.of(1986, Month.JANUARY, 17); + + LocalDate value = this.mapper.readValue('"' + date.toString() + '"', LocalDate.class); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", date, value); + } + + @Test + public void testDeserializationAsString02() throws Exception + { + LocalDate date = LocalDate.of(2013, Month.AUGUST, 21); + + LocalDate value = this.mapper.readValue('"' + date.toString() + '"', LocalDate.class); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", date, value); + } + + @Test + public void testDeserializationWithTypeInfo01() throws Exception + { + LocalDate date = LocalDate.of(2005, Month.NOVEMBER, 5); + + this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); + Temporal value = this.mapper.readValue( + "[\"" + LocalDate.class.getName() + "\",\"" + date.toString() + "\"]", Temporal.class + ); + + assertNotNull("The value should not be null.", value); + assertTrue("The value should be a LocalDate.", value instanceof LocalDate); + assertEquals("The value is not correct.", date, value); + } +} diff --git a/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestLocalDateTimeKeySerialization.java b/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestLocalDateTimeKeySerialization.java new file mode 100644 index 00000000..3490b422 --- /dev/null +++ b/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestLocalDateTimeKeySerialization.java @@ -0,0 +1,84 @@ +package com.fasterxml.jackson.datatype.jsr310.old; + +import static org.junit.Assert.assertEquals; + +import java.time.LocalDateTime; +import java.time.ZoneOffset; +import java.util.HashMap; +import java.util.Map; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.datatype.jsr310.JSR310Module; +import org.junit.Before; +import org.junit.Test; + +public class TestLocalDateTimeKeySerialization { + + private static final TypeReference> TYPE_REF = new TypeReference>() { + }; + private static final LocalDateTime DATE_TIME_0 = LocalDateTime.ofEpochSecond(0, 0, ZoneOffset.UTC); + /* + * Current serializer is LocalDateTime.toString(), which omits seconds if it can + */ + private static final String DATE_TIME_0_STRING = "1970-01-01T00:00"; + private static final LocalDateTime DATE_TIME = LocalDateTime.of(2015, 3, 14, 9, 26, 53, 590 * 1000 * 1000); + private static final String DATE_TIME_STRING = "2015-03-14T09:26:53.590"; + + private ObjectMapper om; + private Map map; + + @Before + public void setUp() { + this.om = new ObjectMapper(); + om.registerModule(new JSR310Module()); + map = new HashMap<>(); + } + + /* + * ObjectMapper configuration is not respected at deserialization and serialization at the moment. + */ + + @Test + public void testSerialization0() throws Exception { + map.put(DATE_TIME_0, "test"); + + String value = om.writeValueAsString(map); + + assertEquals("Value is incorrect", map(DATE_TIME_0_STRING, "test"), value); + } + + @Test + public void testSerialization1() throws Exception { + map.put(DATE_TIME, "test"); + + String value = om.writeValueAsString(map); + + assertEquals("Value is incorrect", map(DATE_TIME_STRING, "test"), value); + } + + @Test + public void testDeserialization0() throws Exception { + Map value = om.readValue( + map(DATE_TIME_0_STRING, "test"), + TYPE_REF); + + map.put(DATE_TIME_0, "test"); + assertEquals("Value is incorrect", map, value); + } + + @Test + public void testDeserialization1() throws Exception { + Map value = om.readValue( + map(DATE_TIME_STRING, "test"), + TYPE_REF); + + map.put(DATE_TIME, "test"); + assertEquals("Value is incorrect", map, value); + } + + private String map(String key, String value) { + return String.format("{\"%s\":\"%s\"}", key, value); + } + +} diff --git a/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestLocalDateTimeSerialization.java b/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestLocalDateTimeSerialization.java new file mode 100644 index 00000000..d0a28032 --- /dev/null +++ b/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestLocalDateTimeSerialization.java @@ -0,0 +1,359 @@ +/* + * Copyright 2013 FasterXML.com + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. You may obtain + * a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the license for the specific language governing permissions and + * limitations under the license. + */ + +package com.fasterxml.jackson.datatype.jsr310.old; + +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.ObjectReader; +import com.fasterxml.jackson.databind.SerializationFeature; + +import org.junit.Before; +import org.junit.Test; + +import java.time.LocalDateTime; +import java.time.Month; +import java.time.temporal.Temporal; + +import static org.junit.Assert.*; + +public class TestLocalDateTimeSerialization + extends ModuleTestBase +{ + private ObjectMapper mapper; + + @Before + public void setUp() + { + mapper = newMapper(); + mapper.enable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); + } + + @Test + public void testSerializationAsTimestamp01() throws Exception + { + LocalDateTime time = LocalDateTime.of(1986, Month.JANUARY, 17, 15, 43); + + String value = this.mapper.writeValueAsString(time); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", "[1986,1,17,15,43]", value); + } + + @Test + public void testSerializationAsTimestamp02() throws Exception + { + LocalDateTime time = LocalDateTime.of(2013, Month.AUGUST, 21, 9, 22, 57); + String value = this.mapper.writeValueAsString(time); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", "[2013,8,21,9,22,57]", value); + } + + @Test + public void testSerializationAsTimestamp03Nanosecond() throws Exception + { + LocalDateTime time = LocalDateTime.of(2013, Month.AUGUST, 21, 9, 22, 0, 57); + + this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, true); + String value = this.mapper.writeValueAsString(time); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", "[2013,8,21,9,22,0,57]", value); + } + + @Test + public void testSerializationAsTimestamp03Millisecond() throws Exception + { + LocalDateTime time = LocalDateTime.of(2013, Month.AUGUST, 21, 9, 22, 0, 57); + + this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false); + String value = this.mapper.writeValueAsString(time); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", "[2013,8,21,9,22,0,0]", value); + } + + @Test + public void testSerializationAsTimestamp04Nanosecond() throws Exception + { + LocalDateTime time = LocalDateTime.of(2005, Month.NOVEMBER, 5, 22, 31, 5, 829837); + + this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, true); + String value = this.mapper.writeValueAsString(time); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", "[2005,11,5,22,31,5,829837]", value); + } + + @Test + public void testSerializationAsTimestamp04Millisecond() throws Exception + { + LocalDateTime time = LocalDateTime.of(2005, Month.NOVEMBER, 5, 22, 31, 5, 422829837); + + this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false); + String value = this.mapper.writeValueAsString(time); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", "[2005,11,5,22,31,5,422]", value); + } + + @Test + public void testSerializationAsString01() throws Exception + { + LocalDateTime time = LocalDateTime.of(1986, Month.JANUARY, 17, 15, 43); + final ObjectMapper m = newMapper(); + + m.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); + String value = m.writeValueAsString(time); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", '"' + time.toString() + '"', value); + } + + @Test + public void testSerializationAsString02() throws Exception + { + LocalDateTime time = LocalDateTime.of(2013, Month.AUGUST, 21, 9, 22, 57); + + final ObjectMapper m = newMapper(); + m.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); + String value = m.writeValueAsString(time); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", '"' + time.toString() + '"', value); + } + + @Test + public void testSerializationAsString03() throws Exception + { + LocalDateTime time = LocalDateTime.of(2005, Month.NOVEMBER, 5, 22, 31, 5, 829837); + + final ObjectMapper m = newMapper(); + m.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); + String value = m.writeValueAsString(time); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", '"' + time.toString() + '"', value); + } + + @Test + public void testSerializationWithTypeInfo01() throws Exception + { + LocalDateTime time = LocalDateTime.of(2005, Month.NOVEMBER, 5, 22, 31, 5, 829837); + + final ObjectMapper m = newMapper(); + m.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true); + m.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, true); + m.addMixIn(Temporal.class, MockObjectConfiguration.class); + String value = m.writeValueAsString(time); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", + "[\"" + LocalDateTime.class.getName() + "\",[2005,11,5,22,31,5,829837]]", value); + } + + @Test + public void testSerializationWithTypeInfo02() throws Exception + { + final ObjectMapper m = newMapper(); + + m.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true); + m.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false); + m.addMixIn(Temporal.class, MockObjectConfiguration.class); + LocalDateTime time = LocalDateTime.of(2005, Month.NOVEMBER, 5, 22, 31, 5, 422829837); + String value = m.writeValueAsString(time); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", + "[\"" + LocalDateTime.class.getName() + "\",[2005,11,5,22,31,5,422]]", value); + } + + @Test + public void testSerializationWithTypeInfo03() throws Exception + { + final ObjectMapper m = newMapper(); + m.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); + m.addMixIn(Temporal.class, MockObjectConfiguration.class); + LocalDateTime time = LocalDateTime.of(2005, Month.NOVEMBER, 5, 22, 31, 5, 829837); + String value = m.writeValueAsString(time); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", + "[\"" + LocalDateTime.class.getName() + "\",\"" + time.toString() + "\"]", value); + } + + @Test + public void testDeserializationAsTimestamp01() throws Exception + { + LocalDateTime value = this.mapper.readValue("[1986,1,17,15,43]", LocalDateTime.class); + + assertNotNull("The value should not be null.", value); + LocalDateTime time = LocalDateTime.of(1986, Month.JANUARY, 17, 15, 43); + assertEquals("The value is not correct.", time, value); + } + + @Test + public void testDeserializationAsTimestamp02() throws Exception + { + LocalDateTime value = this.mapper.readValue("[2013,8,21,9,22,57]", LocalDateTime.class); + + assertNotNull("The value should not be null.", value); + LocalDateTime time = LocalDateTime.of(2013, Month.AUGUST, 21, 9, 22, 57); + assertEquals("The value is not correct.", time, value); + } + + @Test + public void testDeserializationAsTimestamp03Nanoseconds() throws Exception + { + ObjectReader r = mapper.readerFor(LocalDateTime.class) + .with(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS); + LocalDateTime value = r.readValue("[2013,8,21,9,22,0,57]"); + + assertNotNull("The value should not be null.", value); + LocalDateTime time = LocalDateTime.of(2013, Month.AUGUST, 21, 9, 22, 0, 57); + assertEquals("The value is not correct.", time, value); + } + + @Test + public void testDeserializationAsTimestamp03Milliseconds() throws Exception + { + ObjectReader r = mapper.readerFor(LocalDateTime.class) + .without(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS); + LocalDateTime value = r.readValue("[2013,8,21,9,22,0,57]"); + + assertNotNull("The value should not be null.", value); + LocalDateTime time = LocalDateTime.of(2013, Month.AUGUST, 21, 9, 22, 0, 57000000); + assertEquals("The value is not correct.", time, value); + } + + @Test + public void testDeserializationAsTimestamp04Nanoseconds() throws Exception + { + ObjectReader r = mapper.readerFor(LocalDateTime.class) + .with(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS); + LocalDateTime value = r.readValue("[2005,11,5,22,31,5,829837]"); + + assertNotNull("The value should not be null.", value); + LocalDateTime time = LocalDateTime.of(2005, Month.NOVEMBER, 5, 22, 31, 5, 829837); + assertEquals("The value is not correct.", time, value); + } + + @Test + public void testDeserializationAsTimestamp04Milliseconds01() throws Exception + { + ObjectReader r = mapper.readerFor(LocalDateTime.class) + .without(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS); + LocalDateTime value = r.readValue("[2005,11,5,22,31,5,829837]"); + + assertNotNull("The value should not be null.", value); + LocalDateTime time = LocalDateTime.of(2005, Month.NOVEMBER, 5, 22, 31, 5, 829837); + assertEquals("The value is not correct.", time, value); + } + + @Test + public void testDeserializationAsTimestamp04Milliseconds02() throws Exception + { + ObjectReader r = mapper.readerFor(LocalDateTime.class) + .without(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS); + LocalDateTime value = r.readValue("[2005,11,5,22,31,5,829]"); + + assertNotNull("The value should not be null.", value); + LocalDateTime time = LocalDateTime.of(2005, Month.NOVEMBER, 5, 22, 31, 5, 829000000); + assertEquals("The value is not correct.", time, value); + } + + @Test + public void testDeserializationAsString01() throws Exception + { + LocalDateTime time = LocalDateTime.of(1986, Month.JANUARY, 17, 15, 43); + + LocalDateTime value = this.mapper.readValue('"' + time.toString() + '"', LocalDateTime.class); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", time, value); + } + + @Test + public void testDeserializationAsString02() throws Exception + { + LocalDateTime time = LocalDateTime.of(2013, Month.AUGUST, 21, 9, 22, 57); + + LocalDateTime value = this.mapper.readValue('"' + time.toString() + '"', LocalDateTime.class); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", time, value); + } + + @Test + public void testDeserializationAsString03() throws Exception + { + LocalDateTime time = LocalDateTime.of(2005, Month.NOVEMBER, 5, 22, 31, 5, 829837); + + LocalDateTime value = this.mapper.readValue('"' + time.toString() + '"', LocalDateTime.class); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", time, value); + } + + @Test + public void testDeserializationWithTypeInfo01() throws Exception + { + LocalDateTime time = LocalDateTime.of(2005, Month.NOVEMBER, 5, 22, 31, 5, 829837); + + final ObjectMapper m = newMapper().addMixIn(Temporal.class, MockObjectConfiguration.class); + + m.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, true); + Temporal value = m.readValue( + "[\"" + LocalDateTime.class.getName() + "\",[2005,11,5,22,31,5,829837]]", Temporal.class + ); + + assertNotNull("The value should not be null.", value); + assertTrue("The value should be a LocalDateTime.", value instanceof LocalDateTime); + assertEquals("The value is not correct.", time, value); + } + + @Test + public void testDeserializationWithTypeInfo02() throws Exception + { + LocalDateTime time = LocalDateTime.of(2005, Month.NOVEMBER, 5, 22, 31, 5, 422000000); + + final ObjectMapper m = newMapper().addMixIn(Temporal.class, MockObjectConfiguration.class); + m.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false); + Temporal value = m.readValue( + "[\"" + LocalDateTime.class.getName() + "\",[2005,11,5,22,31,5,422]]", Temporal.class + ); + + assertNotNull("The value should not be null.", value); + assertTrue("The value should be a LocalDateTime.", value instanceof LocalDateTime); + assertEquals("The value is not correct.", time, value); + } + + @Test + public void testDeserializationWithTypeInfo03() throws Exception + { + LocalDateTime time = LocalDateTime.of(2005, Month.NOVEMBER, 5, 22, 31, 5, 829837); + + final ObjectMapper m = newMapper().addMixIn(Temporal.class, MockObjectConfiguration.class); + Temporal value = m.readValue( + "[\"" + LocalDateTime.class.getName() + "\",\"" + time.toString() + "\"]", Temporal.class + ); + + assertNotNull("The value should not be null.", value); + assertTrue("The value should be a LocalDateTime.", value instanceof LocalDateTime); + assertEquals("The value is not correct.", time, value); + } +} diff --git a/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestLocalTimeKeySerialization.java b/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestLocalTimeKeySerialization.java new file mode 100644 index 00000000..a0e552f2 --- /dev/null +++ b/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestLocalTimeKeySerialization.java @@ -0,0 +1,78 @@ +package com.fasterxml.jackson.datatype.jsr310.old; + +import java.time.LocalTime; +import java.util.HashMap; +import java.util.Map; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.datatype.jsr310.JSR310Module; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class TestLocalTimeKeySerialization { + + private static final TypeReference> TYPE_REF = new TypeReference>() { + }; + private static final LocalTime TIME_0 = LocalTime.ofSecondOfDay(0); + /* + * Seconds are omitted if possible + */ + private static final String TIME_0_STRING = "00:00"; + private static final LocalTime TIME = LocalTime.of(3, 14, 15, 920 * 1000 * 1000); + private static final String TIME_STRING = "03:14:15.920"; + + private ObjectMapper om; + private Map map; + + @Before + public void setUp() { + this.om = new ObjectMapper(); + om.registerModule(new JSR310Module()); + map = new HashMap<>(); + } + + /* + * ObjectMapper configuration is not respected at deserialization and serialization at the moment. + */ + + @Test + public void testSerialization0() throws Exception { + map.put(TIME_0, "test"); + + String value = om.writeValueAsString(map); + + Assert.assertEquals("Value is incorrect", map(TIME_0_STRING, "test"), value); + } + + @Test + public void testSerialization1() throws Exception { + map.put(TIME, "test"); + + String value = om.writeValueAsString(map); + + Assert.assertEquals("Value is incorrect", map(TIME_STRING, "test"), value); + } + + @Test + public void testDeserialization0() throws Exception { + Map value = om.readValue(map(TIME_0_STRING, "test"), TYPE_REF); + + map.put(TIME_0, "test"); + Assert.assertEquals("Value is incorrect", map, value); + } + + @Test + public void testDeserialization1() throws Exception { + Map value = om.readValue(map(TIME_STRING, "test"), TYPE_REF); + + map.put(TIME, "test"); + Assert.assertEquals("Value is incorrect", map, value); + } + + private String map(String key, String value) { + return String.format("{\"%s\":\"%s\"}", key, value); + } + +} diff --git a/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestLocalTimeSerialization.java b/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestLocalTimeSerialization.java new file mode 100644 index 00000000..65d54112 --- /dev/null +++ b/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestLocalTimeSerialization.java @@ -0,0 +1,366 @@ +/* + * Copyright 2013 FasterXML.com + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. You may obtain + * a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the license for the specific language governing permissions and + * limitations under the license. + */ + +package com.fasterxml.jackson.datatype.jsr310.old; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.time.LocalTime; +import java.time.temporal.Temporal; + +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; +import com.fasterxml.jackson.datatype.jsr310.JSR310Module; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class TestLocalTimeSerialization +{ + private ObjectMapper mapper; + + @Before + public void setUp() + { + this.mapper = new ObjectMapper(); + this.mapper.registerModule(new JSR310Module()); + } + + @After + public void tearDown() + { + + } + + @Test + public void testSerializationAsTimestamp01() throws Exception + { + LocalTime time = LocalTime.of(15, 43); + + this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true); + String value = this.mapper.writeValueAsString(time); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", "[15,43]", value); + } + + @Test + public void testSerializationAsTimestamp02() throws Exception + { + LocalTime time = LocalTime.of(9, 22, 57); + + this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true); + String value = this.mapper.writeValueAsString(time); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", "[9,22,57]", value); + } + + @Test + public void testSerializationAsTimestamp03Nanoseconds() throws Exception + { + LocalTime time = LocalTime.of(9, 22, 0, 57); + + this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true); + this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, true); + String value = this.mapper.writeValueAsString(time); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", "[9,22,0,57]", value); + } + + @Test + public void testSerializationAsTimestamp03Milliseconds() throws Exception + { + LocalTime time = LocalTime.of(9, 22, 0, 57); + + this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true); + this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false); + String value = this.mapper.writeValueAsString(time); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", "[9,22,0,0]", value); + } + + @Test + public void testSerializationAsTimestamp04Nanoseconds() throws Exception + { + LocalTime time = LocalTime.of(22, 31, 5, 829837); + + this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true); + this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, true); + String value = this.mapper.writeValueAsString(time); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", "[22,31,5,829837]", value); + } + + @Test + public void testSerializationAsTimestamp04Milliseconds() throws Exception + { + LocalTime time = LocalTime.of(22, 31, 5, 422829837); + + this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true); + this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false); + String value = this.mapper.writeValueAsString(time); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", "[22,31,5,422]", value); + } + + @Test + public void testSerializationAsString01() throws Exception + { + LocalTime time = LocalTime.of(15, 43); + + this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); + String value = this.mapper.writeValueAsString(time); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", '"' + time.toString() + '"', value); + } + + @Test + public void testSerializationAsString02() throws Exception + { + LocalTime time = LocalTime.of(9, 22, 57); + + this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); + String value = this.mapper.writeValueAsString(time); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", '"' + time.toString() + '"', value); + } + + @Test + public void testSerializationAsString03() throws Exception + { + LocalTime time = LocalTime.of(22, 31, 5, 829837); + + this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); + String value = this.mapper.writeValueAsString(time); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", '"' + time.toString() + '"', value); + } + + @Test + public void testSerializationWithTypeInfo01() throws Exception + { + LocalTime time = LocalTime.of(22, 31, 5, 829837); + + this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true); + this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, true); + this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); + String value = this.mapper.writeValueAsString(time); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", "[\"" + LocalTime.class.getName() + "\",[22,31,5,829837]]", value); + } + + @Test + public void testSerializationWithTypeInfo02() throws Exception + { + LocalTime time = LocalTime.of(22, 31, 5, 422829837); + + this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true); + this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false); + this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); + String value = this.mapper.writeValueAsString(time); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", "[\"" + LocalTime.class.getName() + "\",[22,31,5,422]]", value); + } + + @Test + public void testSerializationWithTypeInfo03() throws Exception + { + LocalTime time = LocalTime.of(22, 31, 5, 829837); + + this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); + this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); + String value = this.mapper.writeValueAsString(time); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", + "[\"" + LocalTime.class.getName() + "\",\"" + time.toString() + "\"]", value); + } + + @Test + public void testDeserializationAsTimestamp01() throws Exception + { + LocalTime time = LocalTime.of(15, 43); + + LocalTime value = this.mapper.readValue("[15,43]", LocalTime.class); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", time, value); + } + + @Test + public void testDeserializationAsTimestamp02() throws Exception + { + LocalTime time = LocalTime.of(9, 22, 57); + + LocalTime value = this.mapper.readValue("[9,22,57]", LocalTime.class); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", time, value); + } + + @Test + public void testDeserializationAsTimestamp03Nanoseconds() throws Exception + { + LocalTime time = LocalTime.of(9, 22, 0, 57); + + this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, true); + LocalTime value = this.mapper.readValue("[9,22,0,57]", LocalTime.class); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", time, value); + } + + @Test + public void testDeserializationAsTimestamp03Milliseconds() throws Exception + { + LocalTime time = LocalTime.of(9, 22, 0, 57000000); + + this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false); + LocalTime value = this.mapper.readValue("[9,22,0,57]", LocalTime.class); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", time, value); + } + + @Test + public void testDeserializationAsTimestamp04Nanoseconds() throws Exception + { + LocalTime time = LocalTime.of(22, 31, 5, 829837); + + this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, true); + LocalTime value = this.mapper.readValue("[22,31,5,829837]", LocalTime.class); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", time, value); + } + + @Test + public void testDeserializationAsTimestamp04Milliseconds01() throws Exception + { + LocalTime time = LocalTime.of(22, 31, 5, 829837); + + this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false); + LocalTime value = this.mapper.readValue("[22,31,5,829837]", LocalTime.class); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", time, value); + } + + @Test + public void testDeserializationAsTimestamp04Milliseconds02() throws Exception + { + LocalTime time = LocalTime.of(22, 31, 5, 829000000); + + this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false); + LocalTime value = this.mapper.readValue("[22,31,5,829]", LocalTime.class); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", time, value); + } + + @Test + public void testDeserializationAsString01() throws Exception + { + LocalTime time = LocalTime.of(15, 43); + + LocalTime value = this.mapper.readValue('"' + time.toString() + '"', LocalTime.class); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", time, value); + } + + @Test + public void testDeserializationAsString02() throws Exception + { + LocalTime time = LocalTime.of(9, 22, 57); + + LocalTime value = this.mapper.readValue('"' + time.toString() + '"', LocalTime.class); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", time, value); + } + + @Test + public void testDeserializationAsString03() throws Exception + { + LocalTime time = LocalTime.of(22, 31, 5, 829837); + + LocalTime value = this.mapper.readValue('"' + time.toString() + '"', LocalTime.class); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", time, value); + } + + @Test + public void testDeserializationWithTypeInfo01() throws Exception + { + LocalTime time = LocalTime.of(22, 31, 5, 829837); + + this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, true); + this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); + Temporal value = this.mapper.readValue( + "[\"" + LocalTime.class.getName() + "\",[22,31,5,829837]]", Temporal.class + ); + + assertNotNull("The value should not be null.", value); + assertTrue("The value should be a LocalTime.", value instanceof LocalTime); + assertEquals("The value is not correct.", time, value); + } + + @Test + public void testDeserializationWithTypeInfo02() throws Exception + { + LocalTime time = LocalTime.of(22, 31, 5, 422000000); + + this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false); + this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); + Temporal value = this.mapper.readValue( + "[\"" + LocalTime.class.getName() + "\",[22,31,5,422]]", Temporal.class + ); + + assertNotNull("The value should not be null.", value); + assertTrue("The value should be a LocalTime.", value instanceof LocalTime); + assertEquals("The value is not correct.", time, value); + } + + @Test + public void testDeserializationWithTypeInfo03() throws Exception + { + LocalTime time = LocalTime.of(22, 31, 5, 829837); + + this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); + Temporal value = this.mapper.readValue( + "[\"" + LocalTime.class.getName() + "\",\"" + time.toString() + "\"]", Temporal.class + ); + + assertNotNull("The value should not be null.", value); + assertTrue("The value should be a LocalTime.", value instanceof LocalTime); + assertEquals("The value is not correct.", time, value); + } +} diff --git a/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestMonthDayKeySerialization.java b/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestMonthDayKeySerialization.java new file mode 100644 index 00000000..61ad91b7 --- /dev/null +++ b/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestMonthDayKeySerialization.java @@ -0,0 +1,56 @@ +package com.fasterxml.jackson.datatype.jsr310.old; + +import java.time.MonthDay; +import java.util.HashMap; +import java.util.Map; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.datatype.jsr310.JSR310Module; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class TestMonthDayKeySerialization { + + private static final TypeReference> TYPE_REF = new TypeReference>() { + }; + private static final MonthDay MONTH_DAY = MonthDay.of(3, 14); + private static final String MONTH_DAY_STRING = "--03-14"; + + private ObjectMapper om; + private Map map; + + @Before + public void setUp() { + this.om = new ObjectMapper(); + om.registerModule(new JSR310Module()); + map = new HashMap<>(); + } + + /* + * ObjectMapper configuration is not respected at deserialization and serialization at the moment. + */ + + @Test + public void testSerialization() throws Exception { + map.put(MONTH_DAY, "test"); + + String value = om.writeValueAsString(map); + + Assert.assertEquals("Value is incorrect", map(MONTH_DAY_STRING, "test"), value); + } + + @Test + public void testDeserialization() throws Exception { + Map value = om.readValue(map(MONTH_DAY_STRING, "test"), TYPE_REF); + + map.put(MONTH_DAY, "test"); + Assert.assertEquals("Value is incorrect", map, value); + } + + private String map(String key, String value) { + return String.format("{\"%s\":\"%s\"}", key, value); + } + +} diff --git a/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestMonthDaySerialization.java b/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestMonthDaySerialization.java new file mode 100644 index 00000000..ddf7e406 --- /dev/null +++ b/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestMonthDaySerialization.java @@ -0,0 +1,118 @@ +/* + * Copyright 2013 FasterXML.com + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. You may obtain + * a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the license for the specific language governing permissions and + * limitations under the license. + */ + +package com.fasterxml.jackson.datatype.jsr310.old; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.time.Month; +import java.time.MonthDay; +import java.time.temporal.TemporalAccessor; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.datatype.jsr310.JSR310Module; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class TestMonthDaySerialization +{ + private ObjectMapper mapper; + + @Before + public void setUp() + { + this.mapper = new ObjectMapper(); + this.mapper.registerModule(new JSR310Module()); + } + + @After + public void tearDown() + { + + } + + @Test + public void testSerialization01() throws Exception + { + MonthDay monthDay = MonthDay.of(Month.JANUARY, 17); + + String value = this.mapper.writeValueAsString(monthDay); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", "\"--01-17\"", value); + } + + @Test + public void testSerialization02() throws Exception + { + MonthDay monthDay = MonthDay.of(Month.AUGUST, 21); + + String value = this.mapper.writeValueAsString(monthDay); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", "\"--08-21\"", value); + } + + @Test + public void testSerializationWithTypeInfo01() throws Exception + { + MonthDay monthDay = MonthDay.of(Month.NOVEMBER, 5); + + this.mapper.addMixIn(TemporalAccessor.class, MockObjectConfiguration.class); + String value = this.mapper.writeValueAsString(monthDay); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", "[\"" + MonthDay.class.getName() + "\",\"--11-05\"]", value); + } + + @Test + public void testDeserialization01() throws Exception + { + MonthDay monthDay = MonthDay.of(Month.JANUARY, 17); + + MonthDay value = this.mapper.readValue("\"--01-17\"", MonthDay.class); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", monthDay, value); + } + + @Test + public void testDeserialization02() throws Exception + { + MonthDay monthDay = MonthDay.of(Month.AUGUST, 21); + + MonthDay value = this.mapper.readValue("\"--08-21\"", MonthDay.class); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", monthDay, value); + } + + @Test + public void testDeserializationWithTypeInfo01() throws Exception + { + MonthDay monthDay = MonthDay.of(Month.NOVEMBER, 5); + + this.mapper.addMixIn(TemporalAccessor.class, MockObjectConfiguration.class); + TemporalAccessor value = this.mapper.readValue("[\"" + MonthDay.class.getName() + "\",\"--11-05\"]", TemporalAccessor.class); + + assertNotNull("The value should not be null.", value); + assertTrue("The value should be a MonthDay.", value instanceof MonthDay); + assertEquals("The value is not correct.", monthDay, value); + } +} diff --git a/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestNullKeySerialization.java b/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestNullKeySerialization.java new file mode 100644 index 00000000..64759186 --- /dev/null +++ b/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestNullKeySerialization.java @@ -0,0 +1,51 @@ +package com.fasterxml.jackson.datatype.jsr310.old; + +import java.time.LocalDate; +import java.util.HashMap; +import java.util.Map; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.datatype.jsr310.JSR310Module; +import com.fasterxml.jackson.datatype.jsr310.ser.key.Jsr310NullKeySerializer; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class TestNullKeySerialization { + + private static final TypeReference> TYPE_REF = new TypeReference>() { + }; + + private ObjectMapper om; + private Map map; + + @Before + public void setUp() { + this.om = new ObjectMapper(); + om.registerModule(new JSR310Module()); + map = new HashMap<>(); + } + + @Test + public void testSerialization() throws Exception { + om.getSerializerProvider().setNullKeySerializer(new Jsr310NullKeySerializer()); + + map.put(null, "test"); + String value = om.writeValueAsString(map); + + Assert.assertEquals(map(Jsr310NullKeySerializer.NULL_KEY, "test"), value); + } + + @Test + public void testDeserialization() throws Exception { + Map value = om.readValue(map(Jsr310NullKeySerializer.NULL_KEY, "test"), TYPE_REF); + + map.put(null, "test"); + Assert.assertEquals(map, value); + } + + private String map(String key, String value) { + return String.format("{\"%s\":\"%s\"}", key, value); + } +} diff --git a/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestOffsetDateTimeKeySerialization.java b/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestOffsetDateTimeKeySerialization.java new file mode 100644 index 00000000..75d569cc --- /dev/null +++ b/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestOffsetDateTimeKeySerialization.java @@ -0,0 +1,96 @@ +package com.fasterxml.jackson.datatype.jsr310.old; + +import java.time.Instant; +import java.time.OffsetDateTime; +import java.time.ZoneOffset; +import java.util.HashMap; +import java.util.Map; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.datatype.jsr310.JSR310Module; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class TestOffsetDateTimeKeySerialization { + + private static final TypeReference> TYPE_REF = new TypeReference>() { + }; + private static final OffsetDateTime DATE_TIME_0 = OffsetDateTime.ofInstant(Instant.ofEpochSecond(0), ZoneOffset.UTC); + private static final String DATE_TIME_0_STRING = "1970-01-01T00:00Z"; + private static final OffsetDateTime DATE_TIME_1 = OffsetDateTime.of(2015, 3, 14, 9, 26, 53, 590 * 1000 * 1000, ZoneOffset.UTC); + private static final String DATE_TIME_1_STRING = "2015-03-14T09:26:53.590Z"; + private static final OffsetDateTime DATE_TIME_2 = OffsetDateTime.of(2015, 3, 14, 9, 26, 53, 590 * 1000 * 1000, ZoneOffset.ofHours(6)); + private static final String DATE_TIME_2_STRING = "2015-03-14T09:26:53.590+06:00"; + + private ObjectMapper om; + private Map map; + + @Before + public void setUp() { + this.om = new ObjectMapper(); + om.registerModule(new JSR310Module()); + map = new HashMap<>(); + } + + /* + * ObjectMapper configuration is not respected at deserialization and serialization at the moment. + */ + + @Test + public void testSerialization0() throws Exception { + map.put(DATE_TIME_0, "test"); + + String value = om.writeValueAsString(map); + + Assert.assertEquals("Value is incorrect", map(DATE_TIME_0_STRING, "test"), value); + } + + @Test + public void testSerialization1() throws Exception { + map.put(DATE_TIME_1, "test"); + + String value = om.writeValueAsString(map); + + Assert.assertEquals("Value is incorrect", map(DATE_TIME_1_STRING, "test"), value); + } + + @Test + public void testSerialization2() throws Exception { + map.put(DATE_TIME_2, "test"); + + String value = om.writeValueAsString(map); + + Assert.assertEquals("Value is incorrect", map(DATE_TIME_2_STRING, "test"), value); + } + + @Test + public void testDeserialization0() throws Exception { + Map value = om.readValue(map(DATE_TIME_0_STRING, "test"), TYPE_REF); + + map.put(DATE_TIME_0, "test"); + Assert.assertEquals("Value is incorrect", map, value); + } + + @Test + public void testDeserialization1() throws Exception { + Map value = om.readValue(map(DATE_TIME_1_STRING, "test"), TYPE_REF); + + map.put(DATE_TIME_1, "test"); + Assert.assertEquals("Value is incorrect", map, value); + } + + @Test + public void testDeserialization2() throws Exception { + Map value = om.readValue(map(DATE_TIME_2_STRING, "test"), TYPE_REF); + + map.put(DATE_TIME_2, "test"); + Assert.assertEquals("Value is incorrect", map, value); + } + + private String map(String key, String value) { + return String.format("{\"%s\":\"%s\"}", key, value); + } + +} diff --git a/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestOffsetDateTimeSerialization.java b/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestOffsetDateTimeSerialization.java new file mode 100644 index 00000000..7b5c47c1 --- /dev/null +++ b/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestOffsetDateTimeSerialization.java @@ -0,0 +1,767 @@ +/* + * Copyright 2013 FasterXML.com + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. You may obtain + * a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the license for the specific language governing permissions and + * limitations under the license. + */ + +package com.fasterxml.jackson.datatype.jsr310.old; + +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.OffsetDateTime; +import java.time.ZoneId; +import java.time.ZoneOffset; +import java.time.format.DateTimeFormatter; +import java.time.temporal.ChronoField; +import java.time.temporal.ChronoUnit; +import java.time.temporal.Temporal; +import java.util.TimeZone; + +import com.fasterxml.jackson.databind.DeserializationFeature; +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.JSR310Module; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class TestOffsetDateTimeSerialization +{ + private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ISO_OFFSET_DATE_TIME; + + private static final ZoneId Z1 = ZoneId.of("America/Chicago"); + + private static final ZoneId Z2 = ZoneId.of("America/Anchorage"); + + private static final ZoneId Z3 = ZoneId.of("America/Los_Angeles"); + + private ObjectMapper mapper; + + @Before + public void setUp() + { + this.mapper = new ObjectMapper(); + this.mapper.registerModule(new JSR310Module()); + } + + @After + public void tearDown() + { + + } + + @Test + public void testSerializationAsTimestamp01Nanoseconds() throws Exception + { + OffsetDateTime date = OffsetDateTime.ofInstant(Instant.ofEpochSecond(0L), Z1); + + this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true); + this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, true); + String value = this.mapper.writeValueAsString(date); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", "0.000000000", value); + } + + @Test + public void testSerializationAsTimestamp01Milliseconds() throws Exception + { + OffsetDateTime date = OffsetDateTime.ofInstant(Instant.ofEpochSecond(0L), Z1); + + this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true); + this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false); + String value = this.mapper.writeValueAsString(date); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", "0", value); + } + + @Test + public void testSerializationAsTimestamp02Nanoseconds() throws Exception + { + OffsetDateTime date = OffsetDateTime.ofInstant(Instant.ofEpochSecond(123456789L, 183917322), Z2); + + this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true); + this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, true); + String value = this.mapper.writeValueAsString(date); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", "123456789.183917322", value); + } + + @Test + public void testSerializationAsTimestamp02Milliseconds() throws Exception + { + OffsetDateTime date = OffsetDateTime.ofInstant(Instant.ofEpochSecond(123456789L, 183917322), Z2); + + this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true); + this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false); + String value = this.mapper.writeValueAsString(date); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", "123456789183", value); + } + + @Test + public void testSerializationAsTimestamp03Nanoseconds() throws Exception + { + OffsetDateTime date = OffsetDateTime.now(Z3); + + this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true); + this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, true); + String value = this.mapper.writeValueAsString(date); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", DecimalUtils.toDecimal(date.toEpochSecond(), date.getNano()), value); + } + + @Test + public void testSerializationAsTimestamp03Milliseconds() throws Exception + { + OffsetDateTime date = OffsetDateTime.now(Z3); + + this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true); + this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false); + String value = this.mapper.writeValueAsString(date); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", Long.toString(date.toInstant().toEpochMilli()), value); + } + + @Test + public void testSerializationAsString01() throws Exception + { + OffsetDateTime date = OffsetDateTime.ofInstant(Instant.ofEpochSecond(0L), Z1); + + this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); + String value = this.mapper.writeValueAsString(date); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", '"' + FORMATTER.format(date) + '"', value); + } + + @Test + public void testSerializationAsString02() throws Exception + { + OffsetDateTime date = OffsetDateTime.ofInstant(Instant.ofEpochSecond(123456789L, 183917322), Z2); + + this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); + String value = this.mapper.writeValueAsString(date); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", '"' + FORMATTER.format(date) + '"', value); + } + + @Test + public void testSerializationAsString03() throws Exception + { + OffsetDateTime date = OffsetDateTime.now(Z3); + + this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); + String value = this.mapper.writeValueAsString(date); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", '"' + FORMATTER.format(date) + '"', value); + } + + @Test + public void testSerializationWithTypeInfo01() throws Exception + { + OffsetDateTime date = OffsetDateTime.ofInstant(Instant.ofEpochSecond(123456789L, 183917322), Z2); + + this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true); + this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, true); + this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); + String value = this.mapper.writeValueAsString(date); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", + "[\"" + OffsetDateTime.class.getName() + "\",123456789.183917322]", value); + } + + @Test + public void testSerializationWithTypeInfo02() throws Exception + { + OffsetDateTime date = OffsetDateTime.ofInstant(Instant.ofEpochSecond(123456789L, 183917322), Z2); + + this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true); + this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false); + this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); + String value = this.mapper.writeValueAsString(date); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", + "[\"" + OffsetDateTime.class.getName() + "\",123456789183]", value); + } + + @Test + public void testSerializationWithTypeInfo03() throws Exception + { + OffsetDateTime date = OffsetDateTime.now(Z3); + + this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); + this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); + String value = this.mapper.writeValueAsString(date); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", + "[\"" + OffsetDateTime.class.getName() + "\",\"" + FORMATTER.format(date) + "\"]", value); + } + + @Test + public void testDeserializationAsFloat01WithoutTimeZone() throws Exception + { + OffsetDateTime date = OffsetDateTime.ofInstant(Instant.ofEpochSecond(0L), Z1); + + OffsetDateTime value = this.mapper.readValue("0.000000000", OffsetDateTime.class); + + assertNotNull("The value should not be null.", value); + assertIsEqual(date, value); + assertEquals("The time zone is not correct.", ZoneOffset.UTC, value.getOffset()); + } + + @Test + public void testDeserializationAsFloat01WithTimeZone() throws Exception + { + OffsetDateTime date = OffsetDateTime.ofInstant(Instant.ofEpochSecond(0L), Z1); + + this.mapper.setTimeZone(TimeZone.getDefault()); + OffsetDateTime value = this.mapper.readValue("0.000000000", OffsetDateTime.class); + + assertNotNull("The value should not be null.", value); + assertIsEqual(date, value); + assertEquals("The time zone is not correct.", getDefaultOffset(date), value.getOffset()); + } + + @Test + public void testDeserializationAsFloat02WithoutTimeZone() throws Exception + { + OffsetDateTime date = OffsetDateTime.ofInstant(Instant.ofEpochSecond(123456789L, 183917322), Z2); + + OffsetDateTime value = this.mapper.readValue("123456789.183917322", OffsetDateTime.class); + + assertNotNull("The value should not be null.", value); + assertIsEqual(date, value); + assertEquals("The time zone is not correct.", ZoneOffset.UTC, value.getOffset()); + } + + @Test + public void testDeserializationAsFloat02WithTimeZone() throws Exception + { + OffsetDateTime date = OffsetDateTime.ofInstant(Instant.ofEpochSecond(123456789L, 183917322), Z2); + + this.mapper.setTimeZone(TimeZone.getDefault()); + OffsetDateTime value = this.mapper.readValue("123456789.183917322", OffsetDateTime.class); + + assertNotNull("The value should not be null.", value); + assertIsEqual(date, value); + assertEquals("The time zone is not correct.", getDefaultOffset(date), value.getOffset()); + } + + @Test + public void testDeserializationAsFloat03WithoutTimeZone() throws Exception + { + OffsetDateTime date = OffsetDateTime.now(Z3); + + OffsetDateTime value = this.mapper.readValue( + DecimalUtils.toDecimal(date.toEpochSecond(), date.getNano()), OffsetDateTime.class + ); + + assertNotNull("The value should not be null.", value); + assertIsEqual(date, value); + assertEquals("The time zone is not correct.", ZoneOffset.UTC, value.getOffset()); + } + + @Test + public void testDeserializationAsFloat03WithTimeZone() throws Exception + { + OffsetDateTime date = OffsetDateTime.now(Z3); + + this.mapper.setTimeZone(TimeZone.getDefault()); + OffsetDateTime value = this.mapper.readValue( + DecimalUtils.toDecimal(date.toEpochSecond(), date.getNano()), OffsetDateTime.class + ); + + assertNotNull("The value should not be null.", value); + assertIsEqual(date, value); + assertEquals("The time zone is not correct.", getDefaultOffset(date), value.getOffset()); + } + + @Test + public void testDeserializationAsInt01NanosecondsWithoutTimeZone() throws Exception + { + OffsetDateTime date = OffsetDateTime.ofInstant(Instant.ofEpochSecond(0L), Z1); + + this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, true); + OffsetDateTime value = this.mapper.readValue("0", OffsetDateTime.class); + + assertNotNull("The value should not be null.", value); + assertIsEqual(date, value); + assertEquals("The time zone is not correct.", ZoneOffset.UTC, value.getOffset()); + } + + @Test + public void testDeserializationAsInt01NanosecondsWithTimeZone() throws Exception + { + OffsetDateTime date = OffsetDateTime.ofInstant(Instant.ofEpochSecond(0L), Z1); + + this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, true); + this.mapper.setTimeZone(TimeZone.getDefault()); + OffsetDateTime value = this.mapper.readValue("0", OffsetDateTime.class); + + assertNotNull("The value should not be null.", value); + assertIsEqual(date, value); + assertEquals("The time zone is not correct.", getDefaultOffset(date), value.getOffset()); + } + + @Test + public void testDeserializationAsInt01MillisecondsWithoutTimeZone() throws Exception + { + OffsetDateTime date = OffsetDateTime.ofInstant(Instant.ofEpochSecond(0L), Z1); + + this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false); + OffsetDateTime value = this.mapper.readValue("0", OffsetDateTime.class); + + assertNotNull("The value should not be null.", value); + assertIsEqual(date, value); + assertEquals("The time zone is not correct.", ZoneOffset.UTC, value.getOffset()); + } + + @Test + public void testDeserializationAsInt01MillisecondsWithTimeZone() throws Exception + { + OffsetDateTime date = OffsetDateTime.ofInstant(Instant.ofEpochSecond(0L), Z1); + + this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false); + this.mapper.setTimeZone(TimeZone.getDefault()); + OffsetDateTime value = this.mapper.readValue("0", OffsetDateTime.class); + + assertNotNull("The value should not be null.", value); + assertIsEqual(date, value); + assertEquals("The time zone is not correct.", getDefaultOffset(date), value.getOffset()); + } + + @Test + public void testDeserializationAsInt02NanosecondsWithoutTimeZone() throws Exception + { + OffsetDateTime date = OffsetDateTime.ofInstant(Instant.ofEpochSecond(123456789L, 0), Z2); + + this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, true); + OffsetDateTime value = this.mapper.readValue("123456789", OffsetDateTime.class); + + assertNotNull("The value should not be null.", value); + assertIsEqual(date, value); + assertEquals("The time zone is not correct.", ZoneOffset.UTC, value.getOffset()); + } + + @Test + public void testDeserializationAsInt02NanosecondsWithTimeZone() throws Exception + { + OffsetDateTime date = OffsetDateTime.ofInstant(Instant.ofEpochSecond(123456789L, 0), Z2); + + this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, true); + this.mapper.setTimeZone(TimeZone.getDefault()); + OffsetDateTime value = this.mapper.readValue("123456789", OffsetDateTime.class); + + assertNotNull("The value should not be null.", value); + assertIsEqual(date, value); + assertEquals("The time zone is not correct.", getDefaultOffset(date), value.getOffset()); + } + + @Test + public void testDeserializationAsInt02MillisecondsWithoutTimeZone() throws Exception + { + OffsetDateTime date = OffsetDateTime.ofInstant(Instant.ofEpochSecond(123456789L, 422000000), Z2); + + this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false); + OffsetDateTime value = this.mapper.readValue("123456789422", OffsetDateTime.class); + + assertNotNull("The value should not be null.", value); + assertIsEqual(date, value); + assertEquals("The time zone is not correct.", ZoneOffset.UTC, value.getOffset()); + } + + @Test + public void testDeserializationAsInt02MillisecondsWithTimeZone() throws Exception + { + OffsetDateTime date = OffsetDateTime.ofInstant(Instant.ofEpochSecond(123456789L, 422000000), Z2); + + this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false); + this.mapper.setTimeZone(TimeZone.getDefault()); + OffsetDateTime value = this.mapper.readValue("123456789422", OffsetDateTime.class); + + assertNotNull("The value should not be null.", value); + assertIsEqual(date, value); + assertEquals("The time zone is not correct.", getDefaultOffset(date), value.getOffset()); + } + + @Test + public void testDeserializationAsInt03NanosecondsWithoutTimeZone() throws Exception + { + OffsetDateTime date = OffsetDateTime.now(Z3); + date = date.minus(date.getNano(), ChronoUnit.NANOS); + + this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, true); + OffsetDateTime value = this.mapper.readValue(Long.toString(date.toEpochSecond()), OffsetDateTime.class); + + assertNotNull("The value should not be null.", value); + assertIsEqual(date, value); + assertEquals("The time zone is not correct.", ZoneOffset.UTC, value.getOffset()); + } + + @Test + public void testDeserializationAsInt03NanosecondsWithTimeZone() throws Exception + { + OffsetDateTime date = OffsetDateTime.now(Z3); + date = date.minus(date.getNano(), ChronoUnit.NANOS); + + this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, true); + this.mapper.setTimeZone(TimeZone.getDefault()); + OffsetDateTime value = this.mapper.readValue(Long.toString(date.toEpochSecond()), OffsetDateTime.class); + + assertNotNull("The value should not be null.", value); + assertIsEqual(date, value); + assertEquals("The time zone is not correct.", getDefaultOffset(date), value.getOffset()); + } + + @Test + public void testDeserializationAsInt03MillisecondsWithoutTimeZone() throws Exception + { + OffsetDateTime date = OffsetDateTime.now(Z3); + date = date.minus(date.getNano() - (date.get(ChronoField.MILLI_OF_SECOND) * 1_000_000), ChronoUnit.NANOS); + + this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false); + OffsetDateTime value = + this.mapper.readValue(Long.toString(date.toInstant().toEpochMilli()), OffsetDateTime.class); + + assertNotNull("The value should not be null.", value); + assertIsEqual(date, value); + assertEquals("The time zone is not correct.", ZoneOffset.UTC, value.getOffset()); + } + + @Test + public void testDeserializationAsInt03MillisecondsWithTimeZone() throws Exception + { + OffsetDateTime date = OffsetDateTime.now(Z3); + date = date.minus(date.getNano() - (date.get(ChronoField.MILLI_OF_SECOND) * 1_000_000), ChronoUnit.NANOS); + + this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false); + this.mapper.setTimeZone(TimeZone.getDefault()); + OffsetDateTime value = + this.mapper.readValue(Long.toString(date.toInstant().toEpochMilli()), OffsetDateTime.class); + + assertNotNull("The value should not be null.", value); + assertIsEqual(date, value); + assertEquals("The time zone is not correct.", getDefaultOffset(date), value.getOffset()); + } + + @Test + public void testDeserializationAsString01WithoutTimeZone() throws Exception + { + OffsetDateTime date = OffsetDateTime.ofInstant(Instant.ofEpochSecond(0L), Z1); + + this.mapper.configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, true); + OffsetDateTime value = this.mapper.readValue('"' + FORMATTER.format(date) + '"', OffsetDateTime.class); + + assertNotNull("The value should not be null.", value); + assertIsEqual(date, value); + assertEquals("The time zone is not correct.", ZoneOffset.UTC, value.getOffset()); + } + + @Test + public void testDeserializationAsString01WithTimeZone() throws Exception + { + OffsetDateTime date = OffsetDateTime.ofInstant(Instant.ofEpochSecond(0L), Z1); + + this.mapper.configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, true); + this.mapper.setTimeZone(TimeZone.getDefault()); + OffsetDateTime value = this.mapper.readValue('"' + FORMATTER.format(date) + '"', OffsetDateTime.class); + + assertNotNull("The value should not be null.", value); + assertIsEqual(date, value); + assertEquals("The time zone is not correct.", getDefaultOffset(date), value.getOffset()); + } + + @Test + public void testDeserializationAsString01WithTimeZoneTurnedOff() throws Exception + { + OffsetDateTime date = OffsetDateTime.ofInstant(Instant.ofEpochSecond(0L), Z1); + + this.mapper.configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, false); + this.mapper.setTimeZone(TimeZone.getDefault()); + OffsetDateTime value = this.mapper.readValue('"' + FORMATTER.format(date) + '"', OffsetDateTime.class); + + assertNotNull("The value should not be null.", value); + assertIsEqual(date, value); + assertEquals("The time zone is not correct.", getOffset(value, Z1), value.getOffset()); + } + + @Test + public void testDeserializationAsString02WithoutTimeZone() throws Exception + { + OffsetDateTime date = OffsetDateTime.ofInstant(Instant.ofEpochSecond(123456789L, 183917322), Z2); + + this.mapper.configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, true); + OffsetDateTime value = this.mapper.readValue('"' + FORMATTER.format(date) + '"', OffsetDateTime.class); + + assertNotNull("The value should not be null.", value); + assertIsEqual(date, value); + assertEquals("The time zone is not correct.", ZoneOffset.UTC, value.getOffset()); + } + + @Test + public void testDeserializationAsString02WithTimeZone() throws Exception + { + OffsetDateTime date = OffsetDateTime.ofInstant(Instant.ofEpochSecond(123456789L, 183917322), Z2); + + this.mapper.configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, true); + this.mapper.setTimeZone(TimeZone.getDefault()); + OffsetDateTime value = this.mapper.readValue('"' + FORMATTER.format(date) + '"', OffsetDateTime.class); + + assertNotNull("The value should not be null.", value); + assertIsEqual(date, value); + assertEquals("The time zone is not correct.", getDefaultOffset(date), value.getOffset()); + } + + @Test + public void testDeserializationAsString02WithTimeZoneTurnedOff() throws Exception + { + OffsetDateTime date = OffsetDateTime.ofInstant(Instant.ofEpochSecond(123456789L, 183917322), Z2); + + this.mapper.configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, false); + this.mapper.setTimeZone(TimeZone.getDefault()); + OffsetDateTime value = this.mapper.readValue('"' + FORMATTER.format(date) + '"', OffsetDateTime.class); + + assertNotNull("The value should not be null.", value); + assertIsEqual(date, value); + assertEquals("The time zone is not correct.", getOffset(value, Z2), value.getOffset()); + } + + @Test + public void testDeserializationAsString03WithoutTimeZone() throws Exception + { + OffsetDateTime date = OffsetDateTime.now(Z3); + + this.mapper.configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, true); + OffsetDateTime value = this.mapper.readValue('"' + FORMATTER.format(date) + '"', OffsetDateTime.class); + + assertNotNull("The value should not be null.", value); + assertIsEqual(date, value); + assertEquals("The time zone is not correct.", ZoneOffset.UTC, value.getOffset()); + } + + @Test + public void testDeserializationAsString03WithTimeZone() throws Exception + { + OffsetDateTime date = OffsetDateTime.now(Z3); + + this.mapper.configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, true); + this.mapper.setTimeZone(TimeZone.getDefault()); + OffsetDateTime value = this.mapper.readValue('"' + FORMATTER.format(date) + '"', OffsetDateTime.class); + + assertNotNull("The value should not be null.", value); + assertIsEqual(date, value); + assertEquals("The time zone is not correct.", getDefaultOffset(date), value.getOffset()); + } + + @Test + public void testDeserializationAsString03WithTimeZoneTurnedOff() throws Exception + { + OffsetDateTime date = OffsetDateTime.now(Z3); + + this.mapper.configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, false); + this.mapper.setTimeZone(TimeZone.getDefault()); + OffsetDateTime value = this.mapper.readValue('"' + FORMATTER.format(date) + '"', OffsetDateTime.class); + + assertNotNull("The value should not be null.", value); + assertIsEqual(date, value); + assertEquals("The time zone is not correct.", getOffset(value, Z3), value.getOffset()); + } + + @Test + public void testDeserializationWithTypeInfo01WithoutTimeZone() throws Exception + { + OffsetDateTime date = OffsetDateTime.ofInstant(Instant.ofEpochSecond(123456789L, 183917322), Z2); + + this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); + Temporal value = this.mapper.readValue( + "[\"" + OffsetDateTime.class.getName() + "\",123456789.183917322]", Temporal.class + ); + + assertNotNull("The value should not be null.", value); + assertTrue("The value should be an OffsetDateTime.", value instanceof OffsetDateTime); + assertIsEqual(date, (OffsetDateTime) value); + assertEquals("The time zone is not correct.", ZoneOffset.UTC, ((OffsetDateTime) value).getOffset()); + } + + @Test + public void testDeserializationWithTypeInfo01WithTimeZone() throws Exception + { + OffsetDateTime date = OffsetDateTime.ofInstant(Instant.ofEpochSecond(123456789L, 183917322), Z2); + + this.mapper.setTimeZone(TimeZone.getDefault()); + this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); + Temporal value = this.mapper.readValue( + "[\"" + OffsetDateTime.class.getName() + "\",123456789.183917322]", Temporal.class + ); + + assertNotNull("The value should not be null.", value); + assertTrue("The value should be an OffsetDateTime.", value instanceof OffsetDateTime); + assertIsEqual(date, (OffsetDateTime) value); + assertEquals("The time zone is not correct.", getDefaultOffset(date), ((OffsetDateTime) value).getOffset()); + } + + @Test + public void testDeserializationWithTypeInfo02WithoutTimeZone() throws Exception + { + OffsetDateTime date = OffsetDateTime.ofInstant(Instant.ofEpochSecond(123456789L, 0), Z2); + + this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, true); + this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); + Temporal value = this.mapper.readValue( + "[\"" + OffsetDateTime.class.getName() + "\",123456789]", Temporal.class + ); + + assertNotNull("The value should not be null.", value); + assertTrue("The value should be an OffsetDateTime.", value instanceof OffsetDateTime); + assertIsEqual(date, (OffsetDateTime) value); + assertEquals("The time zone is not correct.", ZoneOffset.UTC, ((OffsetDateTime) value).getOffset()); + } + + @Test + public void testDeserializationWithTypeInfo02WithTimeZone() throws Exception + { + OffsetDateTime date = OffsetDateTime.ofInstant(Instant.ofEpochSecond(123456789L, 0), Z2); + + this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, true); + this.mapper.setTimeZone(TimeZone.getDefault()); + this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); + Temporal value = this.mapper.readValue( + "[\"" + OffsetDateTime.class.getName() + "\",123456789]", Temporal.class + ); + + assertNotNull("The value should not be null.", value); + assertTrue("The value should be an OffsetDateTime.", value instanceof OffsetDateTime); + assertIsEqual(date, (OffsetDateTime) value); + assertEquals("The time zone is not correct.", getDefaultOffset(date), ((OffsetDateTime) value).getOffset()); + } + + @Test + public void testDeserializationWithTypeInfo03WithoutTimeZone() throws Exception + { + OffsetDateTime date = OffsetDateTime.ofInstant(Instant.ofEpochSecond(123456789L, 422000000), Z2); + + this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false); + this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); + Temporal value = this.mapper.readValue( + "[\"" + OffsetDateTime.class.getName() + "\",123456789422]", Temporal.class + ); + + assertNotNull("The value should not be null.", value); + assertTrue("The value should be an OffsetDateTime.", value instanceof OffsetDateTime); + assertIsEqual(date, (OffsetDateTime) value); + assertEquals("The time zone is not correct.", ZoneOffset.UTC, ((OffsetDateTime) value).getOffset()); + } + + @Test + public void testDeserializationWithTypeInfo03WithTimeZone() throws Exception + { + OffsetDateTime date = OffsetDateTime.ofInstant(Instant.ofEpochSecond(123456789L, 422000000), Z2); + + this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false); + this.mapper.setTimeZone(TimeZone.getDefault()); + this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); + Temporal value = this.mapper.readValue( + "[\"" + OffsetDateTime.class.getName() + "\",123456789422]", Temporal.class + ); + + assertNotNull("The value should not be null.", value); + assertTrue("The value should be an OffsetDateTime.", value instanceof OffsetDateTime); + assertIsEqual(date, (OffsetDateTime) value); + assertEquals("The time zone is not correct.", getDefaultOffset(date), ((OffsetDateTime) value).getOffset()); + } + + @Test + public void testDeserializationWithTypeInfo04WithoutTimeZone() throws Exception + { + OffsetDateTime date = OffsetDateTime.now(Z3); + + this.mapper.configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, true); + this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); + Temporal value = this.mapper.readValue( + "[\"" + OffsetDateTime.class.getName() + "\",\"" + FORMATTER.format(date) + "\"]", Temporal.class + ); + + assertNotNull("The value should not be null.", value); + assertTrue("The value should be an OffsetDateTime.", value instanceof OffsetDateTime); + assertIsEqual(date, (OffsetDateTime) value); + assertEquals("The time zone is not correct.", ZoneOffset.UTC, ((OffsetDateTime) value).getOffset()); + } + + @Test + public void testDeserializationWithTypeInfo04WithTimeZone() throws Exception + { + OffsetDateTime date = OffsetDateTime.now(Z3); + + this.mapper.configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, true); + this.mapper.setTimeZone(TimeZone.getDefault()); + this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); + Temporal value = this.mapper.readValue( + "[\"" + OffsetDateTime.class.getName() + "\",\"" + FORMATTER.format(date) + "\"]", Temporal.class + ); + + assertNotNull("The value should not be null.", value); + assertTrue("The value should be an OffsetDateTime.", value instanceof OffsetDateTime); + assertIsEqual(date, (OffsetDateTime) value); + assertEquals("The time zone is not correct.", getDefaultOffset(date), ((OffsetDateTime) value).getOffset()); + } + + @Test + public void testDeserializationWithTypeInfo04WithTimeZoneTurnedOff() throws Exception + { + OffsetDateTime date = OffsetDateTime.now(Z3); + + this.mapper.configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, false); + this.mapper.setTimeZone(TimeZone.getDefault()); + this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); + Temporal value = this.mapper.readValue( + "[\"" + OffsetDateTime.class.getName() + "\",\"" + FORMATTER.format(date) + "\"]", Temporal.class + ); + + assertNotNull("The value should not be null.", value); + assertTrue("The value should be an OffsetDateTime.", value instanceof OffsetDateTime); + OffsetDateTime cast = (OffsetDateTime) value; + assertIsEqual(date, cast); + assertEquals("The time zone is not correct.", getOffset(cast, Z3), cast.getOffset()); + } + + private static void assertIsEqual(OffsetDateTime expected, OffsetDateTime actual) + { + assertTrue("The value is not correct. Expected timezone-adjusted <" + expected + ">, actual <" + actual + ">.", + expected.isEqual(actual)); + } + + private static ZoneOffset getDefaultOffset(OffsetDateTime date) + { + return ZoneId.systemDefault().getRules().getOffset(date.toLocalDateTime()); + } + + private static ZoneOffset getOffset(OffsetDateTime date, ZoneId zone) + { + return zone.getRules().getOffset(date.toLocalDateTime()); + } +} diff --git a/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestOffsetTimeKeySerialization.java b/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestOffsetTimeKeySerialization.java new file mode 100644 index 00000000..74c631ca --- /dev/null +++ b/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestOffsetTimeKeySerialization.java @@ -0,0 +1,95 @@ +package com.fasterxml.jackson.datatype.jsr310.old; + +import java.time.OffsetTime; +import java.time.ZoneOffset; +import java.util.HashMap; +import java.util.Map; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.datatype.jsr310.JSR310Module; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class TestOffsetTimeKeySerialization { + + private static final TypeReference> TYPE_REF = new TypeReference>() { + }; + private static final OffsetTime TIME_0 = OffsetTime.of(0, 0, 0, 0, ZoneOffset.UTC); + private static final String TIME_0_STRING = "00:00Z"; + private static final OffsetTime TIME_1 = OffsetTime.of(3, 14, 15, 920 * 1000 * 1000, ZoneOffset.UTC); + private static final String TIME_1_STRING = "03:14:15.920Z"; + private static final OffsetTime TIME_2 = OffsetTime.of(3, 14, 15, 920 * 1000 * 1000, ZoneOffset.ofHours(6)); + private static final String TIME_2_STRING = "03:14:15.920+06:00"; + + private ObjectMapper om; + private Map map; + + @Before + public void setUp() { + this.om = new ObjectMapper(); + om.registerModule(new JSR310Module()); + map = new HashMap<>(); + } + + /* + * ObjectMapper configuration is not respected at deserialization and serialization at the moment. + */ + + @Test + public void testSerialization0() throws Exception { + map.put(TIME_0, "test"); + + String value = om.writeValueAsString(map); + + Assert.assertEquals(map(TIME_0_STRING, "test"), value); + } + + @Test + public void testSerialization1() throws Exception { + map.put(TIME_1, "test"); + + String value = om.writeValueAsString(map); + + Assert.assertEquals("Value is incorrect", map(TIME_1_STRING, "test"), value); + } + + @Test + public void testSerialization2() throws Exception { + map.put(TIME_2, "test"); + + String value = om.writeValueAsString(map); + + Assert.assertEquals("Value is incorrect", map(TIME_2_STRING, "test"), value); + } + + @Test + public void testDeserialization0() throws Exception { + Map value = om.readValue(map(TIME_0_STRING, "test"), TYPE_REF); + + map.put(TIME_0, "test"); + Assert.assertEquals("Value is incorrect", map, value); + } + + @Test + public void testDeserialization1() throws Exception { + Map value = om.readValue(map(TIME_1_STRING, "test"), TYPE_REF); + + map.put(TIME_1, "test"); + Assert.assertEquals("Value is incorrect", map, value); + } + + @Test + public void testDeserialization2() throws Exception { + Map value = om.readValue(map(TIME_2_STRING, "test"), TYPE_REF); + + map.put(TIME_2, "test"); + Assert.assertEquals("Value is incorrect", map, value); + } + + private String map(String key, String value) { + return String.format("{\"%s\":\"%s\"}", key, value); + } + +} diff --git a/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestOffsetTimeSerialization.java b/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestOffsetTimeSerialization.java new file mode 100644 index 00000000..daa3fe8b --- /dev/null +++ b/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestOffsetTimeSerialization.java @@ -0,0 +1,369 @@ +/* + * Copyright 2013 FasterXML.com + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. You may obtain + * a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the license for the specific language governing permissions and + * limitations under the license. + */ + +package com.fasterxml.jackson.datatype.jsr310.old; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.time.OffsetTime; +import java.time.ZoneOffset; +import java.time.temporal.Temporal; + +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; +import com.fasterxml.jackson.datatype.jsr310.JSR310Module; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class TestOffsetTimeSerialization +{ + private ObjectMapper mapper; + + @Before + public void setUp() + { + this.mapper = new ObjectMapper(); + this.mapper.registerModule(new JSR310Module()); + } + + @After + public void tearDown() + { + + } + + @Test + public void testSerializationAsTimestamp01() throws Exception + { + OffsetTime time = OffsetTime.of(15, 43, 0, 0, ZoneOffset.of("+0300")); + + this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true); + String value = this.mapper.writeValueAsString(time); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", "[15,43,\"+03:00\"]", value); + } + + @Test + public void testSerializationAsTimestamp02() throws Exception + { + OffsetTime time = OffsetTime.of(9, 22, 57, 0, ZoneOffset.of("-0630")); + + this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true); + String value = this.mapper.writeValueAsString(time); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", "[9,22,57,\"-06:30\"]", value); + } + + @Test + public void testSerializationAsTimestamp03Nanoseconds() throws Exception + { + OffsetTime time = OffsetTime.of(9, 22, 0, 57, ZoneOffset.of("-0630")); + + this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true); + this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, true); + String value = this.mapper.writeValueAsString(time); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", "[9,22,0,57,\"-06:30\"]", value); + } + + @Test + public void testSerializationAsTimestamp03Milliseconds() throws Exception + { + OffsetTime time = OffsetTime.of(9, 22, 0, 57, ZoneOffset.of("-0630")); + + this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true); + this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false); + String value = this.mapper.writeValueAsString(time); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", "[9,22,0,0,\"-06:30\"]", value); + } + + @Test + public void testSerializationAsTimestamp04Nanoseconds() throws Exception + { + OffsetTime time = OffsetTime.of(22, 31, 5, 829837, ZoneOffset.of("+1100")); + + this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true); + this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, true); + String value = this.mapper.writeValueAsString(time); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", "[22,31,5,829837,\"+11:00\"]", value); + } + + @Test + public void testSerializationAsTimestamp04Milliseconds() throws Exception + { + OffsetTime time = OffsetTime.of(22, 31, 5, 422829837, ZoneOffset.of("+1100")); + + this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true); + this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false); + String value = this.mapper.writeValueAsString(time); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", "[22,31,5,422,\"+11:00\"]", value); + } + + @Test + public void testSerializationAsString01() throws Exception + { + OffsetTime time = OffsetTime.of(15, 43, 0, 0, ZoneOffset.of("+0300")); + + this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); + String value = this.mapper.writeValueAsString(time); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", '"' + time.toString() + '"', value); + } + + @Test + public void testSerializationAsString02() throws Exception + { + OffsetTime time = OffsetTime.of(9, 22, 57, 0, ZoneOffset.of("-0630")); + + this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); + String value = this.mapper.writeValueAsString(time); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", '"' + time.toString() + '"', value); + } + + @Test + public void testSerializationAsString03() throws Exception + { + OffsetTime time = OffsetTime.of(22, 31, 5, 829837, ZoneOffset.of("+1100")); + + this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); + String value = this.mapper.writeValueAsString(time); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", '"' + time.toString() + '"', value); + } + + @Test + public void testSerializationWithTypeInfo01() throws Exception + { + OffsetTime time = OffsetTime.of(22, 31, 5, 829837, ZoneOffset.of("+1100")); + + this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true); + this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, true); + this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); + String value = this.mapper.writeValueAsString(time); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", + "[\"" + OffsetTime.class.getName() + "\",[22,31,5,829837,\"+11:00\"]]", value); + } + + @Test + public void testSerializationWithTypeInfo02() throws Exception + { + OffsetTime time = OffsetTime.of(22, 31, 5, 422829837, ZoneOffset.of("+1100")); + + this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true); + this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false); + this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); + String value = this.mapper.writeValueAsString(time); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", + "[\"" + OffsetTime.class.getName() + "\",[22,31,5,422,\"+11:00\"]]", value); + } + + @Test + public void testSerializationWithTypeInfo03() throws Exception + { + OffsetTime time = OffsetTime.of(22, 31, 5, 829837, ZoneOffset.of("+1100")); + + this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); + this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); + String value = this.mapper.writeValueAsString(time); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", + "[\"" + OffsetTime.class.getName() + "\",\"" + time.toString() + "\"]", value); + } + + @Test + public void testDeserializationAsTimestamp01() throws Exception + { + OffsetTime time = OffsetTime.of(15, 43, 0, 0, ZoneOffset.of("+0300")); + + OffsetTime value = this.mapper.readValue("[15,43,\"+0300\"]", OffsetTime.class); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", time, value); + } + + @Test + public void testDeserializationAsTimestamp02() throws Exception + { + OffsetTime time = OffsetTime.of(9, 22, 57, 0, ZoneOffset.of("-0630")); + + OffsetTime value = this.mapper.readValue("[9,22,57,\"-06:30\"]", OffsetTime.class); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", time, value); + } + + @Test + public void testDeserializationAsTimestamp03Nanoseconds() throws Exception + { + OffsetTime time = OffsetTime.of(9, 22, 0, 57, ZoneOffset.of("-0630")); + + this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, true); + OffsetTime value = this.mapper.readValue("[9,22,0,57,\"-06:30\"]", OffsetTime.class); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", time, value); + } + + @Test + public void testDeserializationAsTimestamp03Milliseconds() throws Exception + { + OffsetTime time = OffsetTime.of(9, 22, 0, 57000000, ZoneOffset.of("-0630")); + + this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false); + OffsetTime value = this.mapper.readValue("[9,22,0,57,\"-06:30\"]", OffsetTime.class); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", time, value); + } + + @Test + public void testDeserializationAsTimestamp04Nanoseconds() throws Exception + { + OffsetTime time = OffsetTime.of(22, 31, 5, 829837, ZoneOffset.of("+1100")); + + this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, true); + OffsetTime value = this.mapper.readValue("[22,31,5,829837,\"+11:00\"]", OffsetTime.class); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", time, value); + } + + @Test + public void testDeserializationAsTimestamp04Milliseconds01() throws Exception + { + OffsetTime time = OffsetTime.of(22, 31, 5, 829837, ZoneOffset.of("+1100")); + + this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false); + OffsetTime value = this.mapper.readValue("[22,31,5,829837,\"+11:00\"]", OffsetTime.class); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", time, value); + } + + @Test + public void testDeserializationAsTimestamp04Milliseconds02() throws Exception + { + OffsetTime time = OffsetTime.of(22, 31, 5, 829000000, ZoneOffset.of("+1100")); + + this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false); + OffsetTime value = this.mapper.readValue("[22,31,5,829,\"+11:00\"]", OffsetTime.class); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", time, value); + } + + @Test + public void testDeserializationAsString01() throws Exception + { + OffsetTime time = OffsetTime.of(15, 43, 0, 0, ZoneOffset.of("+0300")); + + OffsetTime value = this.mapper.readValue('"' + time.toString() + '"', OffsetTime.class); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", time, value); + } + + @Test + public void testDeserializationAsString02() throws Exception + { + OffsetTime time = OffsetTime.of(9, 22, 57, 0, ZoneOffset.of("-0630")); + + OffsetTime value = this.mapper.readValue('"' + time.toString() + '"', OffsetTime.class); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", time, value); + } + + @Test + public void testDeserializationAsString03() throws Exception + { + OffsetTime time = OffsetTime.of(22, 31, 5, 829837, ZoneOffset.of("+1100")); + + OffsetTime value = this.mapper.readValue('"' + time.toString() + '"', OffsetTime.class); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", time, value); + } + + @Test + public void testDeserializationWithTypeInfo01() throws Exception + { + OffsetTime time = OffsetTime.of(22, 31, 5, 829837, ZoneOffset.of("+1100")); + + this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, true); + this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); + Temporal value = this.mapper.readValue( + "[\"" + OffsetTime.class.getName() + "\",[22,31,5,829837,\"+11:00\"]]", Temporal.class + ); + + assertNotNull("The value should not be null.", value); + assertTrue("The value should be a OffsetTime.", value instanceof OffsetTime); + assertEquals("The value is not correct.", time, value); + } + + @Test + public void testDeserializationWithTypeInfo02() throws Exception + { + OffsetTime time = OffsetTime.of(22, 31, 5, 422000000, ZoneOffset.of("+1100")); + + this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false); + this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); + Temporal value = this.mapper.readValue( + "[\"" + OffsetTime.class.getName() + "\",[22,31,5,422,\"+11:00\"]]", Temporal.class + ); + + assertNotNull("The value should not be null.", value); + assertTrue("The value should be a OffsetTime.", value instanceof OffsetTime); + assertEquals("The value is not correct.", time, value); + } + + @Test + public void testDeserializationWithTypeInfo03() throws Exception + { + OffsetTime time = OffsetTime.of(22, 31, 5, 829837, ZoneOffset.of("+1100")); + + this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); + Temporal value = this.mapper.readValue( + "[\"" + OffsetTime.class.getName() + "\",\"" + time.toString() + "\"]", Temporal.class + ); + + assertNotNull("The value should not be null.", value); + assertTrue("The value should be a OffsetTime.", value instanceof OffsetTime); + assertEquals("The value is not correct.", time, value); + } +} diff --git a/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestPeriodKeySerialization.java b/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestPeriodKeySerialization.java new file mode 100644 index 00000000..d263b802 --- /dev/null +++ b/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestPeriodKeySerialization.java @@ -0,0 +1,75 @@ +package com.fasterxml.jackson.datatype.jsr310.old; + +import java.time.Period; +import java.util.HashMap; +import java.util.Map; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.datatype.jsr310.JSR310Module; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class TestPeriodKeySerialization { + + private static final TypeReference> TYPE_REF = new TypeReference>() { + }; + private static final Period PERIOD_0 = Period.of(0, 0, 0); + private static final String PERIOD_0_STRING = "P0D"; + private static final Period PERIOD = Period.of(3, 1, 4); + private static final String PERIOD_STRING = "P3Y1M4D"; + + private ObjectMapper om; + private Map map; + + @Before + public void setUp() { + this.om = new ObjectMapper(); + om.registerModule(new JSR310Module()); + map = new HashMap<>(); + } + + /* + * ObjectMapper configuration is not respected at deserialization and serialization at the moment. + */ + + @Test + public void testSerialization0() throws Exception { + map.put(PERIOD_0, "test"); + + String value = om.writeValueAsString(map); + + Assert.assertEquals("Value is incorrect", map(PERIOD_0_STRING, "test"), value); + } + + @Test + public void testSerialization1() throws Exception { + map.put(PERIOD, "test"); + + String value = om.writeValueAsString(map); + + Assert.assertEquals("Value is incorrect", map(PERIOD_STRING, "test"), value); + } + + @Test + public void testDeserialization0() throws Exception { + Map value = om.readValue(map(PERIOD_0_STRING, "test"), TYPE_REF); + + map.put(PERIOD_0, "test"); + Assert.assertEquals("Value is incorrect", map, value); + } + + @Test + public void testDeserialization1() throws Exception { + Map value = om.readValue(map(PERIOD_STRING, "test"), TYPE_REF); + + map.put(PERIOD, "test"); + Assert.assertEquals("Value is incorrect", map, value); + } + + private String map(String key, String value) { + return String.format("{\"%s\":\"%s\"}", key, value); + } + +} diff --git a/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestPeriodSerialization.java b/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestPeriodSerialization.java new file mode 100644 index 00000000..44c2a591 --- /dev/null +++ b/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestPeriodSerialization.java @@ -0,0 +1,120 @@ +/* + * Copyright 2013 FasterXML.com + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. You may obtain + * a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the license for the specific language governing permissions and + * limitations under the license. + */ + +package com.fasterxml.jackson.datatype.jsr310.old; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.time.Period; +import java.time.temporal.TemporalAmount; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.datatype.jsr310.JSR310Module; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class TestPeriodSerialization +{ + private ObjectMapper mapper; + + @Before + public void setUp() + { + this.mapper = new ObjectMapper(); + this.mapper.registerModule(new JSR310Module()); + } + + @After + public void tearDown() + { + + } + + @Test + public void testSerialization01() throws Exception + { + Period period = Period.of(1, 6, 15); + + String value = this.mapper.writeValueAsString(period); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", '"' + period.toString() + '"', value); + } + + @Test + public void testSerialization02() throws Exception + { + Period period = Period.of(0, 0, 21); + + String value = this.mapper.writeValueAsString(period); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", '"' + period.toString() + '"', value); + } + + @Test + public void testSerializationWithTypeInfo01() throws Exception + { + Period period = Period.of(5, 1, 12); + + this.mapper.addMixIn(TemporalAmount.class, MockObjectConfiguration.class); + String value = this.mapper.writeValueAsString(period); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", + "[\"" + Period.class.getName() + "\",\"" + period.toString() + "\"]", value); + } + + @Test + public void testDeserialization01() throws Exception + { + Period period = Period.of(1, 6, 15); + + Period value = this.mapper.readValue('"' + period.toString() + '"', Period.class); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", period, value); + } + + @Test + public void testDeserialization02() throws Exception + { + Period period = Period.of(0, 0, 21); + + Period value = this.mapper.readValue('"' + period.toString() + '"', Period.class); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", period, value); + } + + @Test + public void testDeserializationWithTypeInfo01() throws Exception + { + Period period = Period.of(5, 1, 12); + + this.mapper.addMixIn(TemporalAmount.class, MockObjectConfiguration.class); + TemporalAmount value = this.mapper.readValue( + "[\"" + Period.class.getName() + "\",\"" + period.toString() + "\"]", TemporalAmount.class + ); + + assertNotNull("The value should not be null.", value); + assertTrue("The value should be a Period.", value instanceof Period); + assertEquals("The value is not correct.", period, value); + } +} diff --git a/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestYearKeySerialization.java b/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestYearKeySerialization.java new file mode 100644 index 00000000..8a652468 --- /dev/null +++ b/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestYearKeySerialization.java @@ -0,0 +1,53 @@ +package com.fasterxml.jackson.datatype.jsr310.old; + +import java.time.Year; +import java.util.HashMap; +import java.util.Map; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.datatype.jsr310.JSR310Module; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class TestYearKeySerialization { + + private static final TypeReference> TYPE_REF = new TypeReference>() { + }; + private ObjectMapper om; + private Map map; + + @Before + public void setUp() { + this.om = new ObjectMapper(); + om.registerModule(new JSR310Module()); + map = new HashMap<>(); + } + + /* + * ObjectMapper configuration is not respected at deserialization and serialization at the moment. + */ + + @Test + public void testSerialization() throws Exception { + map.put(Year.of(3141), "test"); + + String value = om.writeValueAsString(map); + + Assert.assertEquals("Value is incorrect", map("3141", "test"), value); + } + + @Test + public void testDeserialization() throws Exception { + Map value = om.readValue(map("3141", "test"), TYPE_REF); + + map.put(Year.of(3141), "test"); + Assert.assertEquals("Value is incorrect", map, value); + } + + private String map(String key, String value) { + return String.format("{\"%s\":\"%s\"}", key, value); + } + +} diff --git a/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestYearMonthKeySerialization.java b/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestYearMonthKeySerialization.java new file mode 100644 index 00000000..f17e254f --- /dev/null +++ b/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestYearMonthKeySerialization.java @@ -0,0 +1,54 @@ +package com.fasterxml.jackson.datatype.jsr310.old; + +import java.time.YearMonth; +import java.util.HashMap; +import java.util.Map; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.datatype.jsr310.JSR310Module; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class TestYearMonthKeySerialization { + + private static final TypeReference> TYPE_REF = new TypeReference>() { + }; + + private ObjectMapper om; + private Map map; + + @Before + public void setUp() { + this.om = new ObjectMapper(); + om.registerModule(new JSR310Module()); + map = new HashMap<>(); + } + + /* + * ObjectMapper configuration is not respected at deserialization and serialization at the moment. + */ + + @Test + public void testSerialization() throws Exception { + map.put(YearMonth.of(3141, 5), "test"); + + String value = om.writeValueAsString(map); + + Assert.assertEquals("Value is incorrect", map("3141-05", "test"), value); + } + + @Test + public void testDeserialization() throws Exception { + Map value = om.readValue(map("3141-05", "test"), TYPE_REF); + + map.put(YearMonth.of(3141, 5), "test"); + Assert.assertEquals("Value is incorrect", map, value); + } + + private String map(String key, String value) { + return String.format("{\"%s\":\"%s\"}", key, value); + } + +} diff --git a/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestYearMonthSerialization.java b/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestYearMonthSerialization.java new file mode 100644 index 00000000..e305fecc --- /dev/null +++ b/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestYearMonthSerialization.java @@ -0,0 +1,213 @@ +/* + * Copyright 2013 FasterXML.com + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. You may obtain + * a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the license for the specific language governing permissions and + * limitations under the license. + */ + +package com.fasterxml.jackson.datatype.jsr310.old; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.time.Month; +import java.time.YearMonth; +import java.time.temporal.Temporal; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonFormat; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; +import com.fasterxml.jackson.datatype.jsr310.JSR310Module; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class TestYearMonthSerialization +{ + private ObjectMapper mapper; + + @Before + public void setUp() + { + this.mapper = new ObjectMapper(); + this.mapper.registerModule(new JSR310Module()); + } + + @After + public void tearDown() + { + + } + + @Test + public void testSerializationAsTimestamp01() throws Exception + { + YearMonth yearMonth = YearMonth.of(1986, Month.JANUARY); + + this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true); + String value = this.mapper.writeValueAsString(yearMonth); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", "[1986,1]", value); + } + + @Test + public void testSerializationAsTmestamp02() throws Exception + { + YearMonth yearMonth = YearMonth.of(2013, Month.AUGUST); + + this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true); + String value = this.mapper.writeValueAsString(yearMonth); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", "[2013,8]", value); + } + + @Test + public void testSerializationAsString01() throws Exception + { + YearMonth yearMonth = YearMonth.of(1986, Month.JANUARY); + + this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); + String value = this.mapper.writeValueAsString(yearMonth); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", '"' + yearMonth.toString() + '"', value); + } + + @Test + public void testSerializationAsString02() throws Exception + { + YearMonth yearMonth = YearMonth.of(2013, Month.AUGUST); + + this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); + String value = this.mapper.writeValueAsString(yearMonth); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", '"' + yearMonth.toString() + '"', value); + } + + @Test + public void testSerializationWithTypeInfo01() throws Exception + { + YearMonth yearMonth = YearMonth.of(2005, Month.NOVEMBER); + + this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); + this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); + String value = this.mapper.writeValueAsString(yearMonth); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", + "[\"" + YearMonth.class.getName() + "\",\"" + yearMonth.toString() + "\"]", value); + } + + @Test + public void testDeserializationAsTimestamp01() throws Exception + { + YearMonth yearMonth = YearMonth.of(1986, Month.JANUARY); + + this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true); + YearMonth value = this.mapper.readValue("[1986,1]", YearMonth.class); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", yearMonth, value); + } + + @Test + public void testDeserializationAsTimestamp02() throws Exception + { + YearMonth yearMonth = YearMonth.of(2013, Month.AUGUST); + + this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true); + YearMonth value = this.mapper.readValue("[2013,8]", YearMonth.class); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", yearMonth, value); + } + + @Test + public void testDeserializationAsString01() throws Exception + { + YearMonth yearMonth = YearMonth.of(1986, Month.JANUARY); + + this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); + YearMonth value = this.mapper.readValue('"' + yearMonth.toString() + '"', YearMonth.class); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", yearMonth, value); + } + + @Test + public void testDeserializationAsString02() throws Exception + { + YearMonth yearMonth = YearMonth.of(2013, Month.AUGUST); + + this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); + YearMonth value = this.mapper.readValue('"' + yearMonth.toString() + '"', YearMonth.class); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", yearMonth, value); + } + + @Test + public void testDeserializationWithTypeInfo01() throws Exception + { + YearMonth yearMonth = YearMonth.of(2005, Month.NOVEMBER); + + this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); + Temporal value = this.mapper.readValue("[\"" + YearMonth.class.getName() + "\",\"" + yearMonth.toString() + "\"]", Temporal.class); + + assertNotNull("The value should not be null.", value); + assertTrue("The value should be a YearMonth.", value instanceof YearMonth); + assertEquals("The value is not correct.", yearMonth, value); + } + + private static class SimpleAggregate + { + @JsonProperty("yearMonth") + @JsonFormat(pattern = "yyMM") + final YearMonth yearMonth; + + @JsonCreator + SimpleAggregate(@JsonProperty("yearMonth") YearMonth yearMonth) + { + this.yearMonth = yearMonth; + } + } + + @Test + public void testSerializationWithPattern01() throws Exception + { + YearMonth yearMonth = YearMonth.of(2013, Month.AUGUST); + SimpleAggregate simpleAggregate = new SimpleAggregate(yearMonth); + + String value = this.mapper.writeValueAsString(simpleAggregate); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", "{\"yearMonth\":\"1308\"}", value); + } + + @Test + public void testDeserializationWithPattern01() throws Exception + { + YearMonth yearMonth = YearMonth.of(2013, Month.AUGUST); + SimpleAggregate simpleAggregate = new SimpleAggregate(yearMonth); + + SimpleAggregate value = this.mapper.readValue("{\"yearMonth\":\"1308\"}", SimpleAggregate.class); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", simpleAggregate.yearMonth, value.yearMonth); + } +} diff --git a/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestYearSerialization.java b/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestYearSerialization.java new file mode 100644 index 00000000..de497b8b --- /dev/null +++ b/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestYearSerialization.java @@ -0,0 +1,111 @@ +/* + * Copyright 2013 FasterXML.com + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. You may obtain + * a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the license for the specific language governing permissions and + * limitations under the license. + */ + +package com.fasterxml.jackson.datatype.jsr310.old; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.time.Year; +import java.time.temporal.Temporal; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.datatype.jsr310.JSR310Module; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class TestYearSerialization +{ + private ObjectMapper mapper; + + @Before + public void setUp() + { + this.mapper = new ObjectMapper(); + this.mapper.registerModule(new JSR310Module()); + } + + @After + public void tearDown() + { + + } + + @Test + public void testSerialization01() throws Exception + { + Year year = Year.of(1986); + + String value = this.mapper.writeValueAsString(year); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", "1986", value); + } + + @Test + public void testSerialization02() throws Exception + { + Year year = Year.of(2013); + + String value = this.mapper.writeValueAsString(year); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", "2013", value); + } + + @Test + public void testSerializationWithTypeInfo01() throws Exception + { + Year year = Year.of(2005); + + this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); + String value = this.mapper.writeValueAsString(year); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", "[\"" + Year.class.getName() + "\",2005]", value); + } + + @Test + public void testDeserialization01() throws Exception + { + Year value = this.mapper.readValue("1986", Year.class); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", Year.of(1986), value); + } + + @Test + public void testDeserialization02() throws Exception + { + Year value = this.mapper.readValue("2013", Year.class); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", Year.of(2013), value); + } + + @Test + public void testDeserializationWithTypeInfo01() throws Exception + { + this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); + Temporal value = this.mapper.readValue("[\"" + Year.class.getName() + "\",2005]", Temporal.class); + + assertNotNull("The value should not be null.", value); + assertTrue("The value should be a Year.", value instanceof Year); + assertEquals("The value is not correct.", Year.of(2005), value); + } +} diff --git a/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestZoneIdKeySerialization.java b/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestZoneIdKeySerialization.java new file mode 100644 index 00000000..fbc7bb7e --- /dev/null +++ b/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestZoneIdKeySerialization.java @@ -0,0 +1,94 @@ +package com.fasterxml.jackson.datatype.jsr310.old; + +import java.time.ZoneId; +import java.util.HashMap; +import java.util.Map; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.datatype.jsr310.JSR310Module; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class TestZoneIdKeySerialization { + + private static final TypeReference> TYPE_REF = new TypeReference>() { + }; + private static final ZoneId ZONE_0 = ZoneId.of("UTC"); + private static final String ZONE_0_STRING = "UTC"; + private static final ZoneId ZONE_1 = ZoneId.of("+06:00"); + private static final String ZONE_1_STRING = "+06:00"; + private static final ZoneId ZONE_2 = ZoneId.of("Europe/London"); + private static final String ZONE_2_STRING = "Europe/London"; + + private ObjectMapper om; + private Map map; + + @Before + public void setUp() { + this.om = new ObjectMapper(); + om.registerModule(new JSR310Module()); + map = new HashMap<>(); + } + + /* + * ObjectMapper configuration is not respected at deserialization and serialization at the moment. + */ + + @Test + public void testSerialization0() throws Exception { + map.put(ZONE_0, "test"); + + String value = om.writeValueAsString(map); + + Assert.assertEquals("Value is incorrect", map(ZONE_0_STRING, "test"), value); + } + + @Test + public void testSerialization1() throws Exception { + map.put(ZONE_1, "test"); + + String value = om.writeValueAsString(map); + + Assert.assertEquals("Value is incorrect", map(ZONE_1_STRING, "test"), value); + } + + @Test + public void testSerialization2() throws Exception { + map.put(ZONE_2, "test"); + + String value = om.writeValueAsString(map); + + Assert.assertEquals("Value is incorrect", map(ZONE_2_STRING, "test"), value); + } + + @Test + public void testDeserialization0() throws Exception { + Map value = om.readValue(map(ZONE_0_STRING, "test"), TYPE_REF); + + map.put(ZONE_0, "test"); + Assert.assertEquals("Value is incorrect", map, value); + } + + @Test + public void testDeserialization1() throws Exception { + Map value = om.readValue(map(ZONE_1_STRING, "test"), TYPE_REF); + + map.put(ZONE_1, "test"); + Assert.assertEquals("Value is incorrect", map, value); + } + + @Test + public void testDeserialization2() throws Exception { + Map value = om.readValue(map(ZONE_2_STRING, "test"), TYPE_REF); + + map.put(ZONE_2, "test"); + Assert.assertEquals("Value is incorrect", map, value); + } + + private String map(String key, String value) { + return String.format("{\"%s\":\"%s\"}", key, value); + } + +} diff --git a/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestZoneIdSerialization.java b/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestZoneIdSerialization.java new file mode 100644 index 00000000..14a0a275 --- /dev/null +++ b/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestZoneIdSerialization.java @@ -0,0 +1,108 @@ +/* + * Copyright 2013 FasterXML.com + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. You may obtain + * a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the license for the specific language governing permissions and + * limitations under the license. + */ + +package com.fasterxml.jackson.datatype.jsr310.old; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.time.ZoneId; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.datatype.jsr310.JSR310Module; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class TestZoneIdSerialization +{ + private ObjectMapper mapper; + + @Before + public void setUp() + { + this.mapper = new ObjectMapper(); + this.mapper.registerModule(new JSR310Module()); + } + + @After + public void tearDown() + { + + } + + @Test + public void testSerialization01() throws Exception + { + ZoneId id = ZoneId.of("America/Chicago"); + + String value = this.mapper.writeValueAsString(id); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", "\"America/Chicago\"", value); + } + + @Test + public void testSerialization02() throws Exception + { + ZoneId id = ZoneId.of("America/Anchorage"); + + String value = this.mapper.writeValueAsString(id); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", "\"America/Anchorage\"", value); + } + + @Test + public void testSerializationWithTypeInfo01() throws Exception + { + ZoneId id = ZoneId.of("America/Denver"); + + this.mapper.addMixIn(ZoneId.class, MockObjectConfiguration.class); + String value = this.mapper.writeValueAsString(id); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", "[\"java.time.ZoneRegion\",\"America/Denver\"]", value); + } + + @Test + public void testDeserialization01() throws Exception + { + ZoneId value = this.mapper.readValue("\"America/Chicago\"", ZoneId.class); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", ZoneId.of("America/Chicago"), value); + } + + @Test + public void testDeserialization02() throws Exception + { + ZoneId value = this.mapper.readValue("\"America/Anchorage\"", ZoneId.class); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", ZoneId.of("America/Anchorage"), value); + } + + @Test + public void testDeserializationWithTypeInfo02() throws Exception + { + this.mapper.addMixIn(ZoneId.class, MockObjectConfiguration.class); + ZoneId value = this.mapper.readValue("[\"" + ZoneId.class.getName() + "\",\"America/Denver\"]", ZoneId.class); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", ZoneId.of("America/Denver"), value); + } +} diff --git a/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestZoneOffsetKeySerialization.java b/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestZoneOffsetKeySerialization.java new file mode 100644 index 00000000..dff0096f --- /dev/null +++ b/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestZoneOffsetKeySerialization.java @@ -0,0 +1,75 @@ +package com.fasterxml.jackson.datatype.jsr310.old; + +import java.time.ZoneOffset; +import java.util.HashMap; +import java.util.Map; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.datatype.jsr310.JSR310Module; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class TestZoneOffsetKeySerialization { + + private static final TypeReference> TYPE_REF = new TypeReference>() { + }; + private static final ZoneOffset OFFSET_0 = ZoneOffset.UTC; + private static final String OFFSET_0_STRING = "Z"; + private static final ZoneOffset OFFSET_1 = ZoneOffset.ofHours(6); + private static final String OFFSET_1_STRING = "+06:00"; + + private ObjectMapper om; + private Map map; + + @Before + public void setUp() { + this.om = new ObjectMapper(); + om.registerModule(new JSR310Module()); + map = new HashMap<>(); + } + + /* + * ObjectMapper configuration is not respected at deserialization and serialization at the moment. + */ + + @Test + public void testSerialization0() throws Exception { + map.put(OFFSET_0, "test"); + + String value = om.writeValueAsString(map); + + Assert.assertEquals("Value is incorrect", map(OFFSET_0_STRING, "test"), value); + } + + @Test + public void testSerialization1() throws Exception { + map.put(OFFSET_1, "test"); + + String value = om.writeValueAsString(map); + + Assert.assertEquals("Value is incorrect", map(OFFSET_1_STRING, "test"), value); + } + + @Test + public void testDeserialization0() throws Exception { + Map value = om.readValue(map(OFFSET_0_STRING, "test"), TYPE_REF); + + map.put(OFFSET_0, "test"); + Assert.assertEquals("Value is incorrect", map, value); + } + + @Test + public void testDeserialization1() throws Exception { + Map value = om.readValue(map(OFFSET_1_STRING, "test"), TYPE_REF); + + map.put(OFFSET_1, "test"); + Assert.assertEquals("Value is incorrect", map, value); + } + + private String map(String key, String value) { + return String.format("{\"%s\":\"%s\"}", key, value); + } + +} diff --git a/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestZoneOffsetSerialization.java b/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestZoneOffsetSerialization.java new file mode 100644 index 00000000..a9358332 --- /dev/null +++ b/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestZoneOffsetSerialization.java @@ -0,0 +1,131 @@ +/* + * Copyright 2013 FasterXML.com + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. You may obtain + * a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the license for the specific language governing permissions and + * limitations under the license. + */ + +package com.fasterxml.jackson.datatype.jsr310.old; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.time.ZoneId; +import java.time.ZoneOffset; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.datatype.jsr310.JSR310Module; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class TestZoneOffsetSerialization +{ + private ObjectMapper mapper; + + @Before + public void setUp() + { + this.mapper = new ObjectMapper(); + this.mapper.registerModule(new JSR310Module()); + } + + @After + public void tearDown() + { + + } + + @Test + public void testSerialization01() throws Exception + { + ZoneOffset offset = ZoneOffset.of("Z"); + + String value = this.mapper.writeValueAsString(offset); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", "\"Z\"", value); + } + + @Test + public void testSerialization02() throws Exception + { + ZoneOffset offset = ZoneOffset.of("+0300"); + + String value = this.mapper.writeValueAsString(offset); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", "\"+03:00\"", value); + } + + @Test + public void testSerialization03() throws Exception + { + ZoneOffset offset = ZoneOffset.of("-0630"); + + String value = this.mapper.writeValueAsString(offset); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", "\"-06:30\"", value); + } + + @Test + public void testSerializationWithTypeInfo03() throws Exception + { + ZoneOffset offset = ZoneOffset.of("+0415"); + + this.mapper.addMixIn(ZoneId.class, MockObjectConfiguration.class); + String value = this.mapper.writeValueAsString(offset); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", "[\"" + ZoneOffset.class.getName() + "\",\"+04:15\"]", value); + } + + @Test + public void testDeserialization01() throws Exception + { + ZoneOffset value = this.mapper.readValue("\"Z\"", ZoneOffset.class); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", ZoneOffset.of("Z"), value); + } + + @Test + public void testDeserialization02() throws Exception + { + ZoneOffset value = this.mapper.readValue("\"+0300\"", ZoneOffset.class); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", ZoneOffset.of("+0300"), value); + } + + @Test + public void testDeserialization03() throws Exception + { + ZoneOffset value = this.mapper.readValue("\"-06:30\"", ZoneOffset.class); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", ZoneOffset.of("-0630"), value); + } + + @Test + public void testDeserializationWithTypeInfo03() throws Exception + { + this.mapper.addMixIn(ZoneId.class, MockObjectConfiguration.class); + ZoneId value = this.mapper.readValue("[\"" + ZoneOffset.class.getName() + "\",\"+0415\"]", ZoneId.class); + + assertNotNull("The value should not be null.", value); + assertTrue("The value should be a ZoneOffset.", value instanceof ZoneOffset); + assertEquals("The value is not correct.", ZoneOffset.of("+0415"), value); + } +} diff --git a/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestZonedDateTimeKeySerialization.java b/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestZonedDateTimeKeySerialization.java new file mode 100644 index 00000000..0875d0d9 --- /dev/null +++ b/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestZonedDateTimeKeySerialization.java @@ -0,0 +1,106 @@ +package com.fasterxml.jackson.datatype.jsr310.old; + +import java.time.Instant; +import java.time.ZoneId; +import java.time.ZoneOffset; +import java.time.ZonedDateTime; +import java.util.HashMap; +import java.util.Map; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.datatype.jsr310.JSR310Module; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class TestZonedDateTimeKeySerialization { + + private static final TypeReference> TYPE_REF = new TypeReference>() { + }; + private static final ZonedDateTime DATE_TIME_0 = ZonedDateTime.ofInstant(Instant.ofEpochSecond(0), ZoneOffset.UTC); + private static final String DATE_TIME_0_STRING = "1970-01-01T00:00:00Z"; + + private static final ZonedDateTime DATE_TIME_1 = ZonedDateTime.of( + 2015, 3, 14, 9, 26, 53, 590 * 1000 * 1000, ZoneOffset.UTC); + private static final String DATE_TIME_1_STRING = "2015-03-14T09:26:53.59Z"; + + private static final ZonedDateTime DATE_TIME_2 = ZonedDateTime.of( + 2015, 3, 14, 9, 26, 53, 590 * 1000 * 1000, ZoneId.of("Europe/Budapest")); + /** + * Value of {@link #DATE_TIME_2} after it's been serialized and read back. Serialization throws away time zone information, it only + * keeps offset data. + */ + private static final ZonedDateTime DATE_TIME_2_OFFSET = DATE_TIME_2.withZoneSameInstant(ZoneOffset.ofHours(1)); + private static final String DATE_TIME_2_STRING = "2015-03-14T09:26:53.59+01:00";; + + private ObjectMapper om; + private Map map; + + @Before + public void setUp() { + this.om = new ObjectMapper(); + om.registerModule(new JSR310Module()); + map = new HashMap<>(); + } + + /* + * ObjectMapper configuration is not respected at deserialization and serialization at the moment. + */ + + @Test + public void testSerialization0() throws Exception { + map.put(DATE_TIME_0, "test"); + + String value = om.writerFor(TYPE_REF).writeValueAsString(map); + + Assert.assertEquals("Value is incorrect", map(DATE_TIME_0_STRING, "test"), value); + } + + @Test + public void testSerialization1() throws Exception { + map.put(DATE_TIME_1, "test"); + + String value = om.writerFor(TYPE_REF).writeValueAsString(map); + + Assert.assertEquals("Value is incorrect", map(DATE_TIME_1_STRING, "test"), value); + } + + @Test + public void testSerialization2() throws Exception { + map.put(DATE_TIME_2, "test"); + + String value = om.writerFor(TYPE_REF).writeValueAsString(map); + + Assert.assertEquals("Value is incorrect", map(DATE_TIME_2_STRING, "test"), value); + } + + @Test + public void testDeserialization0() throws Exception { + Map value = om.readValue(map(DATE_TIME_0_STRING, "test"), TYPE_REF); + + map.put(DATE_TIME_0, "test"); + Assert.assertEquals("Value is incorrect", map, value); + } + + @Test + public void testDeserialization1() throws Exception { + Map value = om.readValue(map(DATE_TIME_1_STRING, "test"), TYPE_REF); + + map.put(DATE_TIME_1, "test"); + Assert.assertEquals("Value is incorrect", map, value); + } + + @Test + public void testDeserialization2() throws Exception { + Map value = om.readValue(map(DATE_TIME_2_STRING, "test"), TYPE_REF); + + map.put(DATE_TIME_2_OFFSET, "test"); + Assert.assertEquals("Value is incorrect", map, value); + } + + private String map(String key, String value) { + return String.format("{\"%s\":\"%s\"}", key, value); + } + +} diff --git a/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestZonedDateTimeSerialization.java b/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestZonedDateTimeSerialization.java new file mode 100644 index 00000000..92ef2284 --- /dev/null +++ b/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestZonedDateTimeSerialization.java @@ -0,0 +1,754 @@ +/* + * Copyright 2013 FasterXML.com + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. You may obtain + * a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the license for the specific language governing permissions and + * limitations under the license. + */ + +package com.fasterxml.jackson.datatype.jsr310.old; + +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; +import java.time.temporal.ChronoField; +import java.time.temporal.ChronoUnit; +import java.time.temporal.Temporal; +import java.util.TimeZone; + +import com.fasterxml.jackson.databind.DeserializationFeature; +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.JSR310Module; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class TestZonedDateTimeSerialization +{ + private static final ZoneId Z1 = ZoneId.of("America/Chicago"); + + private static final ZoneId Z2 = ZoneId.of("America/Anchorage"); + + private static final ZoneId Z3 = ZoneId.of("America/Los_Angeles"); + + private static final ZoneId GMT = ZoneId.of("GMT"); + + private ObjectMapper mapper; + + @Before + public void setUp() + { + this.mapper = new ObjectMapper(); + this.mapper.registerModule(new JSR310Module()); + } + + @After + public void tearDown() + { + + } + + @Test + public void testSerializationAsTimestamp01Nanoseconds() throws Exception + { + ZonedDateTime date = ZonedDateTime.ofInstant(Instant.ofEpochSecond(0L), Z1); + + this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true); + this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, true); + String value = this.mapper.writeValueAsString(date); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", "0.000000000", value); + } + + @Test + public void testSerializationAsTimestamp01Milliseconds() throws Exception + { + ZonedDateTime date = ZonedDateTime.ofInstant(Instant.ofEpochSecond(0L), Z1); + + this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true); + this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false); + String value = this.mapper.writeValueAsString(date); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", "0", value); + } + + @Test + public void testSerializationAsTimestamp02Nanoseconds() throws Exception + { + ZonedDateTime date = ZonedDateTime.ofInstant(Instant.ofEpochSecond(123456789L, 183917322), Z2); + + this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true); + this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, true); + String value = this.mapper.writeValueAsString(date); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", "123456789.183917322", value); + } + + @Test + public void testSerializationAsTimestamp02Milliseconds() throws Exception + { + ZonedDateTime date = ZonedDateTime.ofInstant(Instant.ofEpochSecond(123456789L, 183917322), Z2); + + this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true); + this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false); + String value = this.mapper.writeValueAsString(date); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", "123456789183", value); + } + + @Test + public void testSerializationAsTimestamp03Nanoseconds() throws Exception + { + ZonedDateTime date = ZonedDateTime.now(Z3); + + this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true); + this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, true); + String value = this.mapper.writeValueAsString(date); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", DecimalUtils.toDecimal(date.toEpochSecond(), date.getNano()), value); + } + + @Test + public void testSerializationAsTimestamp03Milliseconds() throws Exception + { + ZonedDateTime date = ZonedDateTime.now(Z3); + + this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true); + this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false); + String value = this.mapper.writeValueAsString(date); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", Long.toString(date.toInstant().toEpochMilli()), value); + } + + @Test + public void testSerializationAsString01() throws Exception + { + ZonedDateTime date = ZonedDateTime.ofInstant(Instant.ofEpochSecond(0L), Z1); + + this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); + String value = this.mapper.writeValueAsString(date); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", '"' + date.toString() + '"', value); + } + + @Test + public void testSerializationAsString02() throws Exception + { + ZonedDateTime date = ZonedDateTime.ofInstant(Instant.ofEpochSecond(123456789L, 183917322), Z2); + + this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); + String value = this.mapper.writeValueAsString(date); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", '"' + date.toString() + '"', value); + } + + @Test + public void testSerializationAsString03() throws Exception + { + ZonedDateTime date = ZonedDateTime.now(Z3); + + this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); + String value = this.mapper.writeValueAsString(date); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", '"' + date.toString() + '"', value); + } + + @Test + public void testSerializationWithTypeInfo01() throws Exception + { + ZonedDateTime date = ZonedDateTime.ofInstant(Instant.ofEpochSecond(123456789L, 183917322), Z2); + + this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true); + this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, true); + this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); + String value = this.mapper.writeValueAsString(date); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", + "[\"" + ZonedDateTime.class.getName() + "\",123456789.183917322]", value); + } + + @Test + public void testSerializationWithTypeInfo02() throws Exception + { + ZonedDateTime date = ZonedDateTime.ofInstant(Instant.ofEpochSecond(123456789L, 183917322), Z2); + + this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true); + this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false); + this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); + String value = this.mapper.writeValueAsString(date); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", + "[\"" + ZonedDateTime.class.getName() + "\",123456789183]", value); + } + + @Test + public void testSerializationWithTypeInfo03() throws Exception + { + ZonedDateTime date = ZonedDateTime.now(Z3); + + this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); + this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); + String value = this.mapper.writeValueAsString(date); + + assertNotNull("The value should not be null.", value); + assertEquals("The value is not correct.", + "[\"" + ZonedDateTime.class.getName() + "\",\"" + date.toString() + "\"]", value); + } + + @Test + public void testDeserializationAsFloat01WithoutTimeZone() throws Exception + { + ZonedDateTime date = ZonedDateTime.ofInstant(Instant.ofEpochSecond(0L), Z1); + + ZonedDateTime value = this.mapper.readValue("0.000000000", ZonedDateTime.class); + + assertNotNull("The value should not be null.", value); + assertIsEqual(date, value); + assertEquals("The time zone is not correct.", GMT, value.getZone()); + } + + @Test + public void testDeserializationAsFloat01WithTimeZone() throws Exception + { + ZonedDateTime date = ZonedDateTime.ofInstant(Instant.ofEpochSecond(0L), Z1); + + this.mapper.setTimeZone(TimeZone.getDefault()); + ZonedDateTime value = this.mapper.readValue("0.000000000", ZonedDateTime.class); + + assertNotNull("The value should not be null.", value); + assertIsEqual(date, value); + assertEquals("The time zone is not correct.", ZoneId.systemDefault(), value.getZone()); + } + + @Test + public void testDeserializationAsFloat02WithoutTimeZone() throws Exception + { + ZonedDateTime date = ZonedDateTime.ofInstant(Instant.ofEpochSecond(123456789L, 183917322), Z2); + + ZonedDateTime value = this.mapper.readValue("123456789.183917322", ZonedDateTime.class); + + assertNotNull("The value should not be null.", value); + assertIsEqual(date, value); + assertEquals("The time zone is not correct.", GMT, value.getZone()); + } + + @Test + public void testDeserializationAsFloat02WithTimeZone() throws Exception + { + ZonedDateTime date = ZonedDateTime.ofInstant(Instant.ofEpochSecond(123456789L, 183917322), Z2); + + this.mapper.setTimeZone(TimeZone.getDefault()); + ZonedDateTime value = this.mapper.readValue("123456789.183917322", ZonedDateTime.class); + + assertNotNull("The value should not be null.", value); + assertIsEqual(date, value); + assertEquals("The time zone is not correct.", ZoneId.systemDefault(), value.getZone()); + } + + @Test + public void testDeserializationAsFloat03WithoutTimeZone() throws Exception + { + ZonedDateTime date = ZonedDateTime.now(Z3); + + ZonedDateTime value = this.mapper.readValue( + DecimalUtils.toDecimal(date.toEpochSecond(), date.getNano()), ZonedDateTime.class + ); + + assertNotNull("The value should not be null.", value); + assertIsEqual(date, value); + assertEquals("The time zone is not correct.", GMT, value.getZone()); + } + + @Test + public void testDeserializationAsFloat03WithTimeZone() throws Exception + { + ZonedDateTime date = ZonedDateTime.now(Z3); + + this.mapper.setTimeZone(TimeZone.getDefault()); + ZonedDateTime value = this.mapper.readValue( + DecimalUtils.toDecimal(date.toEpochSecond(), date.getNano()), ZonedDateTime.class + ); + + assertNotNull("The value should not be null.", value); + assertIsEqual(date, value); + assertEquals("The time zone is not correct.", ZoneId.systemDefault(), value.getZone()); + } + + @Test + public void testDeserializationAsInt01NanosecondsWithoutTimeZone() throws Exception + { + ZonedDateTime date = ZonedDateTime.ofInstant(Instant.ofEpochSecond(0L), Z1); + + this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, true); + ZonedDateTime value = this.mapper.readValue("0", ZonedDateTime.class); + + assertNotNull("The value should not be null.", value); + assertIsEqual(date, value); + assertEquals("The time zone is not correct.", GMT, value.getZone()); + } + + @Test + public void testDeserializationAsInt01NanosecondsWithTimeZone() throws Exception + { + ZonedDateTime date = ZonedDateTime.ofInstant(Instant.ofEpochSecond(0L), Z1); + + this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, true); + this.mapper.setTimeZone(TimeZone.getDefault()); + ZonedDateTime value = this.mapper.readValue("0", ZonedDateTime.class); + + assertNotNull("The value should not be null.", value); + assertIsEqual(date, value); + assertEquals("The time zone is not correct.", ZoneId.systemDefault(), value.getZone()); + } + + @Test + public void testDeserializationAsInt01MillisecondsWithoutTimeZone() throws Exception + { + ZonedDateTime date = ZonedDateTime.ofInstant(Instant.ofEpochSecond(0L), Z1); + + this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false); + ZonedDateTime value = this.mapper.readValue("0", ZonedDateTime.class); + + assertNotNull("The value should not be null.", value); + assertIsEqual(date, value); + assertEquals("The time zone is not correct.", GMT, value.getZone()); + } + + @Test + public void testDeserializationAsInt01MillisecondsWithTimeZone() throws Exception + { + ZonedDateTime date = ZonedDateTime.ofInstant(Instant.ofEpochSecond(0L), Z1); + + this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false); + this.mapper.setTimeZone(TimeZone.getDefault()); + ZonedDateTime value = this.mapper.readValue("0", ZonedDateTime.class); + + assertNotNull("The value should not be null.", value); + assertIsEqual(date, value); + assertEquals("The time zone is not correct.", ZoneId.systemDefault(), value.getZone()); + } + + @Test + public void testDeserializationAsInt02NanosecondsWithoutTimeZone() throws Exception + { + ZonedDateTime date = ZonedDateTime.ofInstant(Instant.ofEpochSecond(123456789L, 0), Z2); + + this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, true); + ZonedDateTime value = this.mapper.readValue("123456789", ZonedDateTime.class); + + assertNotNull("The value should not be null.", value); + assertIsEqual(date, value); + assertEquals("The time zone is not correct.", GMT, value.getZone()); + } + + @Test + public void testDeserializationAsInt02NanosecondsWithTimeZone() throws Exception + { + ZonedDateTime date = ZonedDateTime.ofInstant(Instant.ofEpochSecond(123456789L, 0), Z2); + + this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, true); + this.mapper.setTimeZone(TimeZone.getDefault()); + ZonedDateTime value = this.mapper.readValue("123456789", ZonedDateTime.class); + + assertNotNull("The value should not be null.", value); + assertIsEqual(date, value); + assertEquals("The time zone is not correct.", ZoneId.systemDefault(), value.getZone()); + } + + @Test + public void testDeserializationAsInt02MillisecondsWithoutTimeZone() throws Exception + { + ZonedDateTime date = ZonedDateTime.ofInstant(Instant.ofEpochSecond(123456789L, 422000000), Z2); + + this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false); + ZonedDateTime value = this.mapper.readValue("123456789422", ZonedDateTime.class); + + assertNotNull("The value should not be null.", value); + assertIsEqual(date, value); + assertEquals("The time zone is not correct.", GMT, value.getZone()); + } + + @Test + public void testDeserializationAsInt02MillisecondsWithTimeZone() throws Exception + { + ZonedDateTime date = ZonedDateTime.ofInstant(Instant.ofEpochSecond(123456789L, 422000000), Z2); + + this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false); + this.mapper.setTimeZone(TimeZone.getDefault()); + ZonedDateTime value = this.mapper.readValue("123456789422", ZonedDateTime.class); + + assertNotNull("The value should not be null.", value); + assertIsEqual(date, value); + assertEquals("The time zone is not correct.", ZoneId.systemDefault(), value.getZone()); + } + + @Test + public void testDeserializationAsInt03NanosecondsWithoutTimeZone() throws Exception + { + ZonedDateTime date = ZonedDateTime.now(Z3); + date = date.minus(date.getNano(), ChronoUnit.NANOS); + + this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, true); + ZonedDateTime value = this.mapper.readValue(Long.toString(date.toEpochSecond()), ZonedDateTime.class); + + assertNotNull("The value should not be null.", value); + assertIsEqual(date, value); + assertEquals("The time zone is not correct.", GMT, value.getZone()); + } + + @Test + public void testDeserializationAsInt03NanosecondsWithTimeZone() throws Exception + { + ZonedDateTime date = ZonedDateTime.now(Z3); + date = date.minus(date.getNano(), ChronoUnit.NANOS); + + this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, true); + this.mapper.setTimeZone(TimeZone.getDefault()); + ZonedDateTime value = this.mapper.readValue(Long.toString(date.toEpochSecond()), ZonedDateTime.class); + + assertNotNull("The value should not be null.", value); + assertIsEqual(date, value); + assertEquals("The time zone is not correct.", ZoneId.systemDefault(), value.getZone()); + } + + @Test + public void testDeserializationAsInt03MillisecondsWithoutTimeZone() throws Exception + { + ZonedDateTime date = ZonedDateTime.now(Z3); + date = date.minus(date.getNano() - (date.get(ChronoField.MILLI_OF_SECOND) * 1_000_000), ChronoUnit.NANOS); + + this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false); + ZonedDateTime value = + this.mapper.readValue(Long.toString(date.toInstant().toEpochMilli()), ZonedDateTime.class); + + assertNotNull("The value should not be null.", value); + assertIsEqual(date, value); + assertEquals("The time zone is not correct.", GMT, value.getZone()); + } + + @Test + public void testDeserializationAsInt03MillisecondsWithTimeZone() throws Exception + { + ZonedDateTime date = ZonedDateTime.now(Z3); + date = date.minus(date.getNano() - (date.get(ChronoField.MILLI_OF_SECOND) * 1_000_000), ChronoUnit.NANOS); + + this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false); + this.mapper.setTimeZone(TimeZone.getDefault()); + ZonedDateTime value = + this.mapper.readValue(Long.toString(date.toInstant().toEpochMilli()), ZonedDateTime.class); + + assertNotNull("The value should not be null.", value); + assertIsEqual(date, value); + assertEquals("The time zone is not correct.", ZoneId.systemDefault(), value.getZone()); + } + + @Test + public void testDeserializationAsString01WithoutTimeZone() throws Exception + { + ZonedDateTime date = ZonedDateTime.ofInstant(Instant.ofEpochSecond(0L), Z1); + + this.mapper.configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, true); + ZonedDateTime value = this.mapper.readValue('"' + date.toString() + '"', ZonedDateTime.class); + + assertNotNull("The value should not be null.", value); + assertIsEqual(date, value); + assertEquals("The time zone is not correct.", GMT, value.getZone()); + } + + @Test + public void testDeserializationAsString01WithTimeZone() throws Exception + { + ZonedDateTime date = ZonedDateTime.ofInstant(Instant.ofEpochSecond(0L), Z1); + + this.mapper.configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, true); + this.mapper.setTimeZone(TimeZone.getDefault()); + ZonedDateTime value = this.mapper.readValue('"' + date.toString() + '"', ZonedDateTime.class); + + assertNotNull("The value should not be null.", value); + assertIsEqual(date, value); + assertEquals("The time zone is not correct.", ZoneId.systemDefault(), value.getZone()); + } + + @Test + public void testDeserializationAsString01WithTimeZoneTurnedOff() throws Exception + { + ZonedDateTime date = ZonedDateTime.ofInstant(Instant.ofEpochSecond(0L), Z1); + + this.mapper.configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, false); + this.mapper.setTimeZone(TimeZone.getDefault()); + ZonedDateTime value = this.mapper.readValue('"' + date.toString() + '"', ZonedDateTime.class); + + assertNotNull("The value should not be null.", value); + assertIsEqual(date, value); + assertEquals("The time zone is not correct.", Z1, value.getZone()); + } + + @Test + public void testDeserializationAsString02WithoutTimeZone() throws Exception + { + ZonedDateTime date = ZonedDateTime.ofInstant(Instant.ofEpochSecond(123456789L, 183917322), Z2); + + this.mapper.configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, true); + ZonedDateTime value = this.mapper.readValue('"' + date.toString() + '"', ZonedDateTime.class); + + assertNotNull("The value should not be null.", value); + assertIsEqual(date, value); + assertEquals("The time zone is not correct.", GMT, value.getZone()); + } + + @Test + public void testDeserializationAsString02WithTimeZone() throws Exception + { + ZonedDateTime date = ZonedDateTime.ofInstant(Instant.ofEpochSecond(123456789L, 183917322), Z2); + + this.mapper.configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, true); + this.mapper.setTimeZone(TimeZone.getDefault()); + ZonedDateTime value = this.mapper.readValue('"' + date.toString() + '"', ZonedDateTime.class); + + assertNotNull("The value should not be null.", value); + assertIsEqual(date, value); + assertEquals("The time zone is not correct.", ZoneId.systemDefault(), value.getZone()); + } + + @Test + public void testDeserializationAsString02WithTimeZoneTurnedOff() throws Exception + { + ZonedDateTime date = ZonedDateTime.ofInstant(Instant.ofEpochSecond(123456789L, 183917322), Z2); + + this.mapper.configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, false); + this.mapper.setTimeZone(TimeZone.getDefault()); + ZonedDateTime value = this.mapper.readValue('"' + date.toString() + '"', ZonedDateTime.class); + + assertNotNull("The value should not be null.", value); + assertIsEqual(date, value); + assertEquals("The time zone is not correct.", Z2, value.getZone()); + } + + @Test + public void testDeserializationAsString03WithoutTimeZone() throws Exception + { + ZonedDateTime date = ZonedDateTime.now(Z3); + + this.mapper.configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, true); + ZonedDateTime value = this.mapper.readValue('"' + date.toString() + '"', ZonedDateTime.class); + + assertNotNull("The value should not be null.", value); + assertIsEqual(date, value); + assertEquals("The time zone is not correct.", GMT, value.getZone()); + } + + @Test + public void testDeserializationAsString03WithTimeZone() throws Exception + { + ZonedDateTime date = ZonedDateTime.now(Z3); + + this.mapper.configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, true); + this.mapper.setTimeZone(TimeZone.getDefault()); + ZonedDateTime value = this.mapper.readValue('"' + date.toString() + '"', ZonedDateTime.class); + + assertNotNull("The value should not be null.", value); + assertIsEqual(date, value); + assertEquals("The time zone is not correct.", ZoneId.systemDefault(), value.getZone()); + } + + @Test + public void testDeserializationAsString03WithTimeZoneTurnedOff() throws Exception + { + ZonedDateTime date = ZonedDateTime.now(Z3); + + this.mapper.configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, false); + this.mapper.setTimeZone(TimeZone.getDefault()); + ZonedDateTime value = this.mapper.readValue('"' + date.toString() + '"', ZonedDateTime.class); + + assertNotNull("The value should not be null.", value); + assertIsEqual(date, value); + assertEquals("The time zone is not correct.", Z3, value.getZone()); + } + + @Test + public void testDeserializationWithTypeInfo01WithoutTimeZone() throws Exception + { + ZonedDateTime date = ZonedDateTime.ofInstant(Instant.ofEpochSecond(123456789L, 183917322), Z2); + + this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); + Temporal value = this.mapper.readValue( + "[\"" + ZonedDateTime.class.getName() + "\",123456789.183917322]", Temporal.class + ); + + assertNotNull("The value should not be null.", value); + assertTrue("The value should be an ZonedDateTime.", value instanceof ZonedDateTime); + assertIsEqual(date, (ZonedDateTime) value); + assertEquals("The time zone is not correct.", GMT, ((ZonedDateTime) value).getZone()); + } + + @Test + public void testDeserializationWithTypeInfo01WithTimeZone() throws Exception + { + ZonedDateTime date = ZonedDateTime.ofInstant(Instant.ofEpochSecond(123456789L, 183917322), Z2); + + this.mapper.setTimeZone(TimeZone.getDefault()); + this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); + Temporal value = this.mapper.readValue( + "[\"" + ZonedDateTime.class.getName() + "\",123456789.183917322]", Temporal.class + ); + + assertNotNull("The value should not be null.", value); + assertTrue("The value should be an ZonedDateTime.", value instanceof ZonedDateTime); + assertIsEqual(date, (ZonedDateTime) value); + assertEquals("The time zone is not correct.", ZoneId.systemDefault(), ((ZonedDateTime) value).getZone()); + } + + @Test + public void testDeserializationWithTypeInfo02WithoutTimeZone() throws Exception + { + ZonedDateTime date = ZonedDateTime.ofInstant(Instant.ofEpochSecond(123456789L, 0), Z2); + + this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, true); + this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); + Temporal value = this.mapper.readValue( + "[\"" + ZonedDateTime.class.getName() + "\",123456789]", Temporal.class + ); + + assertNotNull("The value should not be null.", value); + assertTrue("The value should be an ZonedDateTime.", value instanceof ZonedDateTime); + assertIsEqual(date, (ZonedDateTime) value); + assertEquals("The time zone is not correct.", GMT, ((ZonedDateTime) value).getZone()); + } + + @Test + public void testDeserializationWithTypeInfo02WithTimeZone() throws Exception + { + ZonedDateTime date = ZonedDateTime.ofInstant(Instant.ofEpochSecond(123456789L, 0), Z2); + + this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, true); + this.mapper.setTimeZone(TimeZone.getDefault()); + this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); + Temporal value = this.mapper.readValue( + "[\"" + ZonedDateTime.class.getName() + "\",123456789]", Temporal.class + ); + + assertNotNull("The value should not be null.", value); + assertTrue("The value should be an ZonedDateTime.", value instanceof ZonedDateTime); + assertIsEqual(date, (ZonedDateTime) value); + assertEquals("The time zone is not correct.", ZoneId.systemDefault(), ((ZonedDateTime) value).getZone()); + } + + @Test + public void testDeserializationWithTypeInfo03WithoutTimeZone() throws Exception + { + ZonedDateTime date = ZonedDateTime.ofInstant(Instant.ofEpochSecond(123456789L, 422000000), Z2); + + this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false); + this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); + Temporal value = this.mapper.readValue( + "[\"" + ZonedDateTime.class.getName() + "\",123456789422]", Temporal.class + ); + + assertNotNull("The value should not be null.", value); + assertTrue("The value should be an ZonedDateTime.", value instanceof ZonedDateTime); + assertIsEqual(date, (ZonedDateTime) value); + assertEquals("The time zone is not correct.", GMT, ((ZonedDateTime) value).getZone()); + } + + @Test + public void testDeserializationWithTypeInfo03WithTimeZone() throws Exception + { + ZonedDateTime date = ZonedDateTime.ofInstant(Instant.ofEpochSecond(123456789L, 422000000), Z2); + + this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false); + this.mapper.setTimeZone(TimeZone.getDefault()); + this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); + Temporal value = this.mapper.readValue( + "[\"" + ZonedDateTime.class.getName() + "\",123456789422]", Temporal.class + ); + + assertNotNull("The value should not be null.", value); + assertTrue("The value should be an ZonedDateTime.", value instanceof ZonedDateTime); + assertIsEqual(date, (ZonedDateTime) value); + assertEquals("The time zone is not correct.", ZoneId.systemDefault(), ((ZonedDateTime) value).getZone()); + } + + @Test + public void testDeserializationWithTypeInfo04WithoutTimeZone() throws Exception + { + ZonedDateTime date = ZonedDateTime.now(Z3); + + this.mapper.configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, true); + this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); + Temporal value = this.mapper.readValue( + "[\"" + ZonedDateTime.class.getName() + "\",\"" + date.toString() + "\"]", Temporal.class + ); + + assertNotNull("The value should not be null.", value); + assertTrue("The value should be an ZonedDateTime.", value instanceof ZonedDateTime); + assertIsEqual(date, (ZonedDateTime) value); + assertEquals("The time zone is not correct.", GMT, ((ZonedDateTime) value).getZone()); + } + + @Test + public void testDeserializationWithTypeInfo04WithTimeZone() throws Exception + { + ZonedDateTime date = ZonedDateTime.now(Z3); + + this.mapper.configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, true); + this.mapper.setTimeZone(TimeZone.getDefault()); + this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); + Temporal value = this.mapper.readValue( + "[\"" + ZonedDateTime.class.getName() + "\",\"" + date.toString() + "\"]", Temporal.class + ); + + assertNotNull("The value should not be null.", value); + assertTrue("The value should be an ZonedDateTime.", value instanceof ZonedDateTime); + assertIsEqual(date, (ZonedDateTime) value); + assertEquals("The time zone is not correct.", ZoneId.systemDefault(), ((ZonedDateTime) value).getZone()); + } + + @Test + public void testDeserializationWithTypeInfo04WithTimeZoneTurnedOff() throws Exception + { + ZonedDateTime date = ZonedDateTime.now(Z3); + + this.mapper.configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, false); + this.mapper.setTimeZone(TimeZone.getDefault()); + this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); + Temporal value = this.mapper.readValue( + "[\"" + ZonedDateTime.class.getName() + "\",\"" + date.toString() + "\"]", Temporal.class + ); + + assertNotNull("The value should not be null.", value); + assertTrue("The value should be an ZonedDateTime.", value instanceof ZonedDateTime); + assertIsEqual(date, (ZonedDateTime) value); + assertEquals("The time zone is not correct.", Z3, ((ZonedDateTime) value).getZone()); + } + + private static void assertIsEqual(ZonedDateTime expected, ZonedDateTime actual) + { + assertTrue("The value is not correct. Expected timezone-adjusted <" + expected + ">, actual <" + actual + ">.", + expected.isEqual(actual)); + } +} From 4122be33e5309f39523e11bfa0eba3b63d46ae00 Mon Sep 17 00:00:00 2001 From: Zoltan Kiss Date: Fri, 29 May 2015 23:03:37 +0100 Subject: [PATCH 11/11] #13 Use new module in tests --- .../ZonedDateTimeWithZoneIdSerializer.java | 5 +-- .../datatype/jsr310/ModuleTestBase.java | 2 +- .../jsr310/TestDurationKeySerialization.java | 2 +- .../jsr310/TestInstantKeySerialization.java | 2 +- .../jsr310/TestInstantSerialization.java | 27 ++++++------ .../jsr310/TestLocalDateKeySerialization.java | 2 +- .../jsr310/TestLocalDateSerialization.java | 16 ++++--- .../TestLocalDateTimeKeySerialization.java | 2 +- .../jsr310/TestLocalTimeKeySerialization.java | 2 +- .../jsr310/TestLocalTimeSerialization.java | 20 +++++---- .../jsr310/TestMonthDayKeySerialization.java | 2 +- .../jsr310/TestMonthDaySerialization.java | 14 +++--- .../jsr310/TestNullKeySerialization.java | 2 +- .../TestOffsetDateTimeKeySerialization.java | 2 +- .../TestOffsetDateTimeSerialization.java | 43 ++++++++++--------- .../TestOffsetTimeKeySerialization.java | 2 +- .../jsr310/TestOffsetTimeSerialization.java | 22 +++++----- .../jsr310/TestPeriodKeySerialization.java | 2 +- .../jsr310/TestPeriodSerialization.java | 16 ++++--- .../jsr310/TestYearKeySerialization.java | 2 +- .../jsr310/TestYearMonthKeySerialization.java | 2 +- .../jsr310/TestYearMonthSerialization.java | 32 +++++++------- .../jsr310/TestYearSerialization.java | 14 +++--- .../jsr310/TestZoneIdKeySerialization.java | 2 +- .../jsr310/TestZoneIdSerialization.java | 11 ++--- .../TestZoneOffsetKeySerialization.java | 2 +- .../jsr310/TestZoneOffsetSerialization.java | 14 +++--- .../TestZonedDateTimeKeySerialization.java | 2 +- .../TestZonedDateTimeSerialization.java | 41 +++++++++--------- 29 files changed, 162 insertions(+), 145 deletions(-) diff --git a/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/ZonedDateTimeWithZoneIdSerializer.java b/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/ZonedDateTimeWithZoneIdSerializer.java index bf67f768..478fb272 100644 --- a/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/ZonedDateTimeWithZoneIdSerializer.java +++ b/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/ZonedDateTimeWithZoneIdSerializer.java @@ -1,7 +1,6 @@ package com.fasterxml.jackson.datatype.jsr310.ser; import java.time.ZonedDateTime; -import java.time.format.DateTimeFormatter; // TODO deprecate this: SerializationFeature config should be respected, default behaviour should be to // serialize according to ISO-8601 format @@ -14,7 +13,7 @@ public class ZonedDateTimeWithZoneIdSerializer extends InstantSerializerBase dt.toInstant().toEpochMilli(), ZonedDateTime::toEpochSecond, ZonedDateTime::getNano, - // Serialize in a backwards compatible way: with zone id - DateTimeFormatter.ISO_ZONED_DATE_TIME::format); + // Serialize in a backwards compatible way: with zone id, using toString method + Object::toString); } } diff --git a/src/test/java/com/fasterxml/jackson/datatype/jsr310/ModuleTestBase.java b/src/test/java/com/fasterxml/jackson/datatype/jsr310/ModuleTestBase.java index fcb773f9..1e5bf8bb 100644 --- a/src/test/java/com/fasterxml/jackson/datatype/jsr310/ModuleTestBase.java +++ b/src/test/java/com/fasterxml/jackson/datatype/jsr310/ModuleTestBase.java @@ -6,6 +6,6 @@ public class ModuleTestBase { protected ObjectMapper newMapper() { return new ObjectMapper() - .registerModule(new JSR310Module()); + .registerModule(new JavaTimeModule()); } } diff --git a/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestDurationKeySerialization.java b/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestDurationKeySerialization.java index 7cc9e6db..009c5b74 100644 --- a/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestDurationKeySerialization.java +++ b/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestDurationKeySerialization.java @@ -24,7 +24,7 @@ public class TestDurationKeySerialization { @Before public void setUp() { this.om = new ObjectMapper(); - om.registerModule(new JSR310Module()); + om.registerModule(new JavaTimeModule()); map = new HashMap<>(); } diff --git a/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestInstantKeySerialization.java b/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestInstantKeySerialization.java index 3cfc6b75..1ba54c8f 100644 --- a/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestInstantKeySerialization.java +++ b/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestInstantKeySerialization.java @@ -27,7 +27,7 @@ public class TestInstantKeySerialization { @Before public void setUp() { this.om = new ObjectMapper(); - om.registerModule(new JSR310Module()); + om.registerModule(new JavaTimeModule()); map = new HashMap<>(); } diff --git a/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestInstantSerialization.java b/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestInstantSerialization.java index c28ea332..ae397959 100644 --- a/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestInstantSerialization.java +++ b/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestInstantSerialization.java @@ -16,19 +16,20 @@ package com.fasterxml.jackson.datatype.jsr310; -import com.fasterxml.jackson.databind.DeserializationFeature; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.SerializationFeature; - -import org.junit.Before; -import org.junit.Test; +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.format.DateTimeFormatter; import java.time.temporal.ChronoUnit; import java.time.temporal.Temporal; -import static org.junit.Assert.*; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; +import org.junit.Before; +import org.junit.Test; public class TestInstantSerialization { @@ -40,7 +41,7 @@ public class TestInstantSerialization public void setUp() { this.mapper = new ObjectMapper(); - this.mapper.registerModule(new JSR310Module()); + this.mapper.registerModule(new JavaTimeModule()); } @Test @@ -228,7 +229,7 @@ public void testDeserializationAsFloat03() throws Exception Instant value = this.mapper.readValue( DecimalUtils.toDecimal(date.getEpochSecond(), date.getNano()), Instant.class - ); + ); assertNotNull("The value should not be null.", value); assertEquals("The value is not correct.", date, value); @@ -349,7 +350,7 @@ public void testDeserializationWithTypeInfo01() throws Exception this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); Temporal value = this.mapper.readValue( "[\"" + Instant.class.getName() + "\",123456789.183917322]", Temporal.class - ); + ); assertNotNull("The value should not be null.", value); assertTrue("The value should be an Instant.", value instanceof Instant); @@ -365,7 +366,7 @@ public void testDeserializationWithTypeInfo02() throws Exception this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); Temporal value = this.mapper.readValue( "[\"" + Instant.class.getName() + "\",123456789]", Temporal.class - ); + ); assertNotNull("The value should not be null.", value); assertTrue("The value should be an Instant.", value instanceof Instant); @@ -381,7 +382,7 @@ public void testDeserializationWithTypeInfo03() throws Exception this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); Temporal value = this.mapper.readValue( "[\"" + Instant.class.getName() + "\",123456789422]", Temporal.class - ); + ); assertNotNull("The value should not be null.", value); assertTrue("The value should be an Instant.", value instanceof Instant); @@ -396,7 +397,7 @@ public void testDeserializationWithTypeInfo04() throws Exception this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); Temporal value = this.mapper.readValue( "[\"" + Instant.class.getName() + "\",\"" + FORMATTER.format(date) + "\"]", Temporal.class - ); + ); assertNotNull("The value should not be null.", value); assertTrue("The value should be an Instant.", value instanceof Instant); diff --git a/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestLocalDateKeySerialization.java b/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestLocalDateKeySerialization.java index d2628df1..3a3cde4c 100644 --- a/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestLocalDateKeySerialization.java +++ b/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestLocalDateKeySerialization.java @@ -24,7 +24,7 @@ public class TestLocalDateKeySerialization { @Before public void setUp() { this.om = new ObjectMapper(); - om.registerModule(new JSR310Module()); + om.registerModule(new JavaTimeModule()); map = new HashMap<>(); } diff --git a/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestLocalDateSerialization.java b/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestLocalDateSerialization.java index 31ba52d9..ef49b4a6 100644 --- a/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestLocalDateSerialization.java +++ b/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestLocalDateSerialization.java @@ -16,16 +16,18 @@ package com.fasterxml.jackson.datatype.jsr310; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.SerializationFeature; -import org.junit.Before; -import org.junit.Test; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; import java.time.LocalDate; import java.time.Month; import java.time.temporal.Temporal; -import static org.junit.Assert.*; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; +import org.junit.Before; +import org.junit.Test; public class TestLocalDateSerialization { @@ -35,7 +37,7 @@ public class TestLocalDateSerialization public void setUp() { this.mapper = new ObjectMapper(); - this.mapper.registerModule(new JSR310Module()); + this.mapper.registerModule(new JavaTimeModule()); } @Test @@ -152,7 +154,7 @@ public void testDeserializationWithTypeInfo01() throws Exception this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); Temporal value = this.mapper.readValue( "[\"" + LocalDate.class.getName() + "\",\"" + date.toString() + "\"]", Temporal.class - ); + ); assertNotNull("The value should not be null.", value); assertTrue("The value should be a LocalDate.", value instanceof LocalDate); diff --git a/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestLocalDateTimeKeySerialization.java b/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestLocalDateTimeKeySerialization.java index 9f5f7bcb..fe8efb21 100644 --- a/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestLocalDateTimeKeySerialization.java +++ b/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestLocalDateTimeKeySerialization.java @@ -30,7 +30,7 @@ public class TestLocalDateTimeKeySerialization { @Before public void setUp() { this.om = new ObjectMapper(); - om.registerModule(new JSR310Module()); + om.registerModule(new JavaTimeModule()); map = new HashMap<>(); } diff --git a/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestLocalTimeKeySerialization.java b/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestLocalTimeKeySerialization.java index c7320eaa..1a326d12 100644 --- a/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestLocalTimeKeySerialization.java +++ b/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestLocalTimeKeySerialization.java @@ -28,7 +28,7 @@ public class TestLocalTimeKeySerialization { @Before public void setUp() { this.om = new ObjectMapper(); - om.registerModule(new JSR310Module()); + om.registerModule(new JavaTimeModule()); map = new HashMap<>(); } diff --git a/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestLocalTimeSerialization.java b/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestLocalTimeSerialization.java index 95ed266b..9934c551 100644 --- a/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestLocalTimeSerialization.java +++ b/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestLocalTimeSerialization.java @@ -16,6 +16,13 @@ package com.fasterxml.jackson.datatype.jsr310; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.time.LocalTime; +import java.time.temporal.Temporal; + import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; @@ -23,11 +30,6 @@ import org.junit.Before; import org.junit.Test; -import java.time.LocalTime; -import java.time.temporal.Temporal; - -import static org.junit.Assert.*; - public class TestLocalTimeSerialization { private ObjectMapper mapper; @@ -36,7 +38,7 @@ public class TestLocalTimeSerialization public void setUp() { this.mapper = new ObjectMapper(); - this.mapper.registerModule(new JSR310Module()); + this.mapper.registerModule(new JavaTimeModule()); } @After @@ -323,7 +325,7 @@ public void testDeserializationWithTypeInfo01() throws Exception this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); Temporal value = this.mapper.readValue( "[\"" + LocalTime.class.getName() + "\",[22,31,5,829837]]", Temporal.class - ); + ); assertNotNull("The value should not be null.", value); assertTrue("The value should be a LocalTime.", value instanceof LocalTime); @@ -339,7 +341,7 @@ public void testDeserializationWithTypeInfo02() throws Exception this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); Temporal value = this.mapper.readValue( "[\"" + LocalTime.class.getName() + "\",[22,31,5,422]]", Temporal.class - ); + ); assertNotNull("The value should not be null.", value); assertTrue("The value should be a LocalTime.", value instanceof LocalTime); @@ -354,7 +356,7 @@ public void testDeserializationWithTypeInfo03() throws Exception this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); Temporal value = this.mapper.readValue( "[\"" + LocalTime.class.getName() + "\",\"" + time.toString() + "\"]", Temporal.class - ); + ); assertNotNull("The value should not be null.", value); assertTrue("The value should be a LocalTime.", value instanceof LocalTime); diff --git a/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestMonthDayKeySerialization.java b/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestMonthDayKeySerialization.java index 80d407de..f47e1a80 100644 --- a/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestMonthDayKeySerialization.java +++ b/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestMonthDayKeySerialization.java @@ -23,7 +23,7 @@ public class TestMonthDayKeySerialization { @Before public void setUp() { this.om = new ObjectMapper(); - om.registerModule(new JSR310Module()); + om.registerModule(new JavaTimeModule()); map = new HashMap<>(); } diff --git a/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestMonthDaySerialization.java b/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestMonthDaySerialization.java index a548ec8e..9fd8928a 100644 --- a/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestMonthDaySerialization.java +++ b/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestMonthDaySerialization.java @@ -16,16 +16,18 @@ package com.fasterxml.jackson.datatype.jsr310; -import com.fasterxml.jackson.databind.ObjectMapper; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; import java.time.Month; import java.time.MonthDay; import java.time.temporal.TemporalAccessor; -import static org.junit.Assert.*; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; public class TestMonthDaySerialization { @@ -35,7 +37,7 @@ public class TestMonthDaySerialization public void setUp() { this.mapper = new ObjectMapper(); - this.mapper.registerModule(new JSR310Module()); + this.mapper.registerModule(new JavaTimeModule()); } @After diff --git a/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestNullKeySerialization.java b/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestNullKeySerialization.java index 84130de9..76277b02 100644 --- a/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestNullKeySerialization.java +++ b/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestNullKeySerialization.java @@ -22,7 +22,7 @@ public class TestNullKeySerialization { @Before public void setUp() { this.om = new ObjectMapper(); - om.registerModule(new JSR310Module()); + om.registerModule(new JavaTimeModule()); map = new HashMap<>(); } diff --git a/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestOffsetDateTimeKeySerialization.java b/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestOffsetDateTimeKeySerialization.java index f9018062..cc166002 100644 --- a/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestOffsetDateTimeKeySerialization.java +++ b/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestOffsetDateTimeKeySerialization.java @@ -29,7 +29,7 @@ public class TestOffsetDateTimeKeySerialization { @Before public void setUp() { this.om = new ObjectMapper(); - om.registerModule(new JSR310Module()); + om.registerModule(new JavaTimeModule()); map = new HashMap<>(); } diff --git a/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestOffsetDateTimeSerialization.java b/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestOffsetDateTimeSerialization.java index 3d64b23e..419013fa 100644 --- a/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestOffsetDateTimeSerialization.java +++ b/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestOffsetDateTimeSerialization.java @@ -16,13 +16,9 @@ package com.fasterxml.jackson.datatype.jsr310; -import com.fasterxml.jackson.databind.DeserializationFeature; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.SerializationFeature; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; +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.OffsetDateTime; @@ -34,7 +30,12 @@ import java.time.temporal.Temporal; import java.util.TimeZone; -import static org.junit.Assert.*; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; public class TestOffsetDateTimeSerialization { @@ -52,7 +53,7 @@ public class TestOffsetDateTimeSerialization public void setUp() { this.mapper = new ObjectMapper(); - this.mapper.registerModule(new JSR310Module()); + this.mapper.registerModule(new JavaTimeModule()); } @After @@ -276,7 +277,7 @@ public void testDeserializationAsFloat03WithoutTimeZone() throws Exception OffsetDateTime value = this.mapper.readValue( DecimalUtils.toDecimal(date.toEpochSecond(), date.getNano()), OffsetDateTime.class - ); + ); assertNotNull("The value should not be null.", value); assertIsEqual(date, value); @@ -291,7 +292,7 @@ public void testDeserializationAsFloat03WithTimeZone() throws Exception this.mapper.setTimeZone(TimeZone.getDefault()); OffsetDateTime value = this.mapper.readValue( DecimalUtils.toDecimal(date.toEpochSecond(), date.getNano()), OffsetDateTime.class - ); + ); assertNotNull("The value should not be null.", value); assertIsEqual(date, value); @@ -597,7 +598,7 @@ public void testDeserializationWithTypeInfo01WithoutTimeZone() throws Exception this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); Temporal value = this.mapper.readValue( "[\"" + OffsetDateTime.class.getName() + "\",123456789.183917322]", Temporal.class - ); + ); assertNotNull("The value should not be null.", value); assertTrue("The value should be an OffsetDateTime.", value instanceof OffsetDateTime); @@ -614,7 +615,7 @@ public void testDeserializationWithTypeInfo01WithTimeZone() throws Exception this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); Temporal value = this.mapper.readValue( "[\"" + OffsetDateTime.class.getName() + "\",123456789.183917322]", Temporal.class - ); + ); assertNotNull("The value should not be null.", value); assertTrue("The value should be an OffsetDateTime.", value instanceof OffsetDateTime); @@ -631,7 +632,7 @@ public void testDeserializationWithTypeInfo02WithoutTimeZone() throws Exception this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); Temporal value = this.mapper.readValue( "[\"" + OffsetDateTime.class.getName() + "\",123456789]", Temporal.class - ); + ); assertNotNull("The value should not be null.", value); assertTrue("The value should be an OffsetDateTime.", value instanceof OffsetDateTime); @@ -649,7 +650,7 @@ public void testDeserializationWithTypeInfo02WithTimeZone() throws Exception this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); Temporal value = this.mapper.readValue( "[\"" + OffsetDateTime.class.getName() + "\",123456789]", Temporal.class - ); + ); assertNotNull("The value should not be null.", value); assertTrue("The value should be an OffsetDateTime.", value instanceof OffsetDateTime); @@ -666,7 +667,7 @@ public void testDeserializationWithTypeInfo03WithoutTimeZone() throws Exception this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); Temporal value = this.mapper.readValue( "[\"" + OffsetDateTime.class.getName() + "\",123456789422]", Temporal.class - ); + ); assertNotNull("The value should not be null.", value); assertTrue("The value should be an OffsetDateTime.", value instanceof OffsetDateTime); @@ -684,7 +685,7 @@ public void testDeserializationWithTypeInfo03WithTimeZone() throws Exception this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); Temporal value = this.mapper.readValue( "[\"" + OffsetDateTime.class.getName() + "\",123456789422]", Temporal.class - ); + ); assertNotNull("The value should not be null.", value); assertTrue("The value should be an OffsetDateTime.", value instanceof OffsetDateTime); @@ -701,7 +702,7 @@ public void testDeserializationWithTypeInfo04WithoutTimeZone() throws Exception this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); Temporal value = this.mapper.readValue( "[\"" + OffsetDateTime.class.getName() + "\",\"" + FORMATTER.format(date) + "\"]", Temporal.class - ); + ); assertNotNull("The value should not be null.", value); assertTrue("The value should be an OffsetDateTime.", value instanceof OffsetDateTime); @@ -719,7 +720,7 @@ public void testDeserializationWithTypeInfo04WithTimeZone() throws Exception this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); Temporal value = this.mapper.readValue( "[\"" + OffsetDateTime.class.getName() + "\",\"" + FORMATTER.format(date) + "\"]", Temporal.class - ); + ); assertNotNull("The value should not be null.", value); assertTrue("The value should be an OffsetDateTime.", value instanceof OffsetDateTime); @@ -737,11 +738,11 @@ public void testDeserializationWithTypeInfo04WithTimeZoneTurnedOff() throws Exce this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); Temporal value = this.mapper.readValue( "[\"" + OffsetDateTime.class.getName() + "\",\"" + FORMATTER.format(date) + "\"]", Temporal.class - ); + ); assertNotNull("The value should not be null.", value); assertTrue("The value should be an OffsetDateTime.", value instanceof OffsetDateTime); - OffsetDateTime cast = (OffsetDateTime)value; + OffsetDateTime cast = (OffsetDateTime) value; assertIsEqual(date, cast); assertEquals("The time zone is not correct.", getOffset(cast, Z3), cast.getOffset()); } diff --git a/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestOffsetTimeKeySerialization.java b/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestOffsetTimeKeySerialization.java index ab619696..8e7b13bd 100644 --- a/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestOffsetTimeKeySerialization.java +++ b/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestOffsetTimeKeySerialization.java @@ -28,7 +28,7 @@ public class TestOffsetTimeKeySerialization { @Before public void setUp() { this.om = new ObjectMapper(); - om.registerModule(new JSR310Module()); + om.registerModule(new JavaTimeModule()); map = new HashMap<>(); } diff --git a/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestOffsetTimeSerialization.java b/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestOffsetTimeSerialization.java index 179101fd..95c97ccd 100644 --- a/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestOffsetTimeSerialization.java +++ b/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestOffsetTimeSerialization.java @@ -16,6 +16,14 @@ package com.fasterxml.jackson.datatype.jsr310; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.time.OffsetTime; +import java.time.ZoneOffset; +import java.time.temporal.Temporal; + import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; @@ -23,12 +31,6 @@ import org.junit.Before; import org.junit.Test; -import java.time.OffsetTime; -import java.time.ZoneOffset; -import java.time.temporal.Temporal; - -import static org.junit.Assert.*; - public class TestOffsetTimeSerialization { private ObjectMapper mapper; @@ -37,7 +39,7 @@ public class TestOffsetTimeSerialization public void setUp() { this.mapper = new ObjectMapper(); - this.mapper.registerModule(new JSR310Module()); + this.mapper.registerModule(new JavaTimeModule()); } @After @@ -326,7 +328,7 @@ public void testDeserializationWithTypeInfo01() throws Exception this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); Temporal value = this.mapper.readValue( "[\"" + OffsetTime.class.getName() + "\",[22,31,5,829837,\"+11:00\"]]", Temporal.class - ); + ); assertNotNull("The value should not be null.", value); assertTrue("The value should be a OffsetTime.", value instanceof OffsetTime); @@ -342,7 +344,7 @@ public void testDeserializationWithTypeInfo02() throws Exception this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); Temporal value = this.mapper.readValue( "[\"" + OffsetTime.class.getName() + "\",[22,31,5,422,\"+11:00\"]]", Temporal.class - ); + ); assertNotNull("The value should not be null.", value); assertTrue("The value should be a OffsetTime.", value instanceof OffsetTime); @@ -357,7 +359,7 @@ public void testDeserializationWithTypeInfo03() throws Exception this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); Temporal value = this.mapper.readValue( "[\"" + OffsetTime.class.getName() + "\",\"" + time.toString() + "\"]", Temporal.class - ); + ); assertNotNull("The value should not be null.", value); assertTrue("The value should be a OffsetTime.", value instanceof OffsetTime); diff --git a/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestPeriodKeySerialization.java b/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestPeriodKeySerialization.java index d0d6095a..197d5519 100644 --- a/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestPeriodKeySerialization.java +++ b/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestPeriodKeySerialization.java @@ -25,7 +25,7 @@ public class TestPeriodKeySerialization { @Before public void setUp() { this.om = new ObjectMapper(); - om.registerModule(new JSR310Module()); + om.registerModule(new JavaTimeModule()); map = new HashMap<>(); } diff --git a/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestPeriodSerialization.java b/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestPeriodSerialization.java index 91074df5..82d476d1 100644 --- a/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestPeriodSerialization.java +++ b/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestPeriodSerialization.java @@ -16,15 +16,17 @@ package com.fasterxml.jackson.datatype.jsr310; -import com.fasterxml.jackson.databind.ObjectMapper; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; import java.time.Period; import java.time.temporal.TemporalAmount; -import static org.junit.Assert.*; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; public class TestPeriodSerialization { @@ -34,7 +36,7 @@ public class TestPeriodSerialization public void setUp() { this.mapper = new ObjectMapper(); - this.mapper.registerModule(new JSR310Module()); + this.mapper.registerModule(new JavaTimeModule()); } @After @@ -108,7 +110,7 @@ public void testDeserializationWithTypeInfo01() throws Exception this.mapper.addMixIn(TemporalAmount.class, MockObjectConfiguration.class); TemporalAmount value = this.mapper.readValue( "[\"" + Period.class.getName() + "\",\"" + period.toString() + "\"]", TemporalAmount.class - ); + ); assertNotNull("The value should not be null.", value); assertTrue("The value should be a Period.", value instanceof Period); diff --git a/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestYearKeySerialization.java b/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestYearKeySerialization.java index 997a1003..44597f25 100644 --- a/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestYearKeySerialization.java +++ b/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestYearKeySerialization.java @@ -20,7 +20,7 @@ public class TestYearKeySerialization { @Before public void setUp() { this.om = new ObjectMapper(); - om.registerModule(new JSR310Module()); + om.registerModule(new JavaTimeModule()); map = new HashMap<>(); } diff --git a/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestYearMonthKeySerialization.java b/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestYearMonthKeySerialization.java index 949691f4..69b6be14 100644 --- a/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestYearMonthKeySerialization.java +++ b/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestYearMonthKeySerialization.java @@ -21,7 +21,7 @@ public class TestYearMonthKeySerialization { @Before public void setUp() { this.om = new ObjectMapper(); - om.registerModule(new JSR310Module()); + om.registerModule(new JavaTimeModule()); map = new HashMap<>(); } diff --git a/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestYearMonthSerialization.java b/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestYearMonthSerialization.java index ebc1f3bd..977ebf3e 100644 --- a/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestYearMonthSerialization.java +++ b/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestYearMonthSerialization.java @@ -16,22 +16,23 @@ package com.fasterxml.jackson.datatype.jsr310; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.SerializationFeature; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.time.Month; +import java.time.YearMonth; +import java.time.temporal.Temporal; + import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.annotation.JsonProperty; - +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; import org.junit.After; import org.junit.Before; import org.junit.Test; -import java.time.Month; -import java.time.YearMonth; -import java.time.temporal.Temporal; - -import static org.junit.Assert.*; - public class TestYearMonthSerialization { private ObjectMapper mapper; @@ -40,7 +41,7 @@ public class TestYearMonthSerialization public void setUp() { this.mapper = new ObjectMapper(); - this.mapper.registerModule(new JSR310Module()); + this.mapper.registerModule(new JavaTimeModule()); } @After @@ -171,21 +172,20 @@ public void testDeserializationWithTypeInfo01() throws Exception assertTrue("The value should be a YearMonth.", value instanceof YearMonth); assertEquals("The value is not correct.", yearMonth, value); } - - - private static class SimpleAggregate + + private static class SimpleAggregate { @JsonProperty("yearMonth") - @JsonFormat(pattern="yyMM") + @JsonFormat(pattern = "yyMM") final YearMonth yearMonth; - + @JsonCreator SimpleAggregate(@JsonProperty("yearMonth") YearMonth yearMonth) { this.yearMonth = yearMonth; } } - + @Test public void testSerializationWithPattern01() throws Exception { diff --git a/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestYearSerialization.java b/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestYearSerialization.java index 70d149ce..1be2992a 100644 --- a/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestYearSerialization.java +++ b/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestYearSerialization.java @@ -16,15 +16,17 @@ package com.fasterxml.jackson.datatype.jsr310; -import com.fasterxml.jackson.databind.ObjectMapper; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; import java.time.Year; import java.time.temporal.Temporal; -import static org.junit.Assert.*; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; public class TestYearSerialization { @@ -34,7 +36,7 @@ public class TestYearSerialization public void setUp() { this.mapper = new ObjectMapper(); - this.mapper.registerModule(new JSR310Module()); + this.mapper.registerModule(new JavaTimeModule()); } @After diff --git a/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestZoneIdKeySerialization.java b/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestZoneIdKeySerialization.java index aa28c2e8..b2280f2c 100644 --- a/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestZoneIdKeySerialization.java +++ b/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestZoneIdKeySerialization.java @@ -27,7 +27,7 @@ public class TestZoneIdKeySerialization { @Before public void setUp() { this.om = new ObjectMapper(); - om.registerModule(new JSR310Module()); + om.registerModule(new JavaTimeModule()); map = new HashMap<>(); } diff --git a/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestZoneIdSerialization.java b/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestZoneIdSerialization.java index 1cf3fda7..5619c740 100644 --- a/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestZoneIdSerialization.java +++ b/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestZoneIdSerialization.java @@ -16,15 +16,16 @@ package com.fasterxml.jackson.datatype.jsr310; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.time.ZoneId; + import com.fasterxml.jackson.databind.ObjectMapper; import org.junit.After; import org.junit.Before; import org.junit.Test; -import java.time.ZoneId; - -import static org.junit.Assert.*; - public class TestZoneIdSerialization { private ObjectMapper mapper; @@ -33,7 +34,7 @@ public class TestZoneIdSerialization public void setUp() { this.mapper = new ObjectMapper(); - this.mapper.registerModule(new JSR310Module()); + this.mapper.registerModule(new JavaTimeModule()); } @After diff --git a/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestZoneOffsetKeySerialization.java b/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestZoneOffsetKeySerialization.java index cff25bf0..7a00e15d 100644 --- a/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestZoneOffsetKeySerialization.java +++ b/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestZoneOffsetKeySerialization.java @@ -25,7 +25,7 @@ public class TestZoneOffsetKeySerialization { @Before public void setUp() { this.om = new ObjectMapper(); - om.registerModule(new JSR310Module()); + om.registerModule(new JavaTimeModule()); map = new HashMap<>(); } diff --git a/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestZoneOffsetSerialization.java b/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestZoneOffsetSerialization.java index 08f9de89..5207dc45 100644 --- a/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestZoneOffsetSerialization.java +++ b/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestZoneOffsetSerialization.java @@ -16,15 +16,17 @@ package com.fasterxml.jackson.datatype.jsr310; -import com.fasterxml.jackson.databind.ObjectMapper; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; import java.time.ZoneId; import java.time.ZoneOffset; -import static org.junit.Assert.*; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; public class TestZoneOffsetSerialization { @@ -34,7 +36,7 @@ public class TestZoneOffsetSerialization public void setUp() { this.mapper = new ObjectMapper(); - this.mapper.registerModule(new JSR310Module()); + this.mapper.registerModule(new JavaTimeModule()); } @After diff --git a/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestZonedDateTimeKeySerialization.java b/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestZonedDateTimeKeySerialization.java index 02565d2d..520a106f 100644 --- a/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestZonedDateTimeKeySerialization.java +++ b/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestZonedDateTimeKeySerialization.java @@ -39,7 +39,7 @@ public class TestZonedDateTimeKeySerialization { @Before public void setUp() { this.om = new ObjectMapper(); - om.registerModule(new JSR310Module()); + om.registerModule(new JavaTimeModule()); map = new HashMap<>(); } diff --git a/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestZonedDateTimeSerialization.java b/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestZonedDateTimeSerialization.java index ab773d64..a9ab9e75 100644 --- a/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestZonedDateTimeSerialization.java +++ b/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestZonedDateTimeSerialization.java @@ -16,13 +16,9 @@ package com.fasterxml.jackson.datatype.jsr310; -import com.fasterxml.jackson.databind.DeserializationFeature; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.SerializationFeature; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; +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; @@ -33,7 +29,12 @@ import java.time.temporal.Temporal; import java.util.TimeZone; -import static org.junit.Assert.*; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; public class TestZonedDateTimeSerialization { @@ -55,7 +56,7 @@ public class TestZonedDateTimeSerialization public void setUp() { this.mapper = new ObjectMapper(); - this.mapper.registerModule(new JSR310Module()); + this.mapper.registerModule(new JavaTimeModule()); } @After @@ -279,7 +280,7 @@ public void testDeserializationAsFloat03WithoutTimeZone() throws Exception ZonedDateTime value = this.mapper.readValue( DecimalUtils.toDecimal(date.toEpochSecond(), date.getNano()), ZonedDateTime.class - ); + ); assertNotNull("The value should not be null.", value); assertIsEqual(date, value); @@ -294,7 +295,7 @@ public void testDeserializationAsFloat03WithTimeZone() throws Exception this.mapper.setTimeZone(TimeZone.getDefault()); ZonedDateTime value = this.mapper.readValue( DecimalUtils.toDecimal(date.toEpochSecond(), date.getNano()), ZonedDateTime.class - ); + ); assertNotNull("The value should not be null.", value); assertIsEqual(date, value); @@ -600,7 +601,7 @@ public void testDeserializationWithTypeInfo01WithoutTimeZone() throws Exception this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); Temporal value = this.mapper.readValue( "[\"" + ZonedDateTime.class.getName() + "\",123456789.183917322]", Temporal.class - ); + ); assertNotNull("The value should not be null.", value); assertTrue("The value should be an ZonedDateTime.", value instanceof ZonedDateTime); @@ -617,7 +618,7 @@ public void testDeserializationWithTypeInfo01WithTimeZone() throws Exception this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); Temporal value = this.mapper.readValue( "[\"" + ZonedDateTime.class.getName() + "\",123456789.183917322]", Temporal.class - ); + ); assertNotNull("The value should not be null.", value); assertTrue("The value should be an ZonedDateTime.", value instanceof ZonedDateTime); @@ -634,7 +635,7 @@ public void testDeserializationWithTypeInfo02WithoutTimeZone() throws Exception this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); Temporal value = this.mapper.readValue( "[\"" + ZonedDateTime.class.getName() + "\",123456789]", Temporal.class - ); + ); assertNotNull("The value should not be null.", value); assertTrue("The value should be an ZonedDateTime.", value instanceof ZonedDateTime); @@ -652,7 +653,7 @@ public void testDeserializationWithTypeInfo02WithTimeZone() throws Exception this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); Temporal value = this.mapper.readValue( "[\"" + ZonedDateTime.class.getName() + "\",123456789]", Temporal.class - ); + ); assertNotNull("The value should not be null.", value); assertTrue("The value should be an ZonedDateTime.", value instanceof ZonedDateTime); @@ -669,7 +670,7 @@ public void testDeserializationWithTypeInfo03WithoutTimeZone() throws Exception this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); Temporal value = this.mapper.readValue( "[\"" + ZonedDateTime.class.getName() + "\",123456789422]", Temporal.class - ); + ); assertNotNull("The value should not be null.", value); assertTrue("The value should be an ZonedDateTime.", value instanceof ZonedDateTime); @@ -687,7 +688,7 @@ public void testDeserializationWithTypeInfo03WithTimeZone() throws Exception this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); Temporal value = this.mapper.readValue( "[\"" + ZonedDateTime.class.getName() + "\",123456789422]", Temporal.class - ); + ); assertNotNull("The value should not be null.", value); assertTrue("The value should be an ZonedDateTime.", value instanceof ZonedDateTime); @@ -704,7 +705,7 @@ public void testDeserializationWithTypeInfo04WithoutTimeZone() throws Exception this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); Temporal value = this.mapper.readValue( "[\"" + ZonedDateTime.class.getName() + "\",\"" + FORMATTER.format(date) + "\"]", Temporal.class - ); + ); assertNotNull("The value should not be null.", value); assertTrue("The value should be an ZonedDateTime.", value instanceof ZonedDateTime); @@ -722,7 +723,7 @@ public void testDeserializationWithTypeInfo04WithTimeZone() throws Exception this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); Temporal value = this.mapper.readValue( "[\"" + ZonedDateTime.class.getName() + "\",\"" + FORMATTER.format(date) + "\"]", Temporal.class - ); + ); assertNotNull("The value should not be null.", value); assertTrue("The value should be an ZonedDateTime.", value instanceof ZonedDateTime); @@ -740,7 +741,7 @@ public void testDeserializationWithTypeInfo04WithTimeZoneTurnedOff() throws Exce this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); Temporal value = this.mapper.readValue( "[\"" + ZonedDateTime.class.getName() + "\",\"" + FORMATTER.format(date) + "\"]", Temporal.class - ); + ); assertNotNull("The value should not be null.", value); assertTrue("The value should be an ZonedDateTime.", value instanceof ZonedDateTime);