From 43a57dfef0b840cf078510e269e81156be1af295 Mon Sep 17 00:00:00 2001 From: huang <13165998033@163.com> Date: Wed, 28 Oct 2026 18:04:26 +0800 Subject: [PATCH] fix: support parsing Calendar subclasses such as GregorianCalendar The writer handles any `Calendar` subclass (`Calendar.class.isAssignableFrom(clazz)` in `ObjectWriterBaseModule`), but the reader only special-cased the exact `Calendar.class` and `javax.xml.datatype.XMLGregorianCalendar`. A lookup for `GregorianCalendar.class` therefore fell through to the generated bean reader, which expects a JSON object and fails on the string/number the writer produces: GregorianCalendar cal = new GregorianCalendar(); cal.setTimeInMillis(1717243200123L); String json = JSON.toJSONString(cal); JSON.parseObject(json, Calendar.class); // ok JSON.parseObject(json, GregorianCalendar.class); // JSONException: expect ':', ... The same failure happens for a bean field typed `GregorianCalendar`, and with JSONB. Route `Calendar` subclasses to `ObjectReaderImplCalendar` too, mirroring the writer rule. --- .../reader/ObjectReaderBaseModule.java | 5 +- .../primitives/GregorianCalendarTest.java | 48 +++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 core/src/test/java/com/alibaba/fastjson2/primitives/GregorianCalendarTest.java diff --git a/core/src/main/java/com/alibaba/fastjson2/reader/ObjectReaderBaseModule.java b/core/src/main/java/com/alibaba/fastjson2/reader/ObjectReaderBaseModule.java index 9b732c3ea5..792a094956 100644 --- a/core/src/main/java/com/alibaba/fastjson2/reader/ObjectReaderBaseModule.java +++ b/core/src/main/java/com/alibaba/fastjson2/reader/ObjectReaderBaseModule.java @@ -1833,7 +1833,10 @@ public ObjectReader getObjectReader(ObjectReaderProvider provider, Type type) { return typedMap((Class) type, TreeMap.class, null, Object.class); } - if (type == Calendar.class || "javax.xml.datatype.XMLGregorianCalendar".equals(typeName)) { + if (type == Calendar.class + || (type instanceof Class && Calendar.class.isAssignableFrom((Class) type)) + || "javax.xml.datatype.XMLGregorianCalendar".equals(typeName) + ) { return ObjectReaderImplCalendar.INSTANCE; } diff --git a/core/src/test/java/com/alibaba/fastjson2/primitives/GregorianCalendarTest.java b/core/src/test/java/com/alibaba/fastjson2/primitives/GregorianCalendarTest.java new file mode 100644 index 0000000000..4a993f66d9 --- /dev/null +++ b/core/src/test/java/com/alibaba/fastjson2/primitives/GregorianCalendarTest.java @@ -0,0 +1,48 @@ +package com.alibaba.fastjson2.primitives; + +import com.alibaba.fastjson2.JSON; +import com.alibaba.fastjson2.JSONB; +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; + +import java.util.Calendar; +import java.util.GregorianCalendar; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; + +@Tag("primitives") +public class GregorianCalendarTest { + private static final long MILLIS = 1717243200123L; // 2024-06-01T12:00:00.123Z + + @Test + public void test() { + GregorianCalendar calendar = new GregorianCalendar(); + calendar.setTimeInMillis(MILLIS); + + // the object writer handles any Calendar subclass + String json = JSON.toJSONString(calendar); + + // ... so the object reader must too, not only the exact Calendar class + Calendar calendarParsed = JSON.parseObject(json, Calendar.class); + assertEquals(MILLIS, calendarParsed.getTimeInMillis()); + + GregorianCalendar parsed = JSON.parseObject(json, GregorianCalendar.class); + assertInstanceOf(GregorianCalendar.class, parsed); + assertEquals(MILLIS, parsed.getTimeInMillis()); + + GregorianCalendar parsedJSONB = JSONB.parseObject(JSONB.toBytes(calendar), GregorianCalendar.class); + assertInstanceOf(GregorianCalendar.class, parsedJSONB); + assertEquals(MILLIS, parsedJSONB.getTimeInMillis()); + + Bean bean = new Bean(); + bean.calendar = calendar; + Bean beanParsed = JSON.parseObject(JSON.toJSONString(bean), Bean.class); + assertInstanceOf(GregorianCalendar.class, beanParsed.calendar); + assertEquals(MILLIS, beanParsed.calendar.getTimeInMillis()); + } + + public static class Bean { + public GregorianCalendar calendar; + } +}