Skip to content

Commit

Permalink
Convert SQL Timestamp to LocalDateTime to allow custom formatters
Browse files Browse the repository at this point in the history
  • Loading branch information
greek1979 committed Feb 24, 2023
1 parent a5b6943 commit e36b2b1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@

import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
import java.util.Locale;

/**
Expand All @@ -31,6 +33,12 @@ class SqlTimestampSerializer extends AbstractDateSerializer<Timestamp> {
super(serializerBuilder);
}

@Override
protected TemporalAccessor toTemporalAccessor(Timestamp value) {
// convert SQL Timestamp into a LocalDateTime to unlock TemporalAccessor access
return LocalDateTime.ofInstant(value.toInstant(), UTC);
}

@Override
protected Instant toInstant(Timestamp value) {
return value.toInstant();
Expand Down
21 changes: 16 additions & 5 deletions src/test/java/org/eclipse/yasson/serializers/SerializersTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
Expand Down Expand Up @@ -163,7 +164,7 @@ public void testDeserializerDeserializationByType() {
JsonbConfig config = new JsonbConfig().withDeserializers(new CrateDeserializer());
Jsonb jsonb = JsonbBuilder.create(config);

Box box = createPojoWithDates();
Box box = createPojoWithDates(getExpectedDate());

String expected = "{\"boxStr\":\"Box string\",\"crate\":{\"crateInner\":{\"crateInnerBigDec\":10,\"crate_inner_str\":\"Single inner\",\"date\":\"14.05.2015 || 11:10:01\"},\"crateInnerList\":[{\"crateInnerBigDec\":10,\"crate_inner_str\":\"List inner 0\"},{\"crateInnerBigDec\":10,\"crate_inner_str\":\"List inner 1\"}],\"date\":\"2015-05-14T11:10:01\"},\"secondBoxStr\":\"Second box string\"}";

Expand Down Expand Up @@ -250,13 +251,19 @@ private static Date getExpectedDate() {
return new Calendar.Builder().setDate(2015, 4, 14).setTimeOfDay(11, 10, 1).setTimeZone(TimeZone.getTimeZone("Z")).build().getTime();
}

@Test
public void testSqlTimestampSerialization() {
Box box = createPojoWithTimestamp(new Timestamp(getExpectedDate().getTime()));
assertTrue(defaultJsonb.toJson(box).contains("\"timestamp\":\"05/14/2015 @ 11:10\""));
}

@Test
public void testSerializationUsingConversion() {
JsonbConfig config = new JsonbConfig().withSerializers(new CrateSerializerWithConversion());
Jsonb jsonb = JsonbBuilder.create(config);

String json = "{\"boxStr\":\"Box string\",\"crate\":{\"crateStr\":\"REPLACED crate str\",\"crateInner\":{\"crateInnerBigDec\":10,\"crate_inner_str\":\"Single inner\",\"date\":\"14.05.2015 || 11:10:01\"},\"crateInnerList\":[{\"crateInnerBigDec\":10,\"crate_inner_str\":\"List inner 0\"},{\"crateInnerBigDec\":10,\"crate_inner_str\":\"List inner 1\"}],\"crateBigDec\":54321,\"date-converted\":\"2015-05-14T11:10:01Z[UTC]\"},\"secondBoxStr\":\"Second box string\"}";
assertEquals(json, jsonb.toJson(createPojoWithDates()));
assertEquals(json, jsonb.toJson(createPojoWithDates(getExpectedDate())));
}

@Test
Expand Down Expand Up @@ -567,8 +574,13 @@ public void setStringProperty(String stringProperty) {
}
}

private static Box createPojoWithDates() {
Date date = getExpectedDate();
private static Box createPojoWithTimestamp(Timestamp timestamp) {
Box box = createPojo();
box.crate.timestamp = timestamp;
return box;
}

private static Box createPojoWithDates(Date date) {
Box box = createPojo();
box.crate.date = date;
box.crate.crateInner.date = date;
Expand All @@ -581,7 +593,6 @@ private static Box createPojo() {
box.crate = new Crate();
box.secondBoxStr = "Second box string";


box.crate.crateInner = createCrateInner("Single inner");

box.crate.crateInnerList = new ArrayList<>();
Expand Down
4 changes: 4 additions & 0 deletions src/test/java/org/eclipse/yasson/serializers/model/Crate.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import jakarta.json.bind.annotation.JsonbProperty;
import jakarta.json.bind.annotation.JsonbTypeSerializer;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

Expand All @@ -36,6 +37,9 @@ public class Crate {
@JsonbDateFormat("dd.MM.yyy ^ HH:mm:ss")
public Date date;

@JsonbDateFormat("MM/dd/yyy @ HH:mm")
public Timestamp timestamp;

public AnnotatedWithSerializerType annotatedType;

public AnnotatedGenericWithSerializerType<Crate> annotatedGenericType;
Expand Down

0 comments on commit e36b2b1

Please sign in to comment.