Skip to content

Commit

Permalink
Fix #296: add null checks for type serialization for 7 types
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jan 3, 2024
1 parent 3434dfc commit 6470e1f
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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)));
}
}
5 changes: 5 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6470e1f

Please sign in to comment.