Skip to content

[655] java.sql.Time throws java.lang.UnsupportedOperationException when serialized #671

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

Open
wants to merge 1 commit 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
Expand Up @@ -28,7 +28,7 @@ class SqlDateSerializer extends DateSerializer<Date> {

@Override
protected Instant toInstant(Date value) {
if (value instanceof java.sql.Date) {
if (value instanceof java.sql.Date || value instanceof java.sql.Time) {
// java.sql.Date doesn't have a time component, so do our best if TIME_IN_MILLIS is requested
// In the future (at a breaking change boundary) we should probably reject this code path
return Instant.ofEpochMilli(value.getTime());
Expand Down
14 changes: 14 additions & 0 deletions src/test/java/org/eclipse/yasson/serializers/SerializersTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
import org.eclipse.yasson.serializers.model.RecursiveSerializer;
import org.eclipse.yasson.serializers.model.SimpleAnnotatedSerializedArrayContainer;
import org.eclipse.yasson.serializers.model.SimpleContainer;
import org.eclipse.yasson.serializers.model.SqlTimeBean;
import org.eclipse.yasson.serializers.model.StringWrapper;
import org.eclipse.yasson.serializers.model.SupertypeSerializerPojo;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -201,6 +202,19 @@ public void testSerializerSerializationOfType() {
assertEquals(pojo.crate.crateInner.crateInnerBigDec, result.crate.crateInner.crateInnerBigDec);
}

@Test
public void testSerializerSerializationOfSqlTime() {
JsonbConfig config = new JsonbConfig().withSerializers(new CrateSerializer());
Jsonb jsonb = JsonbBuilder.create(config);
String expected = "{\"time\":\"1970-01-01T11:00:00Z[UTC]\"}";

SqlTimeBean value = new SqlTimeBean();
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
value.setTime(java.sql.Time.valueOf("11:00:00"));

assertEquals(expected, jsonb.toJson(value));
}

@Test
public void testSerializerSerializationOfTypeWithExplicitType() {
JsonbConfig config = new JsonbConfig().withSerializers(new CrateSerializer());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright (c) 2025 Red Hat, Inc. and/or its affiliates.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0,
* or the Eclipse Distribution License v. 1.0 which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
*/

package org.eclipse.yasson.serializers.model;

import java.sql.Time;

public class SqlTimeBean {

private java.sql.Time time;

public Time getTime() {
return time;
}

public void setTime(Time time) {
this.time = time;
}
}