diff --git a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/MonthDaySerializer.java b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/MonthDaySerializer.java index 10fc0b0b..46f7aa35 100644 --- a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/MonthDaySerializer.java +++ b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/MonthDaySerializer.java @@ -81,7 +81,8 @@ public void serializeWithType(MonthDay value, JsonGenerator g, WritableTypeId typeIdDef = typeSer.writeTypePrefix(g, typeSer.typeId(value, serializationShape(provider))); // need to write out to avoid double-writing array markers - if (typeIdDef.valueShape == JsonToken.START_ARRAY) { + if ((typeIdDef != null) + && typeIdDef.valueShape == JsonToken.START_ARRAY) { _serializeAsArrayContents(value, g, provider); } else { g.writeString((_formatter == null) ? value.toString() : value.format(_formatter)); diff --git a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/OffsetTimeSerializer.java b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/OffsetTimeSerializer.java index 1c78d462..f1dec8b9 100644 --- a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/OffsetTimeSerializer.java +++ b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/OffsetTimeSerializer.java @@ -79,7 +79,8 @@ public void serializeWithType(OffsetTime value, JsonGenerator g, SerializerProvi WritableTypeId typeIdDef = typeSer.writeTypePrefix(g, typeSer.typeId(value, serializationShape(provider))); // need to write out to avoid double-writing array markers - if (typeIdDef.valueShape == JsonToken.START_ARRAY) { + if ((typeIdDef != null) + && typeIdDef.valueShape == JsonToken.START_ARRAY) { _serializeAsArrayContents(value, g, provider); } else { String str = (_formatter == null) ? value.toString() : value.format(_formatter); diff --git a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/YearMonthSerializer.java b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/YearMonthSerializer.java index bca1adcb..fcafb737 100644 --- a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/YearMonthSerializer.java +++ b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/YearMonthSerializer.java @@ -82,7 +82,8 @@ public void serializeWithType(YearMonth value, JsonGenerator g, WritableTypeId typeIdDef = typeSer.writeTypePrefix(g, typeSer.typeId(value, serializationShape(provider))); // need to write out to avoid double-writing array markers - if (typeIdDef.valueShape == JsonToken.START_ARRAY) { + if ((typeIdDef != null) + && typeIdDef.valueShape == JsonToken.START_ARRAY) { _serializeAsArrayContents(value, g, provider); } else { g.writeString((_formatter == null) ? value.toString() : value.format(_formatter)); diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/misc/DeductionTypeSerialization296Test.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/misc/DeductionTypeSerialization296Test.java index cce9cb2b..08ce9d08 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/misc/DeductionTypeSerialization296Test.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/misc/DeductionTypeSerialization296Test.java @@ -1,9 +1,6 @@ package com.fasterxml.jackson.datatype.jsr310.misc; -import java.time.LocalDate; -import java.time.LocalDateTime; -import java.time.LocalTime; -import java.time.Month; +import java.time.*; import org.junit.Assert; import org.junit.Test; @@ -52,4 +49,36 @@ public void testLocalTime() throws Exception Assert.assertEquals(a2q("{'value':'09:22:57'}"), MAPPER.writeValueAsString(new Wrapper(time))); } + + @Test + public void testMonthDate() throws Exception + { + MonthDay date = MonthDay.of(Month.JANUARY, 17); + Assert.assertEquals(a2q("{'value':'--01-17'}"), + MAPPER.writeValueAsString(new Wrapper(date))); + } + + @Test + public void testOffsetTime() throws Exception + { + OffsetTime time = OffsetTime.of(15, 43, 0, 0, ZoneOffset.of("+0300")); + Assert.assertEquals(a2q("{'value':'15:43+03:00'}"), + MAPPER.writeValueAsString(new Wrapper(time))); + } + + @Test + public void testYearMonth() throws Exception + { + YearMonth date = YearMonth.of(1986, Month.JANUARY); + Assert.assertEquals(a2q("{'value':'1986-01'}"), + MAPPER.writeValueAsString(new Wrapper(date))); + } + + @Test + public void testZoneId() throws Exception + { + ZoneId zone = ZoneId.of("America/Denver"); + Assert.assertEquals(a2q("{'value':'America/Denver'}"), + MAPPER.writeValueAsString(new Wrapper(zone))); + } } diff --git a/release-notes/VERSION-2.x b/release-notes/VERSION-2.x index 88ecc1fb..f0977b3f 100644 --- a/release-notes/VERSION-2.x +++ b/release-notes/VERSION-2.x @@ -8,6 +8,11 @@ Modules: === Releases === ------------------------------------------------------------------------ +2.16.2 (not yet released) + +#296: NPE when serializing a `LocalDate` or `LocalDateTime` using `AsDeductionTypeSerializer` + (reported by @mike-reynolds-savient) + 2.16.1 (24-Dec-2023) #286: Breaking change in `InstantDeserializer API between 2.15 and 2.16