Skip to content

Commit

Permalink
Fixes #148: Deserializes Dates, Times, and DateTimes using DateTimeFo…
Browse files Browse the repository at this point in the history
…rmatter with ResolverStyle.STRICT when JsonFormat lenient = false (#151)
  • Loading branch information
samwill authored and cowtowncoder committed Nov 2, 2019
1 parent 2ef7c25 commit 22acd59
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.IOException;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.ResolverStyle;
import java.util.Locale;

import com.fasterxml.jackson.annotation.JsonFormat;
Expand Down Expand Up @@ -137,6 +138,11 @@ public JsonDeserializer<?> createContextual(DeserializationContext ctxt,
} else {
df = builder.toFormatter(locale);
}

if (format.hasLenient() && !format.isLenient()) {
df = df.withResolverStyle(ResolverStyle.STRICT);
}

//Issue #69: For instant serializers/deserializers we need to configure the formatter with
//a time zone picked up from JsonFormat annotation, otherwise serialization might not work
if (format.hasTimeZone()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import java.time.temporal.Temporal;
import java.util.Map;

import com.fasterxml.jackson.annotation.OptBoolean;
import com.fasterxml.jackson.databind.exc.InvalidFormatException;
import org.junit.Test;

import com.fasterxml.jackson.annotation.JsonFormat;
Expand Down Expand Up @@ -54,6 +56,15 @@ public ShapeWrapper() { }
public ShapeWrapper(LocalDate v) { date = v; }
}

final static class StrictWrapper {
@JsonFormat(pattern="yyyy-MM-dd",
lenient = OptBoolean.FALSE)
public LocalDate value;

public StrictWrapper() { }
public StrictWrapper(LocalDate v) { value = v; }
}

/*
/**********************************************************
/* Deserialization from Int array representation
Expand Down Expand Up @@ -295,6 +306,20 @@ public void testCustomFormat() throws Exception
assertEquals(28, date.getDayOfMonth());
}


/*
/**********************************************************
/* Strict Custom format
/**********************************************************
*/

// for [modules-java8#148]
@Test(expected = InvalidFormatException.class)
public void testStrictCustomFormat() throws Exception
{
StrictWrapper w = MAPPER.readValue("{\"value\":\"2019-11-31\"}", StrictWrapper.class);
}

/*
/**********************************************************************
/* Case-insensitive tests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonFormat.Feature;
import com.fasterxml.jackson.annotation.JsonFormat.Value;
import com.fasterxml.jackson.annotation.OptBoolean;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.JsonToken;
Expand Down Expand Up @@ -52,6 +53,15 @@ public class LocalDateTimeDeserTest
private final static ObjectReader READER = MAPPER.readerFor(LocalDateTime.class);
private final TypeReference<Map<String, LocalDateTime>> MAP_TYPE_REF = new TypeReference<Map<String, LocalDateTime>>() { };

final static class StrictWrapper {
@JsonFormat(pattern="yyyy-MM-dd HH:mm",
lenient = OptBoolean.FALSE)
public LocalDateTime value;

public StrictWrapper() { }
public StrictWrapper(LocalDateTime v) { value = v; }
}

/*
/**********************************************************
/* Tests for deserializing from int array
Expand Down Expand Up @@ -458,6 +468,32 @@ public void testDeserializationCaseInsensitiveDisabled_InvalidDate() throws Thro
}
}

/*
/**********************************************************************
/* Strict JsonFormat tests
/**********************************************************************
*/

// [modules-java8#148]: handle strict deserializaiton for date/time
@Test(expected = InvalidFormatException.class)
public void testStrictCustomFormatInvalidDate() throws Exception
{
StrictWrapper w = MAPPER.readValue("{\"value\":\"2019-11-31 15:45\"}", StrictWrapper.class);
}

@Test(expected = InvalidFormatException.class)
public void testStrictCustomFormatInvalidTime() throws Exception
{
StrictWrapper w = MAPPER.readValue("{\"value\":\"2019-11-30 25:45\"}", StrictWrapper.class);
}

@Test(expected = InvalidFormatException.class)
public void testStrictCustomFormatInvalidDateAndTime() throws Exception
{
StrictWrapper w = MAPPER.readValue("{\"value\":\"2019-11-31 25:45\"}", StrictWrapper.class);
}


private void expectSuccess(ObjectReader reader, Object exp, String json) throws IOException {
final LocalDateTime value = reader.readValue(aposToQuotes(json));
assertNotNull("The value should not be null.", value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@
import static org.junit.Assert.fail;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.OptBoolean;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.databind.exc.InvalidFormatException;
import com.fasterxml.jackson.databind.exc.MismatchedInputException;
import com.fasterxml.jackson.datatype.jsr310.MockObjectConfiguration;
import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase;
Expand All @@ -44,9 +46,19 @@

public class LocalTimeDeserTest extends ModuleTestBase
{
private ObjectReader reader = newMapper().readerFor(LocalTime.class);
private final static ObjectMapper MAPPER = newMapper();
private ObjectReader reader = MAPPER.readerFor(LocalTime.class);
private final TypeReference<Map<String, LocalTime>> MAP_TYPE_REF = new TypeReference<Map<String, LocalTime>>() { };

final static class StrictWrapper {
@JsonFormat(pattern="HH:mm",
lenient = OptBoolean.FALSE)
public LocalDateTime value;

public StrictWrapper() { }
public StrictWrapper(LocalDateTime v) { value = v; }
}

@Test
public void testDeserializationAsTimestamp01() throws Exception
{
Expand Down Expand Up @@ -258,6 +270,20 @@ public void testStrictDeserializeFromEmptyString() throws Exception {
objectReader.readValue(valueFromEmptyStr);
}

/*
/**********************************************************************
/* Strict JsonFormat tests
/**********************************************************************
*/

// [modules-java8#148]: handle strict deserializaiton for date/time

@Test(expected = InvalidFormatException.class)
public void testStrictCustomFormatInvalidTime() throws Exception
{
StrictWrapper w = MAPPER.readValue("{\"value\":\"25:45\"}", StrictWrapper.class);
}

private void expectFailure(String aposJson) throws Throwable {
try {
reader.readValue(aposToQuotes(aposJson));
Expand Down

0 comments on commit 22acd59

Please sign in to comment.