diff --git a/core/src/main/java/com/alibaba/fastjson2/JSONBDump.java b/core/src/main/java/com/alibaba/fastjson2/JSONBDump.java index 4ac46402e7..dd5f40788c 100644 --- a/core/src/main/java/com/alibaba/fastjson2/JSONBDump.java +++ b/core/src/main/java/com/alibaba/fastjson2/JSONBDump.java @@ -411,6 +411,14 @@ private void dumpAny() { jsonWriter.writeLocalDate(localDate); break; } + case BC_TIMESTAMP_WITH_TIMEZONE: { + int start = offset - 1; + try (JSONReader reader = JSONReader.ofJSONB(bytes, start, bytes.length - start, symbolTable)) { + jsonWriter.writeZonedDateTime(reader.readZonedDateTime()); + offset = reader.getOffset(); + } + break; + } case BC_TIMESTAMP: long epochSeconds = readInt64Value(); int nano = readInt32Value(); diff --git a/core/src/test/java/com/alibaba/fastjson2/jsonb/JSONBDumTest.java b/core/src/test/java/com/alibaba/fastjson2/jsonb/JSONBDumTest.java index 061e22f058..8203f08a70 100644 --- a/core/src/test/java/com/alibaba/fastjson2/jsonb/JSONBDumTest.java +++ b/core/src/test/java/com/alibaba/fastjson2/jsonb/JSONBDumTest.java @@ -12,6 +12,9 @@ import java.math.BigDecimal; import java.math.BigInteger; +import java.time.ZoneId; +import java.time.ZoneOffset; +import java.time.ZonedDateTime; import java.util.Map; import java.util.TreeMap; import java.util.concurrent.ConcurrentHashMap; @@ -21,6 +24,24 @@ @Tag("jsonb") public class JSONBDumTest { + @Test + public void testZonedDateTime() { + ZonedDateTime value = ZonedDateTime.of(2024, 1, 15, 12, 30, 45, 123456789, ZoneId.of("Europe/Paris")); + byte[] bytes = JSONB.toBytes(value); + + assertEquals(JSON.toJSONString(value), JSONB.toJSONString(bytes)); + assertEquals(JSON.toJSONString(value), JSONB.toJSONString(bytes, true)); + } + + @Test + public void testZonedDateTimeInArray() { + ZonedDateTime value = ZonedDateTime.of(2024, 1, 15, 12, 30, 45, 0, ZoneOffset.ofHours(5)); + Object[] array = {value, 42}; + byte[] bytes = JSONB.toBytes(array); + + assertEquals(JSON.parseArray(JSON.toJSONString(array)), JSON.parseArray(JSONB.toJSONString(bytes))); + } + @Test public void test_0() throws Exception { Int1 vo = new Int1();