Skip to content

Commit

Permalink
Support more formats in ZonedDateTimeKeyDeserializer (#305)
Browse files Browse the repository at this point in the history
  • Loading branch information
caluml authored Mar 27, 2024
1 parent 67cedd6 commit 9e1ba80
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.io.IOException;
import java.time.DateTimeException;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

import com.fasterxml.jackson.databind.DeserializationContext;

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

@Override
protected ZonedDateTime deserialize(String key, DeserializationContext ctxt) throws IOException {
// not serializing timezone data yet
try {
return ZonedDateTime.parse(key, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
// Not supplying a formatter allows the use of all supported formats
return ZonedDateTime.parse(key);
} catch (DateTimeException e) {
return _handleDateTimeException(ctxt, ZonedDateTime.class, e, key);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.fasterxml.jackson.datatype.jsr310.deser.key;

import com.fasterxml.jackson.core.type.TypeReference;

import org.junit.Test;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase;

import java.time.ZonedDateTime;
import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.junit.Assume.*;

// for [modules-java8#306]
public class ZonedDateTimeKeyDeserializerTest
extends ModuleTestBase
{
private final ObjectMapper MAPPER = newMapper();
private final TypeReference<Map<ZonedDateTime, String>> MAP_TYPE_REF
= new TypeReference<Map<ZonedDateTime, String>>() {};

@Test
public void Instant_style_can_be_deserialized() throws Exception {
Map<ZonedDateTime, String> map = MAPPER.readValue(getMap("2015-07-24T12:23:34.184Z"),
MAP_TYPE_REF);
Map.Entry<ZonedDateTime, String> entry = map.entrySet().iterator().next();
assertEquals("2015-07-24T12:23:34.184Z", entry.getKey().toString());
}

@Test
public void ZonedDateTime_with_zone_name_can_be_deserialized() throws Exception {
Map<ZonedDateTime, String> map = MAPPER.readValue(getMap("2015-07-24T12:23:34.184Z[UTC]"),
MAP_TYPE_REF);
Map.Entry<ZonedDateTime, String> entry = map.entrySet().iterator().next();
assertEquals("2015-07-24T12:23:34.184Z[UTC]", entry.getKey().toString());
}

@Test
public void ZonedDateTime_with_place_name_can_be_deserialized() throws Exception {
assumeFalse(System.getProperty("java.version").startsWith("1.8"));

Map<ZonedDateTime, String> map = MAPPER.readValue(getMap("2015-07-24T12:23:34.184Z[Europe/London]"),
MAP_TYPE_REF);
Map.Entry<ZonedDateTime, String> entry = map.entrySet().iterator().next();
assertEquals("2015-07-24T13:23:34.184+01:00[Europe/London]", entry.getKey().toString());
}

@Test
public void ZonedDateTime_with_place_name_can_be_deserialized_Java_8() throws Exception {
// Java 8 parses this format incorrectly due to https://bugs.openjdk.org/browse/JDK-8066982
assumeTrue(System.getProperty("java.version").startsWith("1.8"));

Map<ZonedDateTime, String> map = MAPPER.readValue(getMap("2015-07-24T12:23:34.184Z[Europe/London]"),
MAP_TYPE_REF);
Map.Entry<ZonedDateTime, String> entry = map.entrySet().iterator().next();
assertEquals("2015-07-24T12:23:34.184+01:00[Europe/London]", entry.getKey().toString());
}

@Test
public void ZonedDateTime_with_offset_can_be_deserialized() throws Exception {
Map<ZonedDateTime, String> map = MAPPER.readValue(getMap("2015-07-24T12:23:34.184+02:00"),
MAP_TYPE_REF);
Map.Entry<ZonedDateTime, String> entry = map.entrySet().iterator().next();
assertEquals("2015-07-24T12:23:34.184+02:00", entry.getKey().toString());
}

private static String getMap(String input) {
return "{\"" + input + "\": \"This is a string\"}";
}
}
9 changes: 9 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ Modules:
=== Releases ===
------------------------------------------------------------------------

2.18.0 (not yet released)

No changes since 2.17

2.17.1 (not yet released)

#306: Only `DateTimeFormatter.ISO_OFFSET_DATE_TIME` accepted by `ZonedDateTimeKeyDeserializer`
(fix contributed by @caluml)

2.17.0 (12-Mar-2024)

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

0 comments on commit 9e1ba80

Please sign in to comment.