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

LocalDateTime包含纳秒,反序列化失败 #1987

@leafaxbyz

Description

@leafaxbyz
public class JsonExample {

    private LocalDateTime testLocalDateTime;

    public LocalDateTime getTestLocalDateTime() {
        return testLocalDateTime;
    }

    public void setTestLocalDateTime(LocalDateTime testLocalDateTime) {
        this.testLocalDateTime = testLocalDateTime;
    }
}


   public static void main(String[] args) {
//        JSON.DEFFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss:SSS";

        JsonExample example = new JsonExample();

        //test1 正确执行, test2, test3 执行出错 com.alibaba.fastjson.JSONException: can not cast to : java.time.LocalDateTime
        example.setTestLocalDateTime(LocalDateTime.now());

        //纳秒数设置为0 ,test1,test2,test3 全部正确执行
        //example.setTestLocalDateTime(LocalDateTime.now().withNano(0));
        String text = JSON.toJSONString(example, SerializerFeature.PrettyFormat);
        System.out.println(text);

        //test1, 全部可以正常执行
        JsonExample example1 = JSON.parseObject(text, JsonExample.class);
        System.out.println(JSON.toJSONString(example1));

        //test2  纳秒数为0, 可以正常执行, 不为0则报错
        JsonExample example2 = JSONObject.parseObject(text).toJavaObject(JsonExample.class);
        System.out.println(JSON.toJSONString(example2));

        //test3 纳秒数为0, 可以正常执行, 不为0则报错
        JsonExample example3 = JSON.parseObject(text).toJavaObject(JsonExample.class);
        System.out.println(JSON.toJSONString(example3));
    }

fastjson :1.2.47

错误信息:
Exception in thread "main" com.alibaba.fastjson.JSONException: can not cast to : java.time.LocalDateTime
at com.alibaba.fastjson.util.TypeUtils.castToJavaBean(TypeUtils.java:1036)
at com.alibaba.fastjson.util.TypeUtils.cast(TypeUtils.java:691)
at com.alibaba.fastjson.JSON.toJavaObject(JSON.java:1004)
at Main.main(Main.java:27)
Caused by: com.alibaba.fastjson.JSONException: can not cast to : java.time.LocalDateTime
at com.alibaba.fastjson.util.TypeUtils.cast(TypeUtils.java:803)
at com.alibaba.fastjson.util.TypeUtils.cast(TypeUtils.java:846)
at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.createInstance(JavaBeanDeserializer.java:1159)
at com.alibaba.fastjson.util.TypeUtils.castToJavaBean(TypeUtils.java:1034)
... 3 more

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions