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 1 commit
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,8 @@ 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);
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,70 @@
package com.fasterxml.jackson.datatype.jsr310.deser.key;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.junit.BeforeClass;
import org.junit.Test;

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

import static org.junit.Assert.assertEquals;

public class ZonedDateTimeKeyDeserializerTest {

private static ObjectMapper objectMapper;
private final TypeReference<Map<ZonedDateTime, String>> MAP_TYPE_REF = new TypeReference<Map<ZonedDateTime, String>>() {
};

@BeforeClass
public static void beforeClass() {
objectMapper = new ObjectMapper();
objectMapper.registerModule(new JavaTimeModule());
}

@Test
public void Instant_style_can_be_deserialized() throws JsonProcessingException {
Copy link
Member

Choose a reason for hiding this comment

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

could you change this to throws Exception -- just so it's easier to merge into master branch (Jackson 3.0) where JsonProcessingException is replaced by StreamReadException.
(and same for other instances)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

String input = "2015-07-24T12:23:34.184Z";

Map<ZonedDateTime, String> map = objectMapper.readValue(getMap(input), 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 JsonProcessingException {
String input = "2015-07-24T12:23:34.184Z[UTC]";

Map<ZonedDateTime, String> map = objectMapper.readValue(getMap(input), 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 JsonProcessingException {
String input = "2015-07-24T12:23:34.184Z[Europe/London]";

Map<ZonedDateTime, String> map = objectMapper.readValue(getMap(input), 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_offset_can_be_deserialized() throws JsonProcessingException {
String input = "2015-07-24T12:23:34.184+02:00";

Map<ZonedDateTime, String> map = objectMapper.readValue(getMap(input), 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\"}";
}
}