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

Commit e2f7664

Browse files
committed
Allow users of the api to pass in a custom formatter to the YearMonthSerializer/Deserializer.
1 parent ba46f10 commit e2f7664

File tree

3 files changed

+68
-8
lines changed

3 files changed

+68
-8
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ private YearMonthDeserializer()
4545
this(DateTimeFormatter.ofPattern("uuuu-MM"));
4646
}
4747

48-
protected YearMonthDeserializer(DateTimeFormatter dtf)
48+
public YearMonthDeserializer(DateTimeFormatter formatter)
4949
{
50-
super(YearMonth.class, dtf);
50+
super(YearMonth.class, formatter);
5151
}
5252

5353
@Override

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

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

4343
private YearMonthSerializer() {
44-
super(YearMonth.class);
44+
this(null);
4545
}
4646

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

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

5760
@Override
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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.YearMonthDeserializer;
10+
import com.fasterxml.jackson.datatype.jsr310.ser.YearMonthSerializer;
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.YearMonth;
17+
import java.time.format.DateTimeFormatter;
18+
import java.util.ArrayList;
19+
import java.util.Collection;
20+
21+
@RunWith(Parameterized.class)
22+
public class TestYearMonthSerializationWithCustomFormatter {
23+
private final DateTimeFormatter formatter;
24+
25+
public TestYearMonthSerializationWithCustomFormatter(DateTimeFormatter formatter) {
26+
this.formatter = formatter;
27+
}
28+
29+
@Test
30+
public void testSerialization() throws Exception {
31+
YearMonth dateTime = YearMonth.now();
32+
assertThat(serializeWith(dateTime, formatter), containsString(dateTime.format(formatter)));
33+
}
34+
35+
private String serializeWith(YearMonth dateTime, DateTimeFormatter formatter) throws Exception {
36+
ObjectMapper mapper = new ObjectMapper().registerModule(new SimpleModule().addSerializer(new YearMonthSerializer(formatter)));
37+
return mapper.writeValueAsString(dateTime);
38+
}
39+
40+
@Test
41+
public void testDeserialization() throws Exception {
42+
YearMonth dateTime = YearMonth.now();
43+
assertThat(deserializeWith(dateTime.format(formatter), formatter), equalTo(dateTime));
44+
}
45+
46+
private YearMonth deserializeWith(String json, DateTimeFormatter formatter) throws Exception {
47+
ObjectMapper mapper = new ObjectMapper().registerModule(new SimpleModule().addDeserializer(YearMonth.class, new YearMonthDeserializer(formatter)));
48+
return mapper.readValue("\"" + json + "\"", YearMonth.class);
49+
}
50+
51+
@Parameters
52+
public static Collection<Object[]> customFormatters() {
53+
Collection<Object[]> formatters = new ArrayList<>();
54+
formatters.add(new Object[]{DateTimeFormatter.ofPattern("uuuu-MM")});
55+
return formatters;
56+
}
57+
}

0 commit comments

Comments
 (0)