-
Notifications
You must be signed in to change notification settings - Fork 66
PR to resolve #13 - ISO 8601 serialization of types ZonedDateTime and OffsetDateTime #15
Changes from 7 commits
40def86
092762f
1d5730e
73ab7a6
69c941c
5d88a0c
1593558
0b6aa54
5f036ff
8979c5c
108fc7d
8543a97
4122be3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ | |
|
||
import java.io.IOException; | ||
import java.time.temporal.Temporal; | ||
import java.util.function.Function; | ||
import java.util.function.ToIntFunction; | ||
import java.util.function.ToLongFunction; | ||
|
||
|
@@ -37,13 +38,23 @@ public class InstantSerializerBase<T extends Temporal> extends JSR310SerializerB | |
|
||
private final ToIntFunction<T> getNanoseconds; | ||
|
||
private final Function<T, String> toIsoString; | ||
|
||
protected InstantSerializerBase(Class<T> supportedType, ToLongFunction<T> getEpochMillis, | ||
ToLongFunction<T> getEpochSeconds, ToIntFunction<T> getNanoseconds) | ||
{ | ||
this(supportedType, getEpochMillis, getEpochSeconds, getNanoseconds, t -> t.toString()); | ||
} | ||
|
||
protected InstantSerializerBase(Class<T> supportedType, ToLongFunction<T> getEpochMillis, | ||
ToLongFunction<T> getEpochSeconds, ToIntFunction<T> getNanoseconds) | ||
ToLongFunction<T> getEpochSeconds, ToIntFunction<T> getNanoseconds, | ||
Function<T, String> toIsoString) | ||
{ | ||
super(supportedType); | ||
this.getEpochMillis = getEpochMillis; | ||
this.getEpochSeconds = getEpochSeconds; | ||
this.getNanoseconds = getNanoseconds; | ||
this.toIsoString = toIsoString; | ||
} | ||
|
||
@Override | ||
|
@@ -66,7 +77,7 @@ public void serialize(T instant, JsonGenerator generator, SerializerProvider pro | |
} | ||
else | ||
{ | ||
generator.writeString(instant.toString()); | ||
generator.writeString(this.toIsoString.apply(instant)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please disregard this. I misunderstood in which file this change was taking place.
|
||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,15 @@ | ||
package com.fasterxml.jackson.datatype.jsr310.ser; | ||
|
||
import java.time.OffsetDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
|
||
public class OffsetDateTimeSerializer extends InstantSerializerBase<OffsetDateTime> | ||
{ | ||
public static final OffsetDateTimeSerializer INSTANCE = new OffsetDateTimeSerializer(); | ||
|
||
protected OffsetDateTimeSerializer() { | ||
super(OffsetDateTime.class, dt -> dt.toInstant().toEpochMilli(), | ||
OffsetDateTime::toEpochSecond, OffsetDateTime::getNano); | ||
OffsetDateTime::toEpochSecond, OffsetDateTime::getNano, | ||
dt -> DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(dt)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't need the lambda here. You can replace:
With:
(I'm pretty sure that's the syntax, but that's off the top of my head. Compiler and tests will confirm.) |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,16 @@ | ||
package com.fasterxml.jackson.datatype.jsr310.ser; | ||
|
||
import java.time.ZonedDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
|
||
public class ZonedDateTimeSerializer extends InstantSerializerBase<ZonedDateTime> | ||
{ | ||
public static final ZonedDateTimeSerializer INSTANCE = new ZonedDateTimeSerializer(); | ||
|
||
protected ZonedDateTimeSerializer() { | ||
super(ZonedDateTime.class, dt -> dt.toInstant().toEpochMilli(), | ||
ZonedDateTime::toEpochSecond, ZonedDateTime::getNano); | ||
ZonedDateTime::toEpochSecond, ZonedDateTime::getNano, | ||
// ISO_ZONED_DATE_TIME is not the ISO format, it is an extension of it | ||
dt -> DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(dt)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't the lambda here check for a setting to determine whether to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, this PR only contained the ISO compatibility changes and introduced loss of time zone information. The idea of the |
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as a comment below: s/
t -> t.toString()
/t::toString
/, compiler to confirm if syntax is correct.