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 LocalDateTimeDeserializer #68

Merged
merged 1 commit into from
Apr 6, 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 @@ -18,7 +18,9 @@

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

import com.fasterxml.jackson.core.JsonParser;
Expand All @@ -40,10 +42,12 @@ public class LocalDateTimeDeserializer
{
private static final long serialVersionUID = 1L;

private static final DateTimeFormatter DEFAULT_FORMATTER = DateTimeFormatter.ISO_LOCAL_DATE_TIME;

public static final LocalDateTimeDeserializer INSTANCE = new LocalDateTimeDeserializer();

private LocalDateTimeDeserializer() {
this(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
this(DEFAULT_FORMATTER);
}

public LocalDateTimeDeserializer(DateTimeFormatter formatter) {
Expand All @@ -63,7 +67,19 @@ public LocalDateTime deserialize(JsonParser parser, DeserializationContext conte
if (string.length() == 0) {
return null;
}

try {
if (_formatter == DEFAULT_FORMATTER) {
// JavaScript by default includes time and zone 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);
} else {
return LocalDateTime.parse(string, DEFAULT_FORMATTER);
}
}
}

return LocalDateTime.parse(string, _formatter);
} catch (DateTimeException e) {
_rethrowDateTimeException(parser, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
import org.junit.Before;
import org.junit.Test;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.ZoneOffset;
import java.time.temporal.Temporal;
import java.util.Calendar;
import java.util.Date;
Expand Down Expand Up @@ -314,13 +316,23 @@ public void testDeserializationAsString03() throws Exception
assertEquals("The value is not correct.", time, value);
}

@Test
public void testDeserializationAsString04() throws Exception
{
Instant instant = Instant.now();
LocalDateTime value = mapper.readValue('"' + instant.toString() + '"', LocalDateTime.class);

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

@Test
public void testDeserializationWithTypeInfo01() throws Exception
{
LocalDateTime time = LocalDateTime.of(2005, Month.NOVEMBER, 5, 22, 31, 5, 829837);

final ObjectMapper m = newMapper().addMixIn(Temporal.class, MockObjectConfiguration.class);

m.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, true);
Temporal value = m.readValue(
"[\"" + LocalDateTime.class.getName() + "\",[2005,11,5,22,31,5,829837]]", Temporal.class
Expand Down