alibaba / fastjson2

🚄 FASTJSON2 is a Java JSON library with excellent performance.
Apache License 2.0
3.75k stars 485 forks source link

[BUG] fastjson2 将时间戳解析时间为LocalDateTime,较早的时间解析异常 #3091

Open gaoxizhi opened 2 weeks ago

gaoxizhi commented 2 weeks ago

问题描述

使用fastjson2解析1970年的时间时,报错 can not cast to java.time.LocalDateTime, from class java.lang.Integer。但是fastjson不会

环境信息

请填写以下信息:

重现步骤

  1. 执行以上代码,会提示报错
  2. import com.alibaba.fastjson2.JSONObject 改为 import com.alibaba.fastjson.JSONObject;
  3. 则正常

测试代码

package net.gaox.util.date.json.deserializer.test;

import com.alibaba.fastjson2.JSONObject;
import net.gaox.util.date.json.deserializer.model.Time2;

/**
 * <p>  </p>
 *
 * @author gaox·Eric
 * @date 2024-10-10 17:21
 */
public class T2 {

    public static void main(String[] args) {

        String json = "{\"time\":0}";
        JSONObject jsonObject = JSONObject.parseObject(json);
        Time2 bean = jsonObject.toJavaObject(Time2.class);
        System.out.println(bean.getTime());

        String json2 = "{\"time\":1577836800000}";
        Time2 bean2 = JSONObject.parseObject(json2, Time2.class);
        System.out.println(bean2.getTime());
    }

}

@Data
public class Time2 {
    private LocalDateTime time;
}

异常的结果(fastjson2的结果)

Connected to the target VM, address: '127.0.0.1:58990', transport: 'socket'
Exception in thread "main" com.alibaba.fastjson2.JSONException: can not cast to java.time.LocalDateTime, from class java.lang.Integer
    at com.alibaba.fastjson2.util.TypeUtils.cast(TypeUtils.java:1521)
    at com.alibaba.fastjson2.reader.FieldReader.acceptAny(FieldReader.java:429)
    at com.alibaba.fastjson2.reader.ObjectReaderAdapter.createInstance(ObjectReaderAdapter.java:627)
    at com.alibaba.fastjson2.JSONObject.to(JSONObject.java:1248)
    at com.alibaba.fastjson2.JSONObject.toJavaObject(JSONObject.java:1258)
    at net.gaox.util.date.json.deserializer.test.T2.main(T2.java:18)
Disconnected from the target VM, address: '127.0.0.1:58990', transport: 'socket'

Process finished with exit code 1

期待的正确结果(fastjson的结果)

Connected to the target VM, address: '127.0.0.1:58981', transport: 'socket'
1970-01-01T08:00
2020-01-01T08:00
Disconnected from the target VM, address: '127.0.0.1:58981', transport: 'socket'

Process finished with exit code 0
wenshao commented 5 days ago

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.54-SNAPSHOT/ 问题已修复,请帮忙用2.0.54-SNAPSHOT验证,2.0.54版本预计在元旦前发布。