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

Commit ba46f10

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

File tree

5 files changed

+72
-10
lines changed

5 files changed

+72
-10
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@ private LocalTimeDeserializer() {
4242
this(DateTimeFormatter.ISO_LOCAL_TIME);
4343
}
4444

45-
protected LocalTimeDeserializer(DateTimeFormatter dtf) {
46-
super(LocalTime.class, dtf);
45+
public LocalTimeDeserializer(DateTimeFormatter formatter) {
46+
super(LocalTime.class, formatter);
4747
}
4848

4949
@Override
50-
protected JsonDeserializer<LocalTime> withDateFormat(DateTimeFormatter dtf) {
51-
return new LocalTimeDeserializer(dtf);
50+
protected JsonDeserializer<LocalTime> withDateFormat(DateTimeFormatter formatter) {
51+
return new LocalTimeDeserializer(formatter);
5252
}
5353

5454
@Override

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,15 @@ public class LocalTimeSerializer extends JSR310FormattedSerializerBase<LocalTime
3838
public static final LocalTimeSerializer INSTANCE = new LocalTimeSerializer();
3939

4040
protected LocalTimeSerializer() {
41-
super(LocalTime.class);
41+
this(null);
4242
}
4343

44-
protected LocalTimeSerializer(LocalTimeSerializer base,
45-
Boolean useTimestamp, DateTimeFormatter dtf) {
46-
super(base, useTimestamp, dtf);
44+
public LocalTimeSerializer(DateTimeFormatter formatter) {
45+
super(LocalTime.class, formatter);
46+
}
47+
48+
protected LocalTimeSerializer(LocalTimeSerializer base, Boolean useTimestamp, DateTimeFormatter formatter) {
49+
super(base, useTimestamp, formatter);
4750
}
4851

4952
@Override

src/test/java/com/fasterxml/jackson/datatype/jsr310/TestLocalDateSerializationWithCustomFormatter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public TestLocalDateSerializationWithCustomFormatter(DateTimeFormatter formatter
2929
@Test
3030
public void testSerialization() throws Exception {
3131
LocalDate date = LocalDate.now();
32-
assertThat("Failed to serialize with " + formatter, serializeWith(date, formatter), containsString(date.format(formatter)));
32+
assertThat(serializeWith(date, formatter), containsString(date.format(formatter)));
3333
}
3434

3535
private String serializeWith(LocalDate date, DateTimeFormatter formatter) throws Exception {

src/test/java/com/fasterxml/jackson/datatype/jsr310/TestLocalDateTimeSerializationWithCustomFormatter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public TestLocalDateTimeSerializationWithCustomFormatter(DateTimeFormatter forma
3333
@Test
3434
public void testSerialization() throws Exception {
3535
LocalDateTime dateTime = LocalDateTime.now();
36-
assertThat("Failed to serialize with " + formatter, serializeWith(dateTime, formatter), containsString(dateTime.format(formatter)));
36+
assertThat(serializeWith(dateTime, formatter), containsString(dateTime.format(formatter)));
3737
}
3838

3939
private String serializeWith(LocalDateTime dateTime, DateTimeFormatter formatter) throws Exception {
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.LocalTimeDeserializer;
10+
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
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.LocalTime;
17+
import java.time.format.DateTimeFormatter;
18+
import java.time.format.FormatStyle;
19+
import java.util.ArrayList;
20+
import java.util.Collection;
21+
22+
@RunWith(Parameterized.class)
23+
public class TestLocalTimeSerializationWithCustomFormatter {
24+
private final DateTimeFormatter formatter;
25+
26+
public TestLocalTimeSerializationWithCustomFormatter(DateTimeFormatter formatter) {
27+
this.formatter = formatter;
28+
}
29+
30+
@Test
31+
public void testSerialization() throws Exception {
32+
LocalTime dateTime = LocalTime.now();
33+
assertThat(serializeWith(dateTime, formatter), containsString(dateTime.format(formatter)));
34+
}
35+
36+
private String serializeWith(LocalTime dateTime, DateTimeFormatter formatter) throws Exception {
37+
ObjectMapper mapper = new ObjectMapper().registerModule(new SimpleModule().addSerializer(new LocalTimeSerializer(formatter)));
38+
return mapper.writeValueAsString(dateTime);
39+
}
40+
41+
@Test
42+
public void testDeserialization() throws Exception {
43+
LocalTime dateTime = LocalTime.now();
44+
assertThat(deserializeWith(dateTime.format(formatter), formatter), equalTo(dateTime));
45+
}
46+
47+
private LocalTime deserializeWith(String json, DateTimeFormatter formatter) throws Exception {
48+
ObjectMapper mapper = new ObjectMapper().registerModule(new SimpleModule().addDeserializer(LocalTime.class, new LocalTimeDeserializer(formatter)));
49+
return mapper.readValue("\"" + json + "\"", LocalTime.class);
50+
}
51+
52+
@Parameters
53+
public static Collection<Object[]> customFormatters() {
54+
Collection<Object[]> formatters = new ArrayList<>();
55+
formatters.add(new Object[]{DateTimeFormatter.ISO_LOCAL_TIME});
56+
formatters.add(new Object[]{DateTimeFormatter.ISO_TIME});
57+
return formatters;
58+
}
59+
}

0 commit comments

Comments
 (0)