Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

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

Open
gaoxizhi opened this issue Oct 11, 2024 · 0 comments

Comments

@gaoxizhi
Copy link

问题描述

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

环境信息

请填写以下信息:

  • OS信息: MacOS 15 & centOS 7
  • JDK信息:JDK8 & JDK17
  • 版本信息:fastjson 2.0.25、2.0.39

重现步骤

  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
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant