Skip to content
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 @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}