Skip to content

Commit 3e051be

Browse files
kupcicowtowncoder
authored andcommitted
Year key deser fix (#134)
Year key deserializer fix #134
1 parent ddbf7e2 commit 3e051be

File tree

2 files changed

+36
-17
lines changed

2 files changed

+36
-17
lines changed

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

+6-10
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,19 @@ public class YearKeyDeserializer extends Jsr310KeyDeserializer {
1515

1616
public static final YearKeyDeserializer INSTANCE = new YearKeyDeserializer();
1717

18-
/*
19-
* formatter copied from Year. There is no way of getting a reference to the formatter it uses.
20-
*/
21-
private static final DateTimeFormatter FORMATTER = new DateTimeFormatterBuilder()
22-
.appendValue(YEAR, 4, 10, SignStyle.EXCEEDS_PAD)
23-
.toFormatter();
24-
2518
private YearKeyDeserializer() {
2619
// singleton
2720
}
2821

2922
@Override
3023
protected Year deserialize(String key, DeserializationContext ctxt) throws IOException {
24+
3125
try {
32-
return Year.parse(key, FORMATTER);
33-
} catch (DateTimeException e) {
34-
return _handleDateTimeException(ctxt, Year.class, e, key);
26+
return Year.of(Integer.parseInt(key));
27+
} catch (NumberFormatException nfe) {
28+
return _handleDateTimeException(ctxt, Year.class, new DateTimeException("Number format exception", nfe), key);
29+
} catch (DateTimeException dte) {
30+
return _handleDateTimeException(ctxt, Year.class, dte, key);
3531
}
3632
}
3733
}
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
package com.fasterxml.jackson.datatype.jsr310.key;
22

3-
import java.time.Year;
4-
import java.util.Map;
5-
63
import com.fasterxml.jackson.core.type.TypeReference;
74
import com.fasterxml.jackson.databind.ObjectMapper;
85
import com.fasterxml.jackson.databind.ObjectReader;
6+
import com.fasterxml.jackson.databind.exc.InvalidFormatException;
97
import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase;
10-
11-
import org.junit.Assert;
128
import org.junit.Test;
139

10+
import java.time.Year;
11+
import java.util.Collections;
12+
import java.util.Map;
13+
14+
import static org.junit.Assert.assertEquals;
15+
1416
public class YearAsKeyTest extends ModuleTestBase
1517
{
1618
private static final TypeReference<Map<Year, String>> TYPE_REF = new TypeReference<Map<Year, String>>() {
@@ -20,13 +22,34 @@ public class YearAsKeyTest extends ModuleTestBase
2022

2123
@Test
2224
public void testSerialization() throws Exception {
23-
Assert.assertEquals("Value is incorrect", mapAsString("3141", "test"),
25+
assertEquals("Value is incorrect", mapAsString("3141", "test"),
2426
MAPPER.writeValueAsString(asMap(Year.of(3141), "test")));
2527
}
2628

2729
@Test
2830
public void testDeserialization() throws Exception {
29-
Assert.assertEquals("Value is incorrect", asMap(Year.of(3141), "test"),
31+
assertEquals("Value is incorrect", asMap(Year.of(3141), "test"),
3032
READER.readValue(mapAsString("3141", "test")));
3133
}
34+
35+
@Test(expected = InvalidFormatException.class)
36+
public void deserializeYearKey_notANumber() throws Exception {
37+
READER.readValue(mapAsString("10000BC", "test"));
38+
}
39+
40+
@Test(expected = InvalidFormatException.class)
41+
public void deserializeYearKey_notAYear() throws Exception {
42+
READER.readValue(mapAsString(Integer.toString(Year.MAX_VALUE+1), "test"));
43+
}
44+
45+
@Test
46+
public void serializeAndDeserializeYearKeyUnpadded() throws Exception {
47+
// fix for issue #51 verify we can deserialize an unpadded year e.g. "1"
48+
ObjectMapper unpaddedMapper = newMapper();
49+
Map<Year, Float> testMap = Collections.singletonMap(Year.of(1), 1F);
50+
String serialized = unpaddedMapper.writeValueAsString(testMap);
51+
TypeReference<Map<Year, Float>> yearFloatTypeReference = new TypeReference<Map<Year, Float>>() {};
52+
Map<Year, Float> deserialized = unpaddedMapper.readValue(serialized, yearFloatTypeReference);
53+
assertEquals(testMap, deserialized);
54+
}
3255
}

0 commit comments

Comments
 (0)