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

Fix backward compatibility with OffsetDateTime stored as double #458

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -11,6 +11,9 @@
import java.io.Serializable;
import java.lang.reflect.Type;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeParseException;
import java.util.Date;

import static java.time.format.DateTimeFormatter.ISO_OFFSET_DATE_TIME;

Expand Down Expand Up @@ -152,7 +155,12 @@ public static class OffsetDateTimeDeserializer extends JsonDeserializer<OffsetDa
@Override
public OffsetDateTime deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
if (jsonParser.getText() != null) {
return OffsetDateTime.parse(jsonParser.getText(), ISO_OFFSET_DATE_TIME);
try {
return OffsetDateTime.parse(jsonParser.getText(), ISO_OFFSET_DATE_TIME);
} catch (DateTimeParseException e) {
Date date = new Date((long) jsonParser.getDoubleValue() * 1000);
return date.toInstant().atOffset(ZoneOffset.UTC);
}
}
return null;
}
Expand Down
Expand Up @@ -11,10 +11,13 @@
import javax.persistence.Table;
import java.io.Serializable;
import java.math.BigDecimal;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

/**
* @author Vlad Mihalcea
Expand All @@ -24,13 +27,13 @@ public class GenericOffsetDateTimeJsonTest extends AbstractPostgreSQLIntegration
@Override
protected Class<?>[] entities() {
return new Class<?>[]{
Event.class,
Event.class,
};
}

@Test
public void test() {
OffsetDateTime dateTime = OffsetDateTime.of(2015, 10, 1, 9, 0 , 0, 0, ZoneOffset.ofHours(2));
OffsetDateTime dateTime = OffsetDateTime.of(2015, 10, 1, 9, 0, 0, 0, ZoneOffset.ofHours(2));

doInJPA(entityManager -> {
Location location = new Location();
Expand All @@ -50,6 +53,36 @@ public void test() {
});
}

@Test
public void testWithDouble() {
OffsetDateTime dateTime = OffsetDateTime.of(2022, 7, 14, 11, 37, 0, 0, ZoneOffset.ofHours(0));

//2022-07-14T11:37Z
String dateAsDouble = "1657798620.121624";
String json = String.format("{\n" +
"\"country\":\"Romania\",\n" +
"\"city\":\"Cluj-Napoca\",\n" +
"\"rentedAt\": %s\n" +
"}", dateAsDouble);

doInJDBC(connection -> {
String sql = "INSERT INTO EVENT (ID, LOCATION) VALUES (1, ?::JSON)";
try (
PreparedStatement statement = connection.prepareStatement(sql);
) {
statement.setObject(1, json);
statement.executeUpdate();
} catch (SQLException e) {
fail(e.getMessage());
}
});

doInJPA(entityManager -> {
Event event = entityManager.find(Event.class, 1L);
assertEquals(dateTime, event.getLocation().getRentedAt());
});
}

@Entity(name = "Event")
@Table(name = "event")
@TypeDef(defaultForType = Location.class, typeClass = JsonType.class)
Expand Down
Expand Up @@ -11,6 +11,9 @@
import java.io.Serializable;
import java.lang.reflect.Type;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeParseException;
import java.util.Date;

import static java.time.format.DateTimeFormatter.ISO_OFFSET_DATE_TIME;

Expand Down Expand Up @@ -152,7 +155,12 @@ public static class OffsetDateTimeDeserializer extends JsonDeserializer<OffsetDa
@Override
public OffsetDateTime deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
if (jsonParser.getText() != null) {
return OffsetDateTime.parse(jsonParser.getText(), ISO_OFFSET_DATE_TIME);
try {
return OffsetDateTime.parse(jsonParser.getText(), ISO_OFFSET_DATE_TIME);
} catch (DateTimeParseException e) {
Date date = new Date((long) jsonParser.getDoubleValue() * 1000);
return date.toInstant().atOffset(ZoneOffset.UTC);
}
}
return null;
}
Expand Down
Expand Up @@ -11,10 +11,13 @@
import javax.persistence.Table;
import java.io.Serializable;
import java.math.BigDecimal;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

/**
* @author Vlad Mihalcea
Expand Down Expand Up @@ -50,6 +53,36 @@ public void test() {
});
}

@Test
public void testWithDouble() {
OffsetDateTime dateTime = OffsetDateTime.of(2022, 7, 14, 11, 37, 0, 0, ZoneOffset.ofHours(0));

//2022-07-14T11:37Z
String dateAsDouble = "1657798620.121624";
String json = String.format("{\n" +
"\"country\":\"Romania\",\n" +
"\"city\":\"Cluj-Napoca\",\n" +
"\"rentedAt\": %s\n" +
"}", dateAsDouble);

doInJDBC(connection -> {
String sql = "INSERT INTO EVENT (ID, LOCATION) VALUES (1, ?::JSON)";
try (
PreparedStatement statement = connection.prepareStatement(sql);
) {
statement.setObject(1, json);
statement.executeUpdate();
} catch (SQLException e) {
fail(e.getMessage());
}
});

doInJPA(entityManager -> {
Event event = entityManager.find(Event.class, 1L);
assertEquals(dateTime, event.getLocation().getRentedAt());
});
}

@Entity(name = "Event")
@Table(name = "event")
@TypeDef(defaultForType = Location.class, typeClass = JsonType.class)
Expand Down
Expand Up @@ -11,6 +11,9 @@
import java.io.Serializable;
import java.lang.reflect.Type;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeParseException;
import java.util.Date;

import static java.time.format.DateTimeFormatter.ISO_OFFSET_DATE_TIME;

Expand Down Expand Up @@ -152,7 +155,12 @@ public static class OffsetDateTimeDeserializer extends JsonDeserializer<OffsetDa
@Override
public OffsetDateTime deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
if (jsonParser.getText() != null) {
return OffsetDateTime.parse(jsonParser.getText(), ISO_OFFSET_DATE_TIME);
try {
return OffsetDateTime.parse(jsonParser.getText(), ISO_OFFSET_DATE_TIME);
} catch (DateTimeParseException e) {
Date date = new Date((long) jsonParser.getDoubleValue() * 1000);
return date.toInstant().atOffset(ZoneOffset.UTC);
}
}
return null;
}
Expand Down
Expand Up @@ -11,10 +11,13 @@

import java.io.Serializable;
import java.math.BigDecimal;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

/**
* @author Vlad Mihalcea
Expand Down Expand Up @@ -50,6 +53,36 @@ public void test() {
});
}

@Test
public void testWithDouble() {
OffsetDateTime dateTime = OffsetDateTime.of(2022, 7, 14, 11, 37, 0, 0, ZoneOffset.ofHours(0));

//2022-07-14T11:37Z
String dateAsDouble = "1657798620.121624";
String json = String.format("{\n" +
"\"country\":\"Romania\",\n" +
"\"city\":\"Cluj-Napoca\",\n" +
"\"rentedAt\": %s\n" +
"}", dateAsDouble);

doInJDBC(connection -> {
String sql = "INSERT INTO EVENT (ID, LOCATION) VALUES (1, ?::JSON)";
try (
PreparedStatement statement = connection.prepareStatement(sql);
) {
statement.setObject(1, json);
statement.executeUpdate();
} catch (SQLException e) {
fail(e.getMessage());
}
});

doInJPA(entityManager -> {
Event event = entityManager.find(Event.class, 1L);
assertEquals(dateTime, event.getLocation().getRentedAt());
});
}

@Entity(name = "Event")
@Table(name = "event")
public static class Event {
Expand Down