Skip to content

Commit

Permalink
Allow OffsetDateTime to be parsed from a Double value #457
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-lind authored and vladmihalcea committed Jul 26, 2022
1 parent dfb3f3b commit c11a3f9
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 5 deletions.
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

0 comments on commit c11a3f9

Please sign in to comment.