diff --git a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/deser/LocalDateDeserializer.java b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/deser/LocalDateDeserializer.java index f07ce4c6..e17a9585 100644 --- a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/deser/LocalDateDeserializer.java +++ b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/deser/LocalDateDeserializer.java @@ -99,6 +99,9 @@ public LocalDate deserialize(JsonParser parser, DeserializationContext context) if (parser.hasToken(JsonToken.VALUE_EMBEDDED_OBJECT)) { return (LocalDate) parser.getEmbeddedObject(); } + if (parser.hasToken(JsonToken.VALUE_NUMBER_INT)) { + return LocalDate.ofEpochDay(parser.getLongValue()); + } throw context.wrongTokenException(parser, handledType(), JsonToken.VALUE_STRING, "Expected array or string."); } diff --git a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/DurationSerializer.java b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/DurationSerializer.java index 6c23da59..fa3439e1 100644 --- a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/DurationSerializer.java +++ b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/DurationSerializer.java @@ -16,6 +16,7 @@ package com.fasterxml.jackson.datatype.jsr310.ser; +import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.databind.JavaType; @@ -49,11 +50,11 @@ private DurationSerializer() { protected DurationSerializer(DurationSerializer base, Boolean useTimestamp, DateTimeFormatter dtf) { - super(base, useTimestamp, dtf); + super(base, useTimestamp, dtf, null); } @Override - protected DurationSerializer withFormat(Boolean useTimestamp, DateTimeFormatter dtf) { + protected DurationSerializer withFormat(Boolean useTimestamp, DateTimeFormatter dtf, JsonFormat.Shape shape) { return new DurationSerializer(this, useTimestamp, dtf); } diff --git a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/InstantSerializer.java b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/InstantSerializer.java index 23dfde31..96a58571 100644 --- a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/InstantSerializer.java +++ b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/InstantSerializer.java @@ -16,6 +16,7 @@ package com.fasterxml.jackson.datatype.jsr310.ser; +import com.fasterxml.jackson.annotation.JsonFormat; import java.time.Instant; import java.time.OffsetDateTime; import java.time.ZonedDateTime; @@ -45,7 +46,10 @@ protected InstantSerializer(InstantSerializer base, } @Override - protected JSR310FormattedSerializerBase withFormat(Boolean useTimestamp, DateTimeFormatter formatter) { + protected JSR310FormattedSerializerBase withFormat( + Boolean useTimestamp, + DateTimeFormatter formatter, + JsonFormat.Shape shape) { return new InstantSerializer(this, useTimestamp, formatter); } } diff --git a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/InstantSerializerBase.java b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/InstantSerializerBase.java index fdbed458..8788839a 100644 --- a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/InstantSerializerBase.java +++ b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/InstantSerializerBase.java @@ -16,6 +16,7 @@ package com.fasterxml.jackson.datatype.jsr310.ser; +import com.fasterxml.jackson.annotation.JsonFormat; import java.io.IOException; import java.time.format.DateTimeFormatter; import java.time.temporal.Temporal; @@ -64,7 +65,7 @@ protected InstantSerializerBase(Class supportedType, ToLongFunction getEpo protected InstantSerializerBase(InstantSerializerBase base, Boolean useTimestamp, DateTimeFormatter dtf) { - super(base, useTimestamp, dtf); + super(base, useTimestamp, dtf, null); defaultFormat = base.defaultFormat; getEpochMillis = base.getEpochMillis; getEpochSeconds = base.getEpochSeconds; @@ -72,8 +73,9 @@ protected InstantSerializerBase(InstantSerializerBase base, } @Override - protected abstract JSR310FormattedSerializerBase withFormat(Boolean useTimestamp, - DateTimeFormatter dtf); + protected abstract JSR310FormattedSerializerBase withFormat( + Boolean useTimestamp, + DateTimeFormatter dtf, JsonFormat.Shape shape); @Override public void serialize(T value, JsonGenerator generator, SerializerProvider provider) throws IOException diff --git a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/JSR310FormattedSerializerBase.java b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/JSR310FormattedSerializerBase.java index 00a27a9d..00c3b935 100644 --- a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/JSR310FormattedSerializerBase.java +++ b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/JSR310FormattedSerializerBase.java @@ -55,6 +55,8 @@ abstract class JSR310FormattedSerializerBase */ protected final DateTimeFormatter _formatter; + protected final JsonFormat.Shape _shape; + protected JSR310FormattedSerializerBase(Class supportedType) { this(supportedType, null); } @@ -63,19 +65,22 @@ protected JSR310FormattedSerializerBase(Class supportedType, DateTimeFormatter formatter) { super(supportedType); _useTimestamp = null; + _shape = null; _formatter = formatter; } protected JSR310FormattedSerializerBase(JSR310FormattedSerializerBase base, - Boolean useTimestamp, DateTimeFormatter dtf) + Boolean useTimestamp, DateTimeFormatter dtf, JsonFormat.Shape shape) { super(base.handledType()); _useTimestamp = useTimestamp; _formatter = dtf; + _shape = shape; } protected abstract JSR310FormattedSerializerBase withFormat(Boolean useTimestamp, - DateTimeFormatter dtf); + DateTimeFormatter dtf, + JsonFormat.Shape shape); /** * @since 2.8 @@ -118,8 +123,8 @@ public JsonSerializer createContextual(SerializerProvider prov, } } JSR310FormattedSerializerBase ser = this; - if ((useTimestamp != _useTimestamp) || (dtf != _formatter)) { - ser = ser.withFormat(useTimestamp, dtf); + if ((shape != _shape) || (useTimestamp != _useTimestamp) || (dtf != _formatter)) { + ser = ser.withFormat(useTimestamp, dtf, shape); } Boolean writeZoneId = format.getFeature(JsonFormat.Feature.WRITE_DATES_WITH_ZONE_ID); if (writeZoneId != null) { diff --git a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/LocalDateSerializer.java b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/LocalDateSerializer.java index ce2a13b1..17070c8d 100644 --- a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/LocalDateSerializer.java +++ b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/LocalDateSerializer.java @@ -16,6 +16,7 @@ package com.fasterxml.jackson.datatype.jsr310.ser; +import com.fasterxml.jackson.annotation.JsonFormat; import java.io.IOException; import java.time.LocalDate; import java.time.format.DateTimeFormatter; @@ -44,8 +45,8 @@ protected LocalDateSerializer() { } protected LocalDateSerializer(LocalDateSerializer base, - Boolean useTimestamp, DateTimeFormatter dtf) { - super(base, useTimestamp, dtf); + Boolean useTimestamp, DateTimeFormatter dtf, JsonFormat.Shape shape) { + super(base, useTimestamp, dtf, shape); } public LocalDateSerializer(DateTimeFormatter formatter) { @@ -53,19 +54,23 @@ public LocalDateSerializer(DateTimeFormatter formatter) { } @Override - protected LocalDateSerializer withFormat(Boolean useTimestamp, DateTimeFormatter dtf) { - return new LocalDateSerializer(this, useTimestamp, dtf); + protected LocalDateSerializer withFormat(Boolean useTimestamp, DateTimeFormatter dtf, JsonFormat.Shape shape) { + return new LocalDateSerializer(this, useTimestamp, dtf, shape); } @Override public void serialize(LocalDate date, JsonGenerator generator, SerializerProvider provider) throws IOException { if (useTimestamp(provider)) { - generator.writeStartArray(); - generator.writeNumber(date.getYear()); - generator.writeNumber(date.getMonthValue()); - generator.writeNumber(date.getDayOfMonth()); - generator.writeEndArray(); + if (_shape == JsonFormat.Shape.NUMBER_INT) { + generator.writeNumber(date.toEpochDay()); + } else { + generator.writeStartArray(); + generator.writeNumber(date.getYear()); + generator.writeNumber(date.getMonthValue()); + generator.writeNumber(date.getDayOfMonth()); + generator.writeEndArray(); + } } else { String str = (_formatter == null) ? date.toString() : date.format(_formatter); generator.writeString(str); diff --git a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/LocalDateTimeSerializer.java b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/LocalDateTimeSerializer.java index 8c33a1d6..7326e4a6 100644 --- a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/LocalDateTimeSerializer.java +++ b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/LocalDateTimeSerializer.java @@ -16,6 +16,7 @@ package com.fasterxml.jackson.datatype.jsr310.ser; +import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.databind.SerializerProvider; @@ -46,11 +47,11 @@ public LocalDateTimeSerializer(DateTimeFormatter f) { } private LocalDateTimeSerializer(LocalDateTimeSerializer base, Boolean useTimestamp, DateTimeFormatter f) { - super(base, useTimestamp, f); + super(base, useTimestamp, f, null); } @Override - protected JSR310FormattedSerializerBase withFormat(Boolean useTimestamp, DateTimeFormatter f) { + protected JSR310FormattedSerializerBase withFormat(Boolean useTimestamp, DateTimeFormatter f, JsonFormat.Shape shape) { return new LocalDateTimeSerializer(this, useTimestamp, f); } diff --git a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/LocalTimeSerializer.java b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/LocalTimeSerializer.java index b0a314ad..6eba1bdd 100644 --- a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/LocalTimeSerializer.java +++ b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/LocalTimeSerializer.java @@ -16,6 +16,7 @@ package com.fasterxml.jackson.datatype.jsr310.ser; +import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.databind.SerializerProvider; @@ -46,11 +47,11 @@ public LocalTimeSerializer(DateTimeFormatter formatter) { } protected LocalTimeSerializer(LocalTimeSerializer base, Boolean useTimestamp, DateTimeFormatter formatter) { - super(base, useTimestamp, formatter); + super(base, useTimestamp, formatter, null); } @Override - protected JSR310FormattedSerializerBase withFormat(Boolean useTimestamp, DateTimeFormatter dtf) { + protected JSR310FormattedSerializerBase withFormat(Boolean useTimestamp, DateTimeFormatter dtf, JsonFormat.Shape shape) { return new LocalTimeSerializer(this, useTimestamp, dtf); } diff --git a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/MonthDaySerializer.java b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/MonthDaySerializer.java index bce61dac..a8ce8593 100644 --- a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/MonthDaySerializer.java +++ b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/MonthDaySerializer.java @@ -16,18 +16,17 @@ package com.fasterxml.jackson.datatype.jsr310.ser; -import java.io.IOException; -import java.time.MonthDay; -import java.time.format.DateTimeFormatter; - +import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.core.JsonGenerator; - import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.SerializerProvider; import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatVisitorWrapper; import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonStringFormatVisitor; import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonValueFormat; +import java.io.IOException; +import java.time.MonthDay; +import java.time.format.DateTimeFormatter; /** * Serializer for Java 8 temporal {@link MonthDay}s. @@ -53,11 +52,11 @@ public MonthDaySerializer(DateTimeFormatter formatter) { } private MonthDaySerializer(MonthDaySerializer base, Boolean useTimestamp, DateTimeFormatter formatter) { - super(base, useTimestamp, formatter); + super(base, useTimestamp, formatter, null); } @Override - protected MonthDaySerializer withFormat(Boolean useTimestamp, DateTimeFormatter formatter) { + protected MonthDaySerializer withFormat(Boolean useTimestamp, DateTimeFormatter formatter, JsonFormat.Shape shape) { return new MonthDaySerializer(this, useTimestamp, formatter); } diff --git a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/OffsetDateTimeSerializer.java b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/OffsetDateTimeSerializer.java index 5d3ab2c6..5f440f24 100644 --- a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/OffsetDateTimeSerializer.java +++ b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/OffsetDateTimeSerializer.java @@ -1,5 +1,6 @@ package com.fasterxml.jackson.datatype.jsr310.ser; +import com.fasterxml.jackson.annotation.JsonFormat; import java.time.OffsetDateTime; import java.time.format.DateTimeFormatter; @@ -21,7 +22,10 @@ protected OffsetDateTimeSerializer(OffsetDateTimeSerializer base, } @Override - protected JSR310FormattedSerializerBase withFormat(Boolean useTimestamp, DateTimeFormatter formatter) { + protected JSR310FormattedSerializerBase withFormat( + Boolean useTimestamp, + DateTimeFormatter formatter, + JsonFormat.Shape shape) { return new OffsetDateTimeSerializer(this, useTimestamp, formatter); } } diff --git a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/OffsetTimeSerializer.java b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/OffsetTimeSerializer.java index d2ab7c2e..63c84152 100644 --- a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/OffsetTimeSerializer.java +++ b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/OffsetTimeSerializer.java @@ -16,6 +16,7 @@ package com.fasterxml.jackson.datatype.jsr310.ser; +import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.databind.SerializerProvider; @@ -43,11 +44,11 @@ protected OffsetTimeSerializer() { protected OffsetTimeSerializer(OffsetTimeSerializer base, Boolean useTimestamp, DateTimeFormatter dtf) { - super(base, useTimestamp, dtf); + super(base, useTimestamp, dtf, null); } @Override - protected OffsetTimeSerializer withFormat(Boolean useTimestamp, DateTimeFormatter dtf) { + protected OffsetTimeSerializer withFormat(Boolean useTimestamp, DateTimeFormatter dtf, JsonFormat.Shape shape) { return new OffsetTimeSerializer(this, useTimestamp, dtf); } diff --git a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/YearMonthSerializer.java b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/YearMonthSerializer.java index 66a59c16..64866538 100644 --- a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/YearMonthSerializer.java +++ b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/YearMonthSerializer.java @@ -16,6 +16,7 @@ package com.fasterxml.jackson.datatype.jsr310.ser; +import com.fasterxml.jackson.annotation.JsonFormat; import java.io.IOException; import java.time.YearMonth; import java.time.format.DateTimeFormatter; @@ -50,11 +51,11 @@ public YearMonthSerializer(DateTimeFormatter formatter) { } private YearMonthSerializer(YearMonthSerializer base, Boolean useTimestamp, DateTimeFormatter formatter) { - super(base, useTimestamp, formatter); + super(base, useTimestamp, formatter, null); } @Override - protected YearMonthSerializer withFormat(Boolean useTimestamp, DateTimeFormatter formatter) { + protected YearMonthSerializer withFormat(Boolean useTimestamp, DateTimeFormatter formatter, JsonFormat.Shape shape) { return new YearMonthSerializer(this, useTimestamp, formatter); } diff --git a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/YearSerializer.java b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/YearSerializer.java index 219e4745..5b47c63f 100644 --- a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/YearSerializer.java +++ b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/YearSerializer.java @@ -16,6 +16,7 @@ package com.fasterxml.jackson.datatype.jsr310.ser; +import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.databind.JavaType; @@ -49,11 +50,11 @@ public YearSerializer(DateTimeFormatter formatter) { } protected YearSerializer(YearSerializer base, Boolean useTimestamp, DateTimeFormatter formatter) { - super(base, useTimestamp, formatter); + super(base, useTimestamp, formatter, null); } @Override - protected YearSerializer withFormat(Boolean useTimestamp, DateTimeFormatter formatter) { + protected YearSerializer withFormat(Boolean useTimestamp, DateTimeFormatter formatter, JsonFormat.Shape shape) { return new YearSerializer(this, useTimestamp, formatter); } diff --git a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/ZonedDateTimeSerializer.java b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/ZonedDateTimeSerializer.java index 83b5c1ed..e48e26c1 100644 --- a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/ZonedDateTimeSerializer.java +++ b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/ZonedDateTimeSerializer.java @@ -1,5 +1,6 @@ package com.fasterxml.jackson.datatype.jsr310.ser; +import com.fasterxml.jackson.annotation.JsonFormat; import java.io.IOException; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; @@ -39,7 +40,10 @@ protected ZonedDateTimeSerializer(ZonedDateTimeSerializer base, } @Override - protected JSR310FormattedSerializerBase withFormat(Boolean useTimestamp, DateTimeFormatter formatter) { + protected JSR310FormattedSerializerBase withFormat( + Boolean useTimestamp, + DateTimeFormatter formatter, + JsonFormat.Shape shape) { return new ZonedDateTimeSerializer(this, useTimestamp, formatter, _writeZoneId); } diff --git a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/ZonedDateTimeWithZoneIdSerializer.java b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/ZonedDateTimeWithZoneIdSerializer.java index 7af68fe2..0aabbc09 100644 --- a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/ZonedDateTimeWithZoneIdSerializer.java +++ b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/ZonedDateTimeWithZoneIdSerializer.java @@ -1,5 +1,6 @@ package com.fasterxml.jackson.datatype.jsr310.ser; +import com.fasterxml.jackson.annotation.JsonFormat; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; @@ -30,7 +31,10 @@ protected ZonedDateTimeWithZoneIdSerializer(ZonedDateTimeWithZoneIdSerializer ba } @Override - protected JSR310FormattedSerializerBase withFormat(Boolean useTimestamp, DateTimeFormatter formatter) { + protected JSR310FormattedSerializerBase withFormat( + Boolean useTimestamp, + DateTimeFormatter formatter, + JsonFormat.Shape shape) { return new ZonedDateTimeWithZoneIdSerializer(this, useTimestamp, formatter); } diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestLocalDateSerialization.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestLocalDateSerialization.java index d1101f54..889d6788 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestLocalDateSerialization.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestLocalDateSerialization.java @@ -16,19 +16,18 @@ package com.fasterxml.jackson.datatype.jsr310; +import com.fasterxml.jackson.annotation.JsonFormat; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; import java.time.Instant; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.Month; import java.time.ZoneOffset; import java.time.temporal.Temporal; - import org.junit.Test; -import com.fasterxml.jackson.annotation.JsonFormat; -import com.fasterxml.jackson.databind.JsonMappingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.SerializationFeature; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; @@ -46,6 +45,14 @@ public Wrapper() { } public Wrapper(LocalDate v) { value = v; } } + final static class EpochDayWrapper { + @JsonFormat(shape=JsonFormat.Shape.NUMBER_INT) + public LocalDate value; + + public EpochDayWrapper() { } + public EpochDayWrapper(LocalDate v) { value = v; } + } + static class VanillaWrapper { public LocalDate value; @@ -227,4 +234,33 @@ public void testConfigOverrides() throws Exception LocalDate date2 = mapper.readValue(EXP_DATE, LocalDate.class); assertEquals(date, date2); } + + @Test + public void testConfigOverridesToEpochDay() throws Exception + { + ObjectMapper mapper = newMapper(); + mapper.configOverride(LocalDate.class) + .setFormat(JsonFormat.Value.forShape(JsonFormat.Shape.NUMBER_INT)); + LocalDate date = LocalDate.ofEpochDay(1000); + VanillaWrapper input = new VanillaWrapper(date); + final String EXP_DATE = "1000"; + String json = mapper.writeValueAsString(input); + assertEquals("{\"value\":"+EXP_DATE+"}", json); + assertEquals(EXP_DATE, mapper.writeValueAsString(date)); + + // and read back, too + VanillaWrapper output = mapper.readValue(json, VanillaWrapper.class); + assertEquals(input.value, output.value); + LocalDate date2 = mapper.readValue(EXP_DATE, LocalDate.class); + assertEquals(date, date2); + } + + @Test + public void testCustomFormatToEpochDay() throws Exception + { + EpochDayWrapper w = MAPPER.readValue("{\"value\": 1000}", EpochDayWrapper.class); + LocalDate date = w.value; + assertNotNull(date); + assertEquals(LocalDate.ofEpochDay(1000), date); + } }