Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Support more formats in ZonedDateTimeKeyDeserializer #305

Merged
merged 5 commits into from
Mar 27, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
cowtowncoder marked this conversation as resolved.
Show resolved Hide resolved
} 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