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

Commit c514fc2

Browse files
committed
Allow users of the api pass in a custom formatter to the LocalDateSerializer/Deserializer.
1 parent 930e4c3 commit c514fc2

File tree

3 files changed

+67
-1
lines changed

3 files changed

+67
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ private LocalDateDeserializer() {
4343
this(DateTimeFormatter.ISO_LOCAL_DATE);
4444
}
4545

46-
protected LocalDateDeserializer(DateTimeFormatter dtf) {
46+
public LocalDateDeserializer(DateTimeFormatter dtf) {
4747
super(LocalDate.class, dtf);
4848
}
4949

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ protected LocalDateSerializer(LocalDateSerializer base,
4444
super(base, useTimestamp, dtf);
4545
}
4646

47+
public LocalDateSerializer(DateTimeFormatter formatter) {
48+
super(LocalDate.class, formatter);
49+
}
50+
4751
@Override
4852
protected LocalDateSerializer withFormat(Boolean useTimestamp, DateTimeFormatter dtf) {
4953
return new LocalDateSerializer(this, useTimestamp, dtf);
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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.LocalDateDeserializer;
10+
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
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.LocalDate;
17+
import java.time.format.DateTimeFormatter;
18+
import java.util.ArrayList;
19+
import java.util.Collection;
20+
21+
@RunWith(Parameterized.class)
22+
public class TestLocalDateSerializationWithCustomFormatter {
23+
private final DateTimeFormatter formatter;
24+
25+
public TestLocalDateSerializationWithCustomFormatter(DateTimeFormatter formatter) {
26+
this.formatter = formatter;
27+
}
28+
29+
@Test
30+
public void testSerialization() throws Exception {
31+
LocalDate date = LocalDate.now();
32+
assertThat("Failed to serialize with " + formatter, serializeWith(date, formatter), containsString(date.format(formatter)));
33+
}
34+
35+
private String serializeWith(LocalDate date, DateTimeFormatter formatter) throws Exception {
36+
ObjectMapper mapper = new ObjectMapper().registerModule(new SimpleModule().addSerializer(new LocalDateSerializer(formatter)));
37+
return mapper.writeValueAsString(date);
38+
}
39+
40+
@Test
41+
public void testDeserialization() throws Exception {
42+
LocalDate date = LocalDate.now();
43+
assertThat(deserializeWith(date.format(formatter), formatter), equalTo(date));
44+
}
45+
46+
private LocalDate deserializeWith(String json, DateTimeFormatter formatter) throws Exception {
47+
ObjectMapper mapper = new ObjectMapper().registerModule(new SimpleModule().addDeserializer(LocalDate.class, new LocalDateDeserializer(formatter)));
48+
return mapper.readValue("\"" + json + "\"", LocalDate.class);
49+
}
50+
51+
@Parameters
52+
public static Collection<Object[]> customFormatters() {
53+
Collection<Object[]> formatters = new ArrayList<>();
54+
formatters.add(new Object[]{DateTimeFormatter.BASIC_ISO_DATE});
55+
formatters.add(new Object[]{DateTimeFormatter.ISO_DATE});
56+
formatters.add(new Object[]{DateTimeFormatter.ISO_LOCAL_DATE});
57+
formatters.add(new Object[]{DateTimeFormatter.ISO_ORDINAL_DATE});
58+
formatters.add(new Object[]{DateTimeFormatter.ISO_WEEK_DATE});
59+
formatters.add(new Object[]{DateTimeFormatter.ofPattern("MM/dd/yyyy")});
60+
return formatters;
61+
}
62+
}

0 commit comments

Comments
 (0)