Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#20 Allow LocalDate to be serialized/deserialized as number (epoch day) #22

Merged
merged 1 commit into from
Mar 27, 2017
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 @@ -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.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -45,7 +46,10 @@ protected InstantSerializer(InstantSerializer base,
}

@Override
protected JSR310FormattedSerializerBase<Instant> withFormat(Boolean useTimestamp, DateTimeFormatter formatter) {
protected JSR310FormattedSerializerBase<Instant> withFormat(
Boolean useTimestamp,
DateTimeFormatter formatter,
JsonFormat.Shape shape) {
return new InstantSerializer(this, useTimestamp, formatter);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -64,16 +65,17 @@ protected InstantSerializerBase(Class<T> supportedType, ToLongFunction<T> getEpo
protected InstantSerializerBase(InstantSerializerBase<T> base,
Boolean useTimestamp, DateTimeFormatter dtf)
{
super(base, useTimestamp, dtf);
super(base, useTimestamp, dtf, null);
defaultFormat = base.defaultFormat;
getEpochMillis = base.getEpochMillis;
getEpochSeconds = base.getEpochSeconds;
getNanoseconds = base.getNanoseconds;
}

@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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ abstract class JSR310FormattedSerializerBase<T>
*/
protected final DateTimeFormatter _formatter;

protected final JsonFormat.Shape _shape;

protected JSR310FormattedSerializerBase(Class<T> supportedType) {
this(supportedType, null);
}
Expand All @@ -63,19 +65,22 @@ protected JSR310FormattedSerializerBase(Class<T> 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
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -44,28 +45,32 @@ 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) {
super(LocalDate.class, 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<LocalDateTime> withFormat(Boolean useTimestamp, DateTimeFormatter f) {
protected JSR310FormattedSerializerBase<LocalDateTime> withFormat(Boolean useTimestamp, DateTimeFormatter f, JsonFormat.Shape shape) {
return new LocalDateTimeSerializer(this, useTimestamp, f);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<LocalTime> withFormat(Boolean useTimestamp, DateTimeFormatter dtf) {
protected JSR310FormattedSerializerBase<LocalTime> withFormat(Boolean useTimestamp, DateTimeFormatter dtf, JsonFormat.Shape shape) {
return new LocalTimeSerializer(this, useTimestamp, dtf);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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);
}

Expand Down
Loading