mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-22 15:10:02 +00:00
FIX: Add fallback for Ebean 11 Jsons
This commit is contained in:
1 parent
7152cf656b
commit
e2d75f4c2e
2 files changed
+44
-3
No files matched your search
@@ -11,6 +11,7 @@ import java.sql.Timestamp;
|
|||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.time.ZoneId;
|
import java.time.ZoneId;
|
||||||
|
import java.time.format.DateTimeParseException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ScalarType for java.sql.Timestamp.
|
* ScalarType for java.sql.Timestamp.
|
||||||
@@ -48,7 +49,7 @@ final class ScalarTypeLocalDateTime extends ScalarTypeBaseDateTime<LocalDateTime
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public LocalDateTime jsonRead(JsonParser parser) throws IOException {
|
public LocalDateTime jsonRead(JsonParser parser) throws IOException {
|
||||||
return LocalDateTime.parse(parser.getText());
|
return parse(parser.getText());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -63,7 +64,11 @@ final class ScalarTypeLocalDateTime extends ScalarTypeBaseDateTime<LocalDateTime
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public LocalDateTime parse(String value) {
|
public LocalDateTime parse(String value) {
|
||||||
return LocalDateTime.parse(value);
|
try {
|
||||||
|
return LocalDateTime.parse(value);
|
||||||
|
} catch (DateTimeParseException pe) {
|
||||||
|
return super.parse(value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
+37
-1
@@ -2,15 +2,20 @@ package io.ebeaninternal.server.type;
|
|||||||
|
|
||||||
import com.fasterxml.jackson.core.JsonFactory;
|
import com.fasterxml.jackson.core.JsonFactory;
|
||||||
import com.fasterxml.jackson.core.JsonGenerator;
|
import com.fasterxml.jackson.core.JsonGenerator;
|
||||||
|
import com.fasterxml.jackson.core.JsonParser;
|
||||||
import io.ebean.config.JsonConfig;
|
import io.ebean.config.JsonConfig;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.io.StringWriter;
|
import java.io.StringWriter;
|
||||||
import java.sql.Timestamp;
|
import java.sql.Timestamp;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.TimeZone;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
public class ScalarTypeLocalDateTimeTest {
|
public class ScalarTypeLocalDateTimeTest {
|
||||||
|
|
||||||
@@ -123,4 +128,35 @@ public class ScalarTypeLocalDateTimeTest {
|
|||||||
LocalDateTime value = typeIso.fromJsonISO8601(asJson);
|
LocalDateTime value = typeIso.fromJsonISO8601(asJson);
|
||||||
assertThat(localDateTime).isEqualToIgnoringNanos(value);
|
assertThat(localDateTime).isEqualToIgnoringNanos(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testParseEbean11() throws IOException {
|
||||||
|
ScalarTypeLocalDateTime type = new ScalarTypeLocalDateTime(JsonConfig.DateTime.ISO8601);
|
||||||
|
JsonFactory factory = new JsonFactory();
|
||||||
|
JsonParser parser11 = factory.createParser("1517627106000"); // its a number!
|
||||||
|
JsonParser parser13 = factory.createParser("\"2022-01-01T01:00:00\"");
|
||||||
|
|
||||||
|
// test parsing an ebean 11/13 timestamp, we do not expect an exception
|
||||||
|
LocalDateTime p = type.parse("1517627106000");
|
||||||
|
parser11.nextToken();
|
||||||
|
LocalDateTime q = type.jsonRead(parser11);
|
||||||
|
assertThat(p).isEqualTo(q);
|
||||||
|
|
||||||
|
p = type.parse("2022-01-01T01:00:00");
|
||||||
|
parser13.nextToken();
|
||||||
|
q = type.jsonRead(parser13);
|
||||||
|
assertThat(p).isEqualTo(q);
|
||||||
|
TimeZone tz = TimeZone.getDefault();
|
||||||
|
try {
|
||||||
|
// Adjust Timezone to run on build server
|
||||||
|
// Note Millis and Iso have a time offset according to TZ
|
||||||
|
TimeZone.setDefault(TimeZone.getTimeZone("Europe/Berlin"));
|
||||||
|
LocalDateTime fromMillis = type.parse("0");
|
||||||
|
LocalDateTime fromIso = type.parse("1970-01-01T01:00");
|
||||||
|
|
||||||
|
assertThat(fromMillis).isEqualToIgnoringNanos(fromIso);
|
||||||
|
} finally {
|
||||||
|
TimeZone.setDefault(tz);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user