Skip to content

Commit

Permalink
Solve NPE part of #296, add test
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jan 3, 2024
1 parent 40750f0 commit 3434dfc
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,12 @@ public void serializeWithType(LocalDate value, JsonGenerator g,
WritableTypeId typeIdDef = typeSer.writeTypePrefix(g,
typeSer.typeId(value, serializationShape(provider)));
// need to write out to avoid double-writing array markers
switch (typeIdDef.valueShape) {
case START_ARRAY:
JsonToken shape = (typeIdDef == null) ? null : typeIdDef.valueShape;
if (shape == JsonToken.START_ARRAY) {
_serializeAsArrayContents(value, g, provider);
break;
case VALUE_NUMBER_INT:
} else if (shape == JsonToken.VALUE_NUMBER_INT) {
g.writeNumber(value.toEpochDay());
break;
default:
} else {
g.writeString((_formatter == null) ? value.toString() : value.format(_formatter));
}
typeSer.writeTypeSuffix(g, typeIdDef);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public void serializeWithType(LocalDateTime value, JsonGenerator g, SerializerPr
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 {
DateTimeFormatter dtf = _formatter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ public void serializeWithType(LocalTime 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 {
DateTimeFormatter dtf = _formatter;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.fasterxml.jackson.datatype.jsr310.misc;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;

import org.junit.Assert;
import org.junit.Test;

import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase;

// for [modules-java8#296]: problem with `JsonTypeInfo.Id.DEDUCTION`
public class DeductionTypeSerialization296Test extends ModuleTestBase
{
static class Wrapper {
@JsonTypeInfo(use = JsonTypeInfo.Id.DEDUCTION)
public Object value;

public Wrapper(Object value) {
this.value = value;
}
}

private final ObjectMapper MAPPER = mapperBuilder()
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.build();

@Test
public void testLocalDate() throws Exception
{
LocalDate date = LocalDate.of(1986, Month.JANUARY, 17);
Assert.assertEquals(a2q("{'value':'1986-01-17'}"),
MAPPER.writeValueAsString(new Wrapper(date)));
}

@Test
public void testLocalDateTime() throws Exception
{
LocalDateTime datetime = LocalDateTime.of(2013, Month.AUGUST, 21, 9, 22, 0, 57);
Assert.assertEquals(a2q("{'value':'2013-08-21T09:22:00.000000057'}"),
MAPPER.writeValueAsString(new Wrapper(datetime)));
}

@Test
public void testLocalTime() throws Exception
{
LocalTime time = LocalTime.of(9, 22, 57);
Assert.assertEquals(a2q("{'value':'09:22:57'}"),
MAPPER.writeValueAsString(new Wrapper(time)));
}
}

0 comments on commit 3434dfc

Please sign in to comment.