Skip to content

Commit

Permalink
Support more formats in ZonedDateTimeKeyDeserializer
Browse files Browse the repository at this point in the history
  • Loading branch information
caluml committed Mar 19, 2024
1 parent 67cedd6 commit 5732d55
Show file tree
Hide file tree
Showing 2 changed files with 71 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,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);
} 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 {
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\"}";
}
}

0 comments on commit 5732d55

Please sign in to comment.