Skip to content
This repository has been archived by the owner on Nov 7, 2019. It is now read-only.

Commit

Permalink
Merge pull request #56 from sandermak/master
Browse files Browse the repository at this point in the history
Handle JSON serialized Dates from JavaScript in LocalDateDeserializer
  • Loading branch information
cowtowncoder committed Jan 18, 2016
2 parents 34f2c20 + 62c50cf commit e74927b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
package com.fasterxml.jackson.datatype.jsr310.deser;

import java.io.IOException;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;

import com.fasterxml.jackson.core.*;
Expand Down Expand Up @@ -64,9 +67,14 @@ public LocalDate deserialize(JsonParser parser, DeserializationContext context)
// if we are using default formatter
DateTimeFormatter format = _formatter;
if (format == DEFAULT_FORMATTER) {
if (string.contains("T")) {
return LocalDate.parse(string, DateTimeFormatter.ISO_LOCAL_DATE_TIME);
}
// JavaScript by default includes time in JSON serialized Dates (UTC/ISO instant format).
if (string.length() > 10 && string.charAt(10) == 'T') {
if (string.endsWith("Z")) {
return LocalDateTime.ofInstant(Instant.parse(string), ZoneOffset.UTC).toLocalDate();
} else {
return LocalDate.parse(string, DateTimeFormatter.ISO_LOCAL_DATE_TIME);
}
}
}
return LocalDate.parse(string, format);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@
import static org.junit.Assert.assertTrue;

import java.time.format.DateTimeParseException;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.ZoneOffset;
import java.time.temporal.Temporal;

import com.fasterxml.jackson.annotation.JsonFormat;
Expand Down Expand Up @@ -171,6 +173,16 @@ public void testDeserializationAsString04() throws Exception
this.MAPPER.readValue("\"2015-06-19TShouldNotParse\"", LocalDate.class);
}

@Test
public void testDeserializationAsString05() throws Exception
{
Instant instant = Instant.now();
LocalDate value = MAPPER.readValue('"' + instant.toString() + '"', LocalDate.class);

assertNotNull("The value should not be null.", value);
assertEquals("The value is not correct.", LocalDateTime.ofInstant(instant, ZoneOffset.UTC).toLocalDate(), value);
}

@Test
public void testDeserializationWithTypeInfo01() throws Exception
{
Expand Down

0 comments on commit e74927b

Please sign in to comment.