Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Time zone aware lenient parsing #343

Closed
wants to merge 1 commit into from
Closed
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,6 +18,7 @@

import java.io.IOException;
import java.time.DateTimeException;
import java.time.Instant;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

Expand Down Expand Up @@ -161,8 +162,7 @@ protected LocalDate _fromString(JsonParser p, DeserializationContext ctxt,
if (string.length() > 10 && string.charAt(10) == 'T') {
if (isLenient()) {
if (string.endsWith("Z")) {
return LocalDate.parse(string.substring(0, string.length() - 1),
DateTimeFormatter.ISO_LOCAL_DATE_TIME);
return Instant.parse(string).atZone(ctxt.getTimeZone().toZoneId()).toLocalDate();
}
return LocalDate.parse(string, DateTimeFormatter.ISO_LOCAL_DATE_TIME);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.io.IOException;
import java.time.DateTimeException;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Objects;
Expand Down Expand Up @@ -195,13 +196,10 @@ protected LocalDateTime _fromString(JsonParser p, DeserializationContext ctxt,
if (_formatter == DEFAULT_FORMATTER) {
// ... only allow iff lenient mode enabled since
// JavaScript by default includes time and zone in JSON serialized Dates (UTC/ISO instant format).
// And if so, do NOT use zoned date parsing as that can easily produce
// incorrect answer.
if (string.length() > 10 && string.charAt(10) == 'T') {
if (string.endsWith("Z")) {
if (isLenient()) {
return LocalDateTime.parse(string.substring(0, string.length()-1),
_formatter);
return Instant.parse(string).atZone(ctxt.getTimeZone().toZoneId()).toLocalDateTime();
}
JavaType t = getValueType(ctxt);
return (LocalDateTime) ctxt.handleWeirdStringValue(t.getRawClass(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.time.format.DateTimeParseException;
import java.time.temporal.Temporal;
import java.util.Map;
import java.util.TimeZone;

import com.fasterxml.jackson.annotation.OptBoolean;
import com.fasterxml.jackson.databind.cfg.CoercionAction;
Expand Down Expand Up @@ -153,6 +154,15 @@ public void testDeserializationAsString03() throws Exception
value);
}

@Test
public void testDeserializationAsString04() throws Exception
{
ObjectReader reader = READER.with(TimeZone.getTimeZone(Z_BUDAPEST));
Instant instant = Instant.parse("2024-07-21T22:00:00Z");
LocalDate value = reader.readValue('"' + instant.toString() + '"');
assertEquals("The value is not correct.", LocalDate.parse("2024-07-22"), value);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is wrong and against the semantics of LocalDate.

}

@Test
public void testBadDeserializationAsString01() throws Throwable
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,14 +261,14 @@ public void testAllowZuluIfLenient() throws Exception
// First, defaults:
assertEquals("The value is not correct.", EXP, r.readValue(input));

// but ensure that global timezone setting doesn't matter
// and ensure that global timezone setting matter
LocalDateTime value = r.with(TimeZone.getTimeZone(Z_CHICAGO))
.readValue(input);
assertEquals("The value is not correct.", EXP, value);
assertEquals("The value is not correct.", EXP.minusHours(5), value);

value = r.with(TimeZone.getTimeZone(Z_BUDAPEST))
.readValue(input);
assertEquals("The value is not correct.", EXP, value);
assertEquals("The value is not correct.", EXP.plusHours(2), value);
}

// [modules-java#94]: "Z" offset not allowed if strict mode
Expand Down