Repository navigation
fix: support parsing Calendar subclasses such as GregorianCalendar - #7934
Open
Huang-404-Q wants to merge 1 commit into
Open
Huang-404-Q wants to merge 1 commit into
Huang-404-Q wants to merge 1 commit into
Conversation
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.
|
|
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this PR does / why we need it?
Calendarsubclasses cannot be deserialized from the JSON produced by the writer for them.The writer handles any
Calendarsubclass —ObjectWriterBaseModuleusesCalendar.class.isAssignableFrom(clazz)— soJSON.toJSONString(cal)emits a date string andJSONB.toBytes(cal)emits the timestamp. The reader (ObjectReaderBaseModule.getObjectReader)only special-cased the exact
Calendar.classandjavax.xml.datatype.XMLGregorianCalendar, so arequest for
GregorianCalendar.classfell through to the generated bean reader(
ORG_1_7_GregorianCalendar). That reader expects a JSON object and fails on the string/number thewriter produced:
Summary of your change
ObjectReaderBaseModule: route anyjava.util.Calendarsubclass toObjectReaderImplCalendar,mirroring the writer rule.
ObjectReaderImplCalendaris type-agnostic (it creates the calendarthrough
Calendar.getInstance()), and it is already what handlesCalendar.classfor exactly thesame input today.
javax.xml.datatype.XMLGregorianCalendaris not ajava.util.Calendarsubclass,so its branch is unchanged.
Added
core/src/test/java/com/alibaba/fastjson2/primitives/GregorianCalendarTest.java, whichround-trips a
GregorianCalendarthrough JSON text, JSONB and as a bean field, and also asserts theexisting
Calendar.classbehavior. It compares milliseconds rather than the serialized string, soit does not depend on the JVM default timezone.
Verification (JDK 21,
./mvnw -pl core -Dtest=... test):Tests run: 1, Errors: 1—JSONException: expect ':', but 26, offset 1 … at GregorianCalendarTest.test(GregorianCalendarTest.java:30)GregorianCalendarTestgreen, checkstyle 0 violations-Dtest='*Calendar*'(CalendarFieldTest, Calendar1Test, GregorianCalendarTest, CalendarTest):Tests run: 37, Failures: 0, Errors: 0Please indicate you've done the following: