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

NPE when serializing a Timestamp field with custom date-time formatter #577

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2022 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand All @@ -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
23 changes: 17 additions & 6 deletions src/test/java/org/eclipse/yasson/serializers/SerializersTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2023 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand All @@ -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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand All @@ -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