Skip to content

Commit 3d20752

Browse files
Fix #196 ZonedDateTime deserialization skipped @jsonformat features override if there were no other options defined on it (#198)
1 parent bc966f9 commit 3d20752

File tree

4 files changed

+28
-10
lines changed

4 files changed

+28
-10
lines changed

datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/deser/InstantDeserializer.java

+7-9
Original file line numberDiff line numberDiff line change
@@ -187,15 +187,13 @@ public JsonDeserializer<T> createContextual(DeserializationContext ctxt,
187187
{
188188
InstantDeserializer<T> deserializer =
189189
(InstantDeserializer<T>)super.createContextual(ctxt, property);
190-
if (deserializer != this) {
191-
JsonFormat.Value val = findFormatOverrides(ctxt, property, handledType());
192-
if (val != null) {
193-
deserializer = new InstantDeserializer<>(deserializer, val.getFeature(JsonFormat.Feature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE));
194-
if (val.hasLenient()) {
195-
Boolean leniency = val.getLenient();
196-
if (leniency != null) {
197-
deserializer = deserializer.withLeniency(leniency);
198-
}
190+
JsonFormat.Value val = findFormatOverrides(ctxt, property, handledType());
191+
if (val != null) {
192+
deserializer = new InstantDeserializer<>(deserializer, val.getFeature(JsonFormat.Feature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE));
193+
if (val.hasLenient()) {
194+
Boolean leniency = val.getLenient();
195+
if (leniency != null) {
196+
deserializer = deserializer.withLeniency(leniency);
199197
}
200198
}
201199
}

datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ModuleTestBase.java

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.time.ZoneId;
44
import java.util.Arrays;
55
import java.util.Collections;
6+
import java.util.Locale;
67
import java.util.Map;
78

89
import com.fasterxml.jackson.databind.JavaType;
@@ -40,6 +41,7 @@ protected static ObjectMapper newMapper() {
4041

4142
protected static JsonMapper.Builder mapperBuilder() {
4243
return JsonMapper.builder()
44+
.defaultLocale(Locale.ENGLISH)
4345
.addModule(new JavaTimeModule());
4446
}
4547

datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/ZonedDateTimeDeserTest.java

+17
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import java.io.IOException;
1616
import java.time.ZoneId;
17+
import java.time.ZoneOffset;
1718
import java.time.ZonedDateTime;
1819
import java.time.format.DateTimeParseException;
1920
import java.util.Map;
@@ -27,6 +28,11 @@ public class ZonedDateTimeDeserTest extends ModuleTestBase
2728
private final ObjectReader READER = newMapper().readerFor(ZonedDateTime.class);
2829
private final TypeReference<Map<String, ZonedDateTime>> MAP_TYPE_REF = new TypeReference<Map<String, ZonedDateTime>>() { };
2930

31+
static class WrapperWithFeatures {
32+
@JsonFormat(without = JsonFormat.Feature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE)
33+
public ZonedDateTime value;
34+
}
35+
3036
@Test
3137
public void testDeserializationAsString01() throws Exception
3238
{
@@ -102,6 +108,17 @@ public void testDeserializationAsEmptyArrayEnabled() throws Throwable
102108
assertNull(value);
103109
}
104110

111+
@Test
112+
public void testDeserializationWithZonePreserved() throws Throwable
113+
{
114+
WrapperWithFeatures wrapper = newMapper()
115+
.readerFor(WrapperWithFeatures.class)
116+
.readValue("{\"value\":\"2000-01-01T12:00+01:00\"}");
117+
assertEquals("Timezone should be preserved.",
118+
ZonedDateTime.of(2000, 1, 1, 12, 0, 0 ,0, ZoneOffset.ofHours(1)),
119+
wrapper.value);
120+
}
121+
105122
/*
106123
/**********************************************************
107124
/* Tests for empty string handling

datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/ZonedDateTimeSerTest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.time.temporal.ChronoField;
2424
import java.time.temporal.ChronoUnit;
2525
import java.time.temporal.Temporal;
26+
import java.util.Locale;
2627
import java.util.TimeZone;
2728

2829
import com.fasterxml.jackson.annotation.JsonFormat;
@@ -106,7 +107,7 @@ public void testSerializationAsTimestamp01NegativeSeconds() throws Exception
106107
public void testSerializationAsTimestamp01NegativeSecondsWithDefaults() throws Exception
107108
{
108109
// test for Issue #69 using default mapper config
109-
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MMM dd yyyy HH:mm:ss.SSS zzz");
110+
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MMM dd yyyy HH:mm:ss.SSS zzz", Locale.ENGLISH);
110111
ZonedDateTime original = ZonedDateTime.parse("Apr 13 1969 05:05:38.599 UTC", dtf);
111112
String serialized = MAPPER.writeValueAsString(original);
112113
ZonedDateTime deserialized = MAPPER.readValue(serialized, ZonedDateTime.class);

0 commit comments

Comments
 (0)