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

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

Closed
q1045243113 opened this issue Aug 1, 2018 · 1 comment
Closed

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

q1045243113 opened this issue Aug 1, 2018 · 1 comment
Labels
Milestone

Comments

@q1045243113
Copy link

q1045243113 commented Aug 1, 2018

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

@wenshao wenshao added the bug label Aug 1, 2018
@wenshao wenshao added this to the 1.2.49 milestone Aug 1, 2018
@wenshao
Copy link
Member

wenshao commented Aug 5, 2018

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

@wenshao wenshao closed this as completed Aug 5, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants