Skip to content

Commit

Permalink
Serialize LocalDateTime to string always. Fixes #284 (#311)
Browse files Browse the repository at this point in the history
* Serialize LocalDateTime to string always. Fixes #284
  • Loading branch information
graemerocher authored Nov 7, 2022
1 parent 6c6bfc0 commit bc9c5cf
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@
package io.micronaut.serde.support.serdes;

import java.io.IOException;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalQuery;

Expand Down Expand Up @@ -54,14 +51,11 @@ public TemporalQuery<LocalDateTime> query() {

@Override
protected void serializeWithoutFormat(Encoder encoder, EncoderContext context, LocalDateTime value, Argument<? extends LocalDateTime> type) throws IOException {
encoder.encodeLong(value.toInstant(ZoneOffset.UTC).toEpochMilli());
encoder.encodeString(getDefaultFormatter().format(value));
}

@Override
protected LocalDateTime deserializeNonNullWithoutFormat(Decoder decoder, DecoderContext decoderContext, Argument<? super LocalDateTime> type) throws IOException {
return LocalDateTime.ofInstant(
Instant.ofEpochMilli(decoder.decodeLong()),
ZoneId.from(ZoneOffset.UTC)
);
return getDefaultFormatter().parse(decoder.decodeString(), LocalDateTime::from);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class Test {
private $type.name value;
public void setValue($type.name value) {
this.value = value;
}
}
public $type.name getValue() {
return value;
}
Expand Down Expand Up @@ -106,7 +106,7 @@ class Test {
private $type value;
public void setValue($type value) {
this.value = value;
}
}
public $type getValue() {
return value;
}
Expand Down Expand Up @@ -141,7 +141,7 @@ class Test {
private $type.name value;
public void setValue($type.name value) {
this.value = value;
}
}
public $type.name getValue() {
return value;
}
Expand Down Expand Up @@ -185,7 +185,7 @@ class Test {
private $type.componentType.name[] value;
public void setValue($type.componentType.name[] value) {
this.value = value;
}
}
public $type.componentType.name[] getValue() {
return value;
}
Expand Down Expand Up @@ -245,7 +245,7 @@ class Test {
private $type.componentType.name[] value;
public void setValue($type.componentType.name[] value) {
this.value = value;
}
}
public $type.componentType.name[] getValue() {
return value;
}
Expand Down Expand Up @@ -287,7 +287,7 @@ class Test {
private $type value;
public void setValue($type value) {
this.value = value;
}
}
public $type getValue() {
return value;
}
Expand Down Expand Up @@ -334,7 +334,7 @@ class Test {
private $type.name value;
public void setValue($type.name value) {
this.value = value;
}
}
public $type.name getValue() {
return value;
}
Expand All @@ -355,6 +355,8 @@ class Test {
Instant | Instant.now() | { Instant i -> i.toEpochMilli() }
LocalTime | LocalTime.now() | { LocalTime i -> i.toSecondOfDay() }
LocalDate | LocalDate.now() | { LocalDate d -> d }
LocalDateTime | LocalDateTime.MAX | { LocalDateTime i -> LocalDateTime.MAX }
LocalDateTime | LocalDateTime.MIN | { LocalDateTime i -> LocalDateTime.MIN }
LocalDateTime | LocalDateTime.now() | { LocalDateTime i -> i.toInstant(ZoneOffset.from(ZoneOffset.UTC)).toEpochMilli() }
ZonedDateTime | ZonedDateTime.now() | { ZonedDateTime i -> i.toInstant().toEpochMilli() }
Duration | Duration.ofSeconds(10) | { Duration d -> d.toSeconds() }
Expand Down

0 comments on commit bc9c5cf

Please sign in to comment.