diff --git a/ebean-core/src/main/java/io/ebeaninternal/server/type/ScalarTypeLocalDateTime.java b/ebean-core/src/main/java/io/ebeaninternal/server/type/ScalarTypeLocalDateTime.java index a4bc91aaea..12b884e828 100644 --- a/ebean-core/src/main/java/io/ebeaninternal/server/type/ScalarTypeLocalDateTime.java +++ b/ebean-core/src/main/java/io/ebeaninternal/server/type/ScalarTypeLocalDateTime.java @@ -1,7 +1,10 @@ package io.ebeaninternal.server.type; +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.core.JsonParser; import io.ebean.config.JsonConfig; +import java.io.IOException; import java.sql.Timestamp; import java.sql.Types; import java.time.Instant; @@ -29,12 +32,37 @@ public long convertToMillis(LocalDateTime value) { @Override protected String toJsonNanos(LocalDateTime value) { - return String.valueOf(convertToMillis(value)); + return value.toString(); } @Override - protected String toJsonISO8601(LocalDateTime dateTime) { - return dateTime.atZone(ZoneId.systemDefault()).toInstant().toString(); + protected String toJsonISO8601(LocalDateTime value) { + return value.toString(); + } + + @Override + protected LocalDateTime fromJsonISO8601(String value) { + return LocalDateTime.parse(value); + } + + @Override + public LocalDateTime jsonRead(JsonParser parser) throws IOException { + return LocalDateTime.parse(parser.getText()); + } + + @Override + public void jsonWrite(JsonGenerator writer, LocalDateTime value) throws IOException { + writer.writeString(value.toString()); + } + + @Override + public String formatValue(LocalDateTime value) { + return value.toString(); + } + + @Override + public LocalDateTime parse(String value) { + return LocalDateTime.parse(value); } @Override diff --git a/ebean-core/src/test/java/io/ebeaninternal/server/type/ScalarTypeLocalDateTimeTest.java b/ebean-core/src/test/java/io/ebeaninternal/server/type/ScalarTypeLocalDateTimeTest.java index 88803da744..65423e0126 100644 --- a/ebean-core/src/test/java/io/ebeaninternal/server/type/ScalarTypeLocalDateTimeTest.java +++ b/ebean-core/src/test/java/io/ebeaninternal/server/type/ScalarTypeLocalDateTimeTest.java @@ -1,25 +1,28 @@ package io.ebeaninternal.server.type; +import com.fasterxml.jackson.core.JsonFactory; +import com.fasterxml.jackson.core.JsonGenerator; import io.ebean.config.JsonConfig; import org.junit.Test; +import java.io.StringWriter; import java.sql.Timestamp; import java.time.LocalDateTime; import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.*; public class ScalarTypeLocalDateTimeTest { - ScalarTypeLocalDateTime type = new ScalarTypeLocalDateTime(JsonConfig.DateTime.MILLIS); + private final ScalarTypeLocalDateTime type = new ScalarTypeLocalDateTime(JsonConfig.DateTime.MILLIS); + + private final JsonFactory factory = new JsonFactory(); // warm up - LocalDateTime warmUp = LocalDateTime.now(); + private final LocalDateTime warmUp = LocalDateTime.now(); @Test - public void testNowToMillis() throws Exception { + public void testNowToMillis() { warmUp.hashCode(); @@ -29,7 +32,7 @@ public void testNowToMillis() throws Exception { } @Test - public void testConvertToMillis() throws Exception { + public void testConvertToMillis() { LocalDateTime now = LocalDateTime.now().withNano(123_000_000); // jdk11 workaround long asMillis = type.convertToMillis(now); @@ -39,7 +42,7 @@ public void testConvertToMillis() throws Exception { } @Test - public void testConvertFromTimestamp() throws Exception { + public void testConvertFromTimestamp() { Timestamp now = new Timestamp(System.currentTimeMillis()); @@ -74,6 +77,24 @@ public void testToBeanType() throws Exception { assertEquals(timestamp, timestamp1); } + + @Test + public void testJsonRaw() throws Exception { + + final LocalDateTime of = LocalDateTime.of(2020, 5, 4, 13, 20, 40); + + ScalarTypeLocalDateTime typeIso = new ScalarTypeLocalDateTime(JsonConfig.DateTime.ISO8601); + + StringWriter writer = new StringWriter(); + JsonGenerator generator = factory.createGenerator(writer); + + typeIso.jsonWrite(generator, of); + generator.flush(); + + assertThat(of.toString()).isEqualTo("2020-05-04T13:20:40"); + assertThat(writer.toString()).isEqualTo("\"2020-05-04T13:20:40\""); + } + @Test public void testJson() throws Exception {