Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
gaoxizhi opened this issue Oct 11, 2024 · 2 comments
Labels
bug Something isn't working fixed
Milestone

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
@gaoxizhi gaoxizhi added the bug Something isn't working label Oct 11, 2024
@wenshao wenshao added this to the 2.0.54 milestone Oct 22, 2024
@wenshao wenshao added the fixed label Oct 23, 2024
@wenshao
Copy link
Member

wenshao commented Oct 23, 2024

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

@wenshao
Copy link
Member

wenshao commented Jan 12, 2025

https://github.com/alibaba/fastjson2/releases/tag/2.0.54
问题已修复,请用新版本

@wenshao wenshao closed this as completed Jan 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed
Projects
None yet
Development

No branches or pull requests

2 participants