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

Handle JSON serialized Dates from JavaScript in LocalDateDeserializer #56

Merged
merged 1 commit into from
Jan 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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