Skip to content

fix: support parsing Calendar subclasses such as GregorianCalendar - #7934

Open
Huang-404-Q wants to merge 1 commit into
alibaba:mainfrom
Huang-404-Q:fix/gregorian-calendar-deserialize
Open

Huang-404-Q wants to merge 1 commit into
alibaba:mainfrom
Huang-404-Q:fix/gregorian-calendar-deserialize

Conversation

@Huang-404-Q

Copy link
Copy Markdown

What this PR does / why we need it?

Calendar subclasses cannot be deserialized from the JSON produced by the writer for them.

The writer handles any Calendar subclass — ObjectWriterBaseModule uses
Calendar.class.isAssignableFrom(clazz) — so JSON.toJSONString(cal) emits a date string and
JSONB.toBytes(cal) emits the timestamp. The reader (ObjectReaderBaseModule.getObjectReader)
only special-cased the exact Calendar.class and javax.xml.datatype.XMLGregorianCalendar, so a
request for GregorianCalendar.class fell through to the generated bean reader
(ORG_1_7_GregorianCalendar). That reader expects a JSON object and fails on the string/number the
writer produced:

GregorianCalendar cal = new GregorianCalendar();
cal.setTimeInMillis(1717243200123L);

String json = JSON.toJSONString(cal);                  // "2024-06-01 20:00:00.123"
JSON.parseObject(json, Calendar.class);                // ok
JSON.parseObject(json, GregorianCalendar.class);       // JSONException: expect ':', but 26, offset 1
JSONB.parseObject(JSONB.toBytes(cal), GregorianCalendar.class);
                                                       // JSONException: fieldName not support input type TIMESTAMP_SECONDS

class Bean { public GregorianCalendar calendar; }
JSON.parseObject(JSON.toJSONString(new Bean()), Bean.class);
                                                       // same JSONException

Summary of your change

ObjectReaderBaseModule: route any java.util.Calendar subclass to ObjectReaderImplCalendar,
mirroring the writer rule. ObjectReaderImplCalendar is type-agnostic (it creates the calendar
through Calendar.getInstance()), and it is already what handles Calendar.class for exactly the
same input today. javax.xml.datatype.XMLGregorianCalendar is not a java.util.Calendar subclass,
so its branch is unchanged.

Added core/src/test/java/com/alibaba/fastjson2/primitives/GregorianCalendarTest.java, which
round-trips a GregorianCalendar through JSON text, JSONB and as a bean field, and also asserts the
existing Calendar.class behavior. It compares milliseconds rather than the serialized string, so
it does not depend on the JVM default timezone.

Verification (JDK 21, ./mvnw -pl core -Dtest=... test):

  • Before: Tests run: 1, Errors: 1 — JSONException: expect ':', but 26, offset 1 … at GregorianCalendarTest.test(GregorianCalendarTest.java:30)
  • After: GregorianCalendarTest green, checkstyle 0 violations
  • -Dtest='*Calendar*' (CalendarFieldTest, Calendar1Test, GregorianCalendarTest, CalendarTest): Tests run: 37, Failures: 0, Errors: 0

Please indicate you've done the following:

  • Made sure tests are passing and test coverage is added if needed.
  • Made sure commit message follow the rule of Conventional Commits specification.
  • Considered the docs impact and opened a new docs issue or PR with docs changes if needed.

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.
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants