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

Allow custom DateTimeFormatters #34

Merged
merged 6 commits into from
Jul 27, 2015
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 @@ -43,7 +43,7 @@ private LocalDateDeserializer() {
this(DateTimeFormatter.ISO_LOCAL_DATE);
}

protected LocalDateDeserializer(DateTimeFormatter dtf) {
public LocalDateDeserializer(DateTimeFormatter dtf) {
super(LocalDate.class, dtf);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ private LocalDateTimeDeserializer() {
this(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
}

protected LocalDateTimeDeserializer(DateTimeFormatter dtf) {
super(LocalDateTime.class, dtf);
public LocalDateTimeDeserializer(DateTimeFormatter formatter) {
super(LocalDateTime.class, formatter);
}

@Override
protected JsonDeserializer<LocalDateTime> withDateFormat(DateTimeFormatter dtf) {
return new LocalDateTimeDeserializer(dtf);
protected JsonDeserializer<LocalDateTime> withDateFormat(DateTimeFormatter formatter) {
return new LocalDateTimeDeserializer(formatter);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ private LocalTimeDeserializer() {
this(DateTimeFormatter.ISO_LOCAL_TIME);
}

protected LocalTimeDeserializer(DateTimeFormatter dtf) {
super(LocalTime.class, dtf);
public LocalTimeDeserializer(DateTimeFormatter formatter) {
super(LocalTime.class, formatter);
}

@Override
protected JsonDeserializer<LocalTime> withDateFormat(DateTimeFormatter dtf) {
return new LocalTimeDeserializer(dtf);
protected JsonDeserializer<LocalTime> withDateFormat(DateTimeFormatter formatter) {
return new LocalTimeDeserializer(formatter);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import java.io.IOException;
import java.time.Year;
import java.time.format.DateTimeFormatter;

/**
* Deserializer for Java 8 temporal {@link Year}s.
Expand All @@ -31,17 +32,25 @@
public class YearDeserializer extends JSR310DeserializerBase<Year>
{
private static final long serialVersionUID = 1L;

private DateTimeFormatter formatter;
public static final YearDeserializer INSTANCE = new YearDeserializer();

private YearDeserializer()
{
super(Year.class);
}

public YearDeserializer(DateTimeFormatter formatter) {
super(Year.class);
this.formatter = formatter;
}

@Override
public Year deserialize(JsonParser parser, DeserializationContext context) throws IOException
{
return Year.of(parser.getValueAsInt());
if (formatter == null) {
return Year.of(parser.getValueAsInt());
}
return Year.parse(parser.getValueAsString(), formatter);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ private YearMonthDeserializer()
this(DateTimeFormatter.ofPattern("uuuu-MM"));
}

protected YearMonthDeserializer(DateTimeFormatter dtf)
public YearMonthDeserializer(DateTimeFormatter formatter)
{
super(YearMonth.class, dtf);
super(YearMonth.class, formatter);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ protected LocalDateSerializer(LocalDateSerializer base,
super(base, useTimestamp, dtf);
}

public LocalDateSerializer(DateTimeFormatter formatter) {
super(LocalDate.class, formatter);
}

@Override
protected LocalDateSerializer withFormat(Boolean useTimestamp, DateTimeFormatter dtf) {
return new LocalDateSerializer(this, useTimestamp, dtf);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,20 @@ public class LocalDateTimeSerializer extends JSR310FormattedSerializerBase<Local
public static final LocalDateTimeSerializer INSTANCE = new LocalDateTimeSerializer();

protected LocalDateTimeSerializer() {
super(LocalDateTime.class);
this(null);
}

private LocalDateTimeSerializer(LocalDateTimeSerializer base,
Boolean useTimestamp, DateTimeFormatter dtf) {
super(base, useTimestamp, dtf);
public LocalDateTimeSerializer(DateTimeFormatter formatter) {
super(LocalDateTime.class, formatter);
}

private LocalDateTimeSerializer(LocalDateTimeSerializer base, Boolean useTimestamp, DateTimeFormatter formatter) {
super(base, useTimestamp, formatter);
}

@Override
protected JSR310FormattedSerializerBase<LocalDateTime> withFormat(Boolean useTimestamp, DateTimeFormatter dtf) {
return new LocalDateTimeSerializer(this, useTimestamp, dtf);
protected JSR310FormattedSerializerBase<LocalDateTime> withFormat(Boolean useTimestamp, DateTimeFormatter formatter) {
return new LocalDateTimeSerializer(this, useTimestamp, formatter);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,15 @@ public class LocalTimeSerializer extends JSR310FormattedSerializerBase<LocalTime
public static final LocalTimeSerializer INSTANCE = new LocalTimeSerializer();

protected LocalTimeSerializer() {
super(LocalTime.class);
this(null);
}

protected LocalTimeSerializer(LocalTimeSerializer base,
Boolean useTimestamp, DateTimeFormatter dtf) {
super(base, useTimestamp, dtf);
public LocalTimeSerializer(DateTimeFormatter formatter) {
super(LocalTime.class, formatter);
}

protected LocalTimeSerializer(LocalTimeSerializer base, Boolean useTimestamp, DateTimeFormatter formatter) {
super(base, useTimestamp, formatter);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,20 @@ public class YearMonthSerializer extends JSR310FormattedSerializerBase<YearMonth
public static final YearMonthSerializer INSTANCE = new YearMonthSerializer();

private YearMonthSerializer() {
super(YearMonth.class);
this(null);
}

private YearMonthSerializer(YearMonthSerializer base,
Boolean useTimestamp, DateTimeFormatter dtf) {
super(base, useTimestamp, dtf);
public YearMonthSerializer(DateTimeFormatter formatter) {
super(YearMonth.class, formatter);
}

private YearMonthSerializer(YearMonthSerializer base, Boolean useTimestamp, DateTimeFormatter formatter) {
super(base, useTimestamp, formatter);
}

@Override
protected YearMonthSerializer withFormat(Boolean useTimestamp, DateTimeFormatter dtf) {
return new YearMonthSerializer(this, useTimestamp, dtf);
protected YearMonthSerializer withFormat(Boolean useTimestamp, DateTimeFormatter formatter) {
return new YearMonthSerializer(this, useTimestamp, formatter);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,20 @@ public class YearSerializer extends JSR310FormattedSerializerBase<Year>
public static final YearSerializer INSTANCE = new YearSerializer();

protected YearSerializer() {
super(Year.class);
this(null);
}

protected YearSerializer(YearSerializer base,
Boolean useTimestamp, DateTimeFormatter dtf) {
super(base, useTimestamp, dtf);
public YearSerializer(DateTimeFormatter formatter) {
super(Year.class, formatter);
}

protected YearSerializer(YearSerializer base, Boolean useTimestamp, DateTimeFormatter formatter) {
super(base, useTimestamp, formatter);
}

@Override
protected YearSerializer withFormat(Boolean useTimestamp, DateTimeFormatter dtf) {
return new YearSerializer(this, useTimestamp, dtf);
protected YearSerializer withFormat(Boolean useTimestamp, DateTimeFormatter formatter) {
return new YearSerializer(this, useTimestamp, formatter);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.fasterxml.jackson.datatype.jsr310;

import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertThat;
import static org.junit.internal.matchers.StringContains.containsString;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collection;

@RunWith(Parameterized.class)
public class TestLocalDateSerializationWithCustomFormatter {
private final DateTimeFormatter formatter;

public TestLocalDateSerializationWithCustomFormatter(DateTimeFormatter formatter) {
this.formatter = formatter;
}

@Test
public void testSerialization() throws Exception {
LocalDate date = LocalDate.now();
assertThat(serializeWith(date, formatter), containsString(date.format(formatter)));
}

private String serializeWith(LocalDate date, DateTimeFormatter formatter) throws Exception {
ObjectMapper mapper = new ObjectMapper().registerModule(new SimpleModule().addSerializer(new LocalDateSerializer(formatter)));
return mapper.writeValueAsString(date);
}

@Test
public void testDeserialization() throws Exception {
LocalDate date = LocalDate.now();
assertThat(deserializeWith(date.format(formatter), formatter), equalTo(date));
}

private LocalDate deserializeWith(String json, DateTimeFormatter formatter) throws Exception {
ObjectMapper mapper = new ObjectMapper().registerModule(new SimpleModule().addDeserializer(LocalDate.class, new LocalDateDeserializer(formatter)));
return mapper.readValue("\"" + json + "\"", LocalDate.class);
}

@Parameters
public static Collection<Object[]> customFormatters() {
Collection<Object[]> formatters = new ArrayList<>();
formatters.add(new Object[]{DateTimeFormatter.BASIC_ISO_DATE});
formatters.add(new Object[]{DateTimeFormatter.ISO_DATE});
formatters.add(new Object[]{DateTimeFormatter.ISO_LOCAL_DATE});
formatters.add(new Object[]{DateTimeFormatter.ISO_ORDINAL_DATE});
formatters.add(new Object[]{DateTimeFormatter.ISO_WEEK_DATE});
formatters.add(new Object[]{DateTimeFormatter.ofPattern("MM/dd/yyyy")});
return formatters;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.fasterxml.jackson.datatype.jsr310;

import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertThat;
import static org.junit.internal.matchers.StringContains.containsString;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.util.ArrayList;
import java.util.Collection;

@RunWith(Parameterized.class)
public class TestLocalDateTimeSerializationWithCustomFormatter {
private final DateTimeFormatter formatter;

public TestLocalDateTimeSerializationWithCustomFormatter(DateTimeFormatter formatter) {
this.formatter = formatter;
}

@Test
public void testSerialization() throws Exception {
LocalDateTime dateTime = LocalDateTime.now();
assertThat(serializeWith(dateTime, formatter), containsString(dateTime.format(formatter)));
}

private String serializeWith(LocalDateTime dateTime, DateTimeFormatter formatter) throws Exception {
ObjectMapper mapper = new ObjectMapper().registerModule(new SimpleModule().addSerializer(new LocalDateTimeSerializer(formatter)));
return mapper.writeValueAsString(dateTime);
}

@Test
public void testDeserialization() throws Exception {
LocalDateTime dateTime = LocalDateTime.now();
assertThat(deserializeWith(dateTime.format(formatter), formatter), equalTo(dateTime));
}

private LocalDateTime deserializeWith(String json, DateTimeFormatter formatter) throws Exception {
ObjectMapper mapper = new ObjectMapper().registerModule(new SimpleModule().addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(formatter)));
return mapper.readValue("\"" + json + "\"", LocalDateTime.class);
}

@Parameters
public static Collection<Object[]> customFormatters() {
Collection<Object[]> formatters = new ArrayList<>();
formatters.add(new Object[]{DateTimeFormatter.ISO_DATE_TIME});
formatters.add(new Object[]{DateTimeFormatter.ISO_LOCAL_DATE_TIME});
return formatters;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.fasterxml.jackson.datatype.jsr310;

import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertThat;
import static org.junit.internal.matchers.StringContains.containsString;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.ArrayList;
import java.util.Collection;

@RunWith(Parameterized.class)
public class TestLocalTimeSerializationWithCustomFormatter {
private final DateTimeFormatter formatter;

public TestLocalTimeSerializationWithCustomFormatter(DateTimeFormatter formatter) {
this.formatter = formatter;
}

@Test
public void testSerialization() throws Exception {
LocalTime dateTime = LocalTime.now();
assertThat(serializeWith(dateTime, formatter), containsString(dateTime.format(formatter)));
}

private String serializeWith(LocalTime dateTime, DateTimeFormatter formatter) throws Exception {
ObjectMapper mapper = new ObjectMapper().registerModule(new SimpleModule().addSerializer(new LocalTimeSerializer(formatter)));
return mapper.writeValueAsString(dateTime);
}

@Test
public void testDeserialization() throws Exception {
LocalTime dateTime = LocalTime.now();
assertThat(deserializeWith(dateTime.format(formatter), formatter), equalTo(dateTime));
}

private LocalTime deserializeWith(String json, DateTimeFormatter formatter) throws Exception {
ObjectMapper mapper = new ObjectMapper().registerModule(new SimpleModule().addDeserializer(LocalTime.class, new LocalTimeDeserializer(formatter)));
return mapper.readValue("\"" + json + "\"", LocalTime.class);
}

@Parameters
public static Collection<Object[]> customFormatters() {
Collection<Object[]> formatters = new ArrayList<>();
formatters.add(new Object[]{DateTimeFormatter.ISO_LOCAL_TIME});
formatters.add(new Object[]{DateTimeFormatter.ISO_TIME});
return formatters;
}
}
Loading