Skip to content
This repository was archived by the owner on Nov 7, 2019. It is now read-only.

Commit 27d7080

Browse files
committed
Allow users of the api to pass in a custom formatter to the YearSerializer/Deserializer.
1 parent 59709a3 commit 27d7080

File tree

3 files changed

+79
-8
lines changed

3 files changed

+79
-8
lines changed

src/main/java/com/fasterxml/jackson/datatype/jsr310/deser/YearDeserializer.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import java.io.IOException;
2323
import java.time.Year;
24+
import java.time.format.DateTimeFormatter;
2425

2526
/**
2627
* Deserializer for Java 8 temporal {@link Year}s.
@@ -31,17 +32,25 @@
3132
public class YearDeserializer extends JSR310DeserializerBase<Year>
3233
{
3334
private static final long serialVersionUID = 1L;
34-
35+
private DateTimeFormatter formatter;
3536
public static final YearDeserializer INSTANCE = new YearDeserializer();
3637

3738
private YearDeserializer()
3839
{
3940
super(Year.class);
4041
}
4142

43+
public YearDeserializer(DateTimeFormatter formatter) {
44+
super(Year.class);
45+
this.formatter = formatter;
46+
}
47+
4248
@Override
4349
public Year deserialize(JsonParser parser, DeserializationContext context) throws IOException
4450
{
45-
return Year.of(parser.getValueAsInt());
51+
if (formatter == null) {
52+
return Year.of(parser.getValueAsInt());
53+
}
54+
return Year.parse(parser.getValueAsString(), formatter);
4655
}
4756
}

src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/YearSerializer.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,20 @@ public class YearSerializer extends JSR310FormattedSerializerBase<Year>
4141
public static final YearSerializer INSTANCE = new YearSerializer();
4242

4343
protected YearSerializer() {
44-
super(Year.class);
44+
this(null);
4545
}
4646

47-
protected YearSerializer(YearSerializer base,
48-
Boolean useTimestamp, DateTimeFormatter dtf) {
49-
super(base, useTimestamp, dtf);
47+
public YearSerializer(DateTimeFormatter formatter) {
48+
super(Year.class, formatter);
49+
}
50+
51+
protected YearSerializer(YearSerializer base, Boolean useTimestamp, DateTimeFormatter formatter) {
52+
super(base, useTimestamp, formatter);
5053
}
5154

5255
@Override
53-
protected YearSerializer withFormat(Boolean useTimestamp, DateTimeFormatter dtf) {
54-
return new YearSerializer(this, useTimestamp, dtf);
56+
protected YearSerializer withFormat(Boolean useTimestamp, DateTimeFormatter formatter) {
57+
return new YearSerializer(this, useTimestamp, formatter);
5558
}
5659

5760
@Override
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.fasterxml.jackson.datatype.jsr310;
2+
3+
import static org.hamcrest.core.IsEqual.equalTo;
4+
import static org.junit.Assert.assertThat;
5+
import static org.junit.internal.matchers.StringContains.containsString;
6+
7+
import com.fasterxml.jackson.databind.ObjectMapper;
8+
import com.fasterxml.jackson.databind.module.SimpleModule;
9+
import com.fasterxml.jackson.datatype.jsr310.deser.YearDeserializer;
10+
import com.fasterxml.jackson.datatype.jsr310.ser.YearSerializer;
11+
import org.junit.Test;
12+
import org.junit.runner.RunWith;
13+
import org.junit.runners.Parameterized;
14+
import org.junit.runners.Parameterized.Parameters;
15+
16+
import java.time.Year;
17+
import java.time.format.DateTimeFormatter;
18+
import java.util.ArrayList;
19+
import java.util.Collection;
20+
21+
@RunWith(Parameterized.class)
22+
public class TestYearSerializationWithCustomFormatter {
23+
private final DateTimeFormatter formatter;
24+
25+
public TestYearSerializationWithCustomFormatter(DateTimeFormatter formatter) {
26+
this.formatter = formatter;
27+
}
28+
29+
@Test
30+
public void testSerialization() throws Exception {
31+
Year year = Year.now();
32+
String expected = "\"" + year.format(formatter) + "\"";
33+
assertThat(serializeWith(year, formatter), equalTo(expected));
34+
}
35+
36+
private String serializeWith(Year dateTime, DateTimeFormatter formatter) throws Exception {
37+
ObjectMapper mapper = new ObjectMapper().registerModule(new SimpleModule().addSerializer(new YearSerializer(formatter)));
38+
return mapper.writeValueAsString(dateTime);
39+
}
40+
41+
@Test
42+
public void testDeserialization() throws Exception {
43+
Year dateTime = Year.now();
44+
assertThat(deserializeWith(dateTime.format(formatter), formatter), equalTo(dateTime));
45+
}
46+
47+
private Year deserializeWith(String json, DateTimeFormatter formatter) throws Exception {
48+
ObjectMapper mapper = new ObjectMapper().registerModule(new SimpleModule().addDeserializer(Year.class, new YearDeserializer(formatter)));
49+
return mapper.readValue("\"" + json + "\"", Year.class);
50+
}
51+
52+
@Parameters
53+
public static Collection<Object[]> customFormatters() {
54+
Collection<Object[]> formatters = new ArrayList<>();
55+
formatters.add(new Object[]{DateTimeFormatter.ofPattern("yyyy")});
56+
formatters.add(new Object[]{DateTimeFormatter.ofPattern("yy")});
57+
return formatters;
58+
}
59+
}

0 commit comments

Comments
 (0)