diff --git a/src/main/java/org/eclipse/yasson/internal/serializer/types/SqlTimestampSerializer.java b/src/main/java/org/eclipse/yasson/internal/serializer/types/SqlTimestampSerializer.java index 16604734..899f4d80 100644 --- a/src/main/java/org/eclipse/yasson/internal/serializer/types/SqlTimestampSerializer.java +++ b/src/main/java/org/eclipse/yasson/internal/serializer/types/SqlTimestampSerializer.java @@ -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; /** @@ -31,6 +33,12 @@ class SqlTimestampSerializer extends AbstractDateSerializer { 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(); diff --git a/src/test/java/org/eclipse/yasson/serializers/SerializersTest.java b/src/test/java/org/eclipse/yasson/serializers/SerializersTest.java index db0efdf1..f5e131fa 100644 --- a/src/test/java/org/eclipse/yasson/serializers/SerializersTest.java +++ b/src/test/java/org/eclipse/yasson/serializers/SerializersTest.java @@ -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; @@ -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\"}"; @@ -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 @@ -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; @@ -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<>(); diff --git a/src/test/java/org/eclipse/yasson/serializers/model/Crate.java b/src/test/java/org/eclipse/yasson/serializers/model/Crate.java index 5cf67daf..5d067d85 100644 --- a/src/test/java/org/eclipse/yasson/serializers/model/Crate.java +++ b/src/test/java/org/eclipse/yasson/serializers/model/Crate.java @@ -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; @@ -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 annotatedGenericType;