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 @@ -636,6 +636,8 @@ static ObjectReader createFormattedObjectReader(Type fieldType, Class fieldClass
if (format != null && !format.isEmpty()) {
String typeName = fieldType.getTypeName();
switch (typeName) {
case "cn.hutool.core.date.DateTime":
return new ObjectReaderImplHutoolDateTime(fieldClass, format, locale);
case "java.sql.Time":
return JdbcSupport.createTimeReader((Class) fieldType, format, locale);
case "java.sql.Timestamp":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2294,6 +2294,8 @@ public ObjectReader getObjectReader(ObjectReaderProvider provider, Type type) {
}

switch (typeName) {
case "cn.hutool.core.date.DateTime":
return new ObjectReaderImplHutoolDateTime((Class) type, null, null);
case "java.sql.Time":
return JdbcSupport.createTimeReader((Class) type, null, null);
case "java.sql.Timestamp":
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.alibaba.fastjson2.reader;

import com.alibaba.fastjson2.JSONException;
import com.alibaba.fastjson2.JSONReader;

import java.lang.reflect.Constructor;
import java.lang.reflect.Type;
import java.util.Date;
import java.util.Locale;

final class ObjectReaderImplHutoolDateTime
extends ObjectReaderImplDate {
private final Class<?> objectClass;
private final Constructor<?> constructor;

ObjectReaderImplHutoolDateTime(Class<?> objectClass, String format, Locale locale) {
super(format, locale);
this.objectClass = objectClass;
try {
this.constructor = objectClass.getConstructor(long.class);
} catch (NoSuchMethodException e) {
throw new JSONException("create Hutool DateTime reader error", e);
}
}

@Override
public Class<?> getObjectClass() {
return objectClass;
}

@Override
public Object readObject(JSONReader reader, Type fieldType, Object fieldName, long features) {
return createDateTime((Date) super.readObject(reader, fieldType, fieldName, features));
}

@Override
public Object readJSONBObject(JSONReader reader, Type fieldType, Object fieldName, long features) {
return createDateTime((Date) super.readJSONBObject(reader, fieldType, fieldName, features));
}

private Object createDateTime(Date date) {
if (date == null) {
return null;
}
try {
return constructor.newInstance(date.getTime());
} catch (ReflectiveOperationException e) {
throw new JSONException("create Hutool DateTime error", e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.alibaba.fastjson2.issues_3200;

import cn.hutool.core.date.DateTime;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONB;
import com.alibaba.fastjson2.JSONFactory;
import com.alibaba.fastjson2.JSONReader;
import com.alibaba.fastjson2.annotation.JSONField;
import org.junit.jupiter.api.Test;

import java.nio.charset.StandardCharsets;
import java.util.Date;

import static org.junit.jupiter.api.Assertions.*;

public class Issue3297 {
@Test
public void testGlobalFormatRoundTrip() {
String readerFormat = JSONFactory.createReadContext().getDateFormat();
String writerFormat = JSONFactory.createWriteContext().getDateFormat();
try {
JSON.configReaderDateFormat("yyyy-MM-dd HH:mm:ss");
JSON.configWriterDateFormat("yyyy-MM-dd HH:mm:ss");
Bean input = new Bean();
input.date3 = new DateTime(1737378009000L);
Bean output = JSON.parseObject(JSON.toJSONString(input), Bean.class);
assertEquals(input.date3.getTime(), output.date3.getTime());
} finally {
JSON.configReaderDateFormat(readerFormat);
JSON.configWriterDateFormat(writerFormat);
}
}

@Test
public void testDateTimeField() {
Bean bean = JSON.parseObject("{\"date3\":\"2025-01-20 13:00:09\"}", Bean.class,
"yyyy-MM-dd HH:mm:ss");
assertEquals(DateTime.class, bean.date3.getClass());
assertEquals(JSON.parseObject("\"2025-01-20 13:00:09\"", Date.class,
"yyyy-MM-dd HH:mm:ss").getTime(), bean.date3.getTime());
}

@Test
public void testInputs() {
long millis = 1737378009000L;
String text = "{\"date3\":" + millis + "}";
assertEquals(millis, JSON.parseObject(text, Bean.class).date3.getTime());
assertEquals(millis, JSON.parseObject(text.getBytes(StandardCharsets.UTF_8), Bean.class).date3.getTime());
assertEquals(millis, JSON.parseObject(text, Bean.class, JSONReader.Feature.FieldBased).date3.getTime());
assertEquals(millis, JSON.parseObject(Long.toString(millis), DateTime.class).getTime());
assertNull(JSON.parseObject("null", DateTime.class));
assertNull(JSON.parseObject("{\"date3\":null}", Bean.class).date3);
Bean bean = new Bean();
bean.date3 = new DateTime(millis);
assertEquals(millis, JSONB.parseObject(JSONB.toBytes(bean), Bean.class).date3.getTime());
}

@Test
public void testFieldFormat() {
FormattedBean bean = JSON.parseObject("{\"date\":\"20/01/2025\"}", FormattedBean.class);
assertEquals(JSON.parseObject("\"20/01/2025\"", Date.class, "dd/MM/yyyy").getTime(), bean.date.getTime());
}

public static class Bean {
public DateTime date3;
}

public static class FormattedBean {
@JSONField(format = "dd/MM/yyyy")
public DateTime date;
}
}