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

Year key deser fix #134

Merged
merged 5 commits into from
Sep 5, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,19 @@ public class YearKeyDeserializer extends Jsr310KeyDeserializer {

public static final YearKeyDeserializer INSTANCE = new YearKeyDeserializer();

/*
* formatter copied from Year. There is no way of getting a reference to the formatter it uses.
*/
private static final DateTimeFormatter FORMATTER = new DateTimeFormatterBuilder()
.appendValue(YEAR, 4, 10, SignStyle.EXCEEDS_PAD)
.toFormatter();

private YearKeyDeserializer() {
// singleton
}

@Override
protected Year deserialize(String key, DeserializationContext ctxt) throws IOException {

try {
return Year.parse(key, FORMATTER);
} catch (DateTimeException e) {
return _handleDateTimeException(ctxt, Year.class, e, key);
return Year.of(Integer.parseInt(key));
} catch (NumberFormatException nfe) {
return _handleDateTimeException(ctxt, Year.class, new DateTimeException("Number format exception", nfe), key);
} catch (DateTimeException dte) {
return _handleDateTimeException(ctxt, Year.class, dte, key);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package com.fasterxml.jackson.datatype.jsr310.key;

import java.time.Year;
import java.util.Map;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.databind.exc.InvalidFormatException;
import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase;

import org.junit.Assert;
import org.junit.Test;

import java.time.Year;
import java.util.Collections;
import java.util.Map;

import static org.junit.Assert.assertEquals;

public class YearAsKeyTest extends ModuleTestBase
{
private static final TypeReference<Map<Year, String>> TYPE_REF = new TypeReference<Map<Year, String>>() {
Expand All @@ -20,13 +22,34 @@ public class YearAsKeyTest extends ModuleTestBase

@Test
public void testSerialization() throws Exception {
Assert.assertEquals("Value is incorrect", mapAsString("3141", "test"),
assertEquals("Value is incorrect", mapAsString("3141", "test"),
MAPPER.writeValueAsString(asMap(Year.of(3141), "test")));
}

@Test
public void testDeserialization() throws Exception {
Assert.assertEquals("Value is incorrect", asMap(Year.of(3141), "test"),
assertEquals("Value is incorrect", asMap(Year.of(3141), "test"),
READER.readValue(mapAsString("3141", "test")));
}

@Test(expected = InvalidFormatException.class)
public void deserializeYearKey_notANumber() throws Exception {
READER.readValue(mapAsString("10000BC", "test"));
}

@Test(expected = InvalidFormatException.class)
public void deserializeYearKey_notAYear() throws Exception {
READER.readValue(mapAsString(Integer.toString(Year.MAX_VALUE+1), "test"));
}

@Test
public void serializeAndDeserializeYearKeyUnpadded() throws Exception {
// fix for issue #51 verify we can deserialize an unpadded year e.g. "1"
ObjectMapper unpaddedMapper = newMapper();
Map<Year, Float> testMap = Collections.singletonMap(Year.of(1), 1F);
String serialized = unpaddedMapper.writeValueAsString(testMap);
TypeReference<Map<Year, Float>> yearFloatTypeReference = new TypeReference<Map<Year, Float>>() {};
Map<Year, Float> deserialized = unpaddedMapper.readValue(serialized, yearFloatTypeReference);
assertEquals(testMap, deserialized);
}
}