Skip to content

Commit 9e1ba80

Browse files
authored
Support more formats in ZonedDateTimeKeyDeserializer (#305)
1 parent 67cedd6 commit 9e1ba80

File tree

3 files changed

+83
-3
lines changed

3 files changed

+83
-3
lines changed

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import java.io.IOException;
44
import java.time.DateTimeException;
55
import java.time.ZonedDateTime;
6-
import java.time.format.DateTimeFormatter;
76

87
import com.fasterxml.jackson.databind.DeserializationContext;
98

@@ -17,9 +16,9 @@ private ZonedDateTimeKeyDeserializer() {
1716

1817
@Override
1918
protected ZonedDateTime deserialize(String key, DeserializationContext ctxt) throws IOException {
20-
// not serializing timezone data yet
2119
try {
22-
return ZonedDateTime.parse(key, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
20+
// Not supplying a formatter allows the use of all supported formats
21+
return ZonedDateTime.parse(key);
2322
} catch (DateTimeException e) {
2423
return _handleDateTimeException(ctxt, ZonedDateTime.class, e, key);
2524
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.fasterxml.jackson.datatype.jsr310.deser.key;
2+
3+
import com.fasterxml.jackson.core.type.TypeReference;
4+
5+
import org.junit.Test;
6+
7+
import com.fasterxml.jackson.databind.ObjectMapper;
8+
import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase;
9+
10+
import java.time.ZonedDateTime;
11+
import java.util.Map;
12+
13+
import static org.junit.Assert.assertEquals;
14+
import static org.junit.Assume.*;
15+
16+
// for [modules-java8#306]
17+
public class ZonedDateTimeKeyDeserializerTest
18+
extends ModuleTestBase
19+
{
20+
private final ObjectMapper MAPPER = newMapper();
21+
private final TypeReference<Map<ZonedDateTime, String>> MAP_TYPE_REF
22+
= new TypeReference<Map<ZonedDateTime, String>>() {};
23+
24+
@Test
25+
public void Instant_style_can_be_deserialized() throws Exception {
26+
Map<ZonedDateTime, String> map = MAPPER.readValue(getMap("2015-07-24T12:23:34.184Z"),
27+
MAP_TYPE_REF);
28+
Map.Entry<ZonedDateTime, String> entry = map.entrySet().iterator().next();
29+
assertEquals("2015-07-24T12:23:34.184Z", entry.getKey().toString());
30+
}
31+
32+
@Test
33+
public void ZonedDateTime_with_zone_name_can_be_deserialized() throws Exception {
34+
Map<ZonedDateTime, String> map = MAPPER.readValue(getMap("2015-07-24T12:23:34.184Z[UTC]"),
35+
MAP_TYPE_REF);
36+
Map.Entry<ZonedDateTime, String> entry = map.entrySet().iterator().next();
37+
assertEquals("2015-07-24T12:23:34.184Z[UTC]", entry.getKey().toString());
38+
}
39+
40+
@Test
41+
public void ZonedDateTime_with_place_name_can_be_deserialized() throws Exception {
42+
assumeFalse(System.getProperty("java.version").startsWith("1.8"));
43+
44+
Map<ZonedDateTime, String> map = MAPPER.readValue(getMap("2015-07-24T12:23:34.184Z[Europe/London]"),
45+
MAP_TYPE_REF);
46+
Map.Entry<ZonedDateTime, String> entry = map.entrySet().iterator().next();
47+
assertEquals("2015-07-24T13:23:34.184+01:00[Europe/London]", entry.getKey().toString());
48+
}
49+
50+
@Test
51+
public void ZonedDateTime_with_place_name_can_be_deserialized_Java_8() throws Exception {
52+
// Java 8 parses this format incorrectly due to https://bugs.openjdk.org/browse/JDK-8066982
53+
assumeTrue(System.getProperty("java.version").startsWith("1.8"));
54+
55+
Map<ZonedDateTime, String> map = MAPPER.readValue(getMap("2015-07-24T12:23:34.184Z[Europe/London]"),
56+
MAP_TYPE_REF);
57+
Map.Entry<ZonedDateTime, String> entry = map.entrySet().iterator().next();
58+
assertEquals("2015-07-24T12:23:34.184+01:00[Europe/London]", entry.getKey().toString());
59+
}
60+
61+
@Test
62+
public void ZonedDateTime_with_offset_can_be_deserialized() throws Exception {
63+
Map<ZonedDateTime, String> map = MAPPER.readValue(getMap("2015-07-24T12:23:34.184+02:00"),
64+
MAP_TYPE_REF);
65+
Map.Entry<ZonedDateTime, String> entry = map.entrySet().iterator().next();
66+
assertEquals("2015-07-24T12:23:34.184+02:00", entry.getKey().toString());
67+
}
68+
69+
private static String getMap(String input) {
70+
return "{\"" + input + "\": \"This is a string\"}";
71+
}
72+
}

release-notes/VERSION-2.x

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@ Modules:
88
=== Releases ===
99
------------------------------------------------------------------------
1010

11+
2.18.0 (not yet released)
12+
13+
No changes since 2.17
14+
15+
2.17.1 (not yet released)
16+
17+
#306: Only `DateTimeFormatter.ISO_OFFSET_DATE_TIME` accepted by `ZonedDateTimeKeyDeserializer`
18+
(fix contributed by @caluml)
19+
1120
2.17.0 (12-Mar-2024)
1221

1322
#274: Deserializing `java.time.Month` from an int causes an off-by-one

0 commit comments

Comments
 (0)