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 类型时,序列化后的json会出现不同DateFormat结果 #1493

@organics2016

Description

@organics2016

问题复现代码如下 :

// 这个配置好像并不生效

        JSON.DEFFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";

        TestBean test = new TestBean();

        LocalDateTime time1 = LocalDateTime.now();
        LocalDateTime time2 = LocalDateTime.parse("2017-09-22 15:08:56", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        test.setTime1(time1);
        test.setTime2(time2);

        String json = JSON.toJSONString(test, SerializerFeature.WriteDateUseDateFormat);
        System.out.println(json);

output : {"time1":"2017-09-22T15:19:53.359","time2":"2017-09-22 15:08:56"}

输出的jsonString 会出现不同的格式。
这使的我在反序列化时出现困扰,需要为不同的格式制定不同的DateTimeFormatter.
目前只知道
jsonString 直接反序列化为对象时,是没有问题的,LocalDateTime 类型的字段会被正确赋值。
但是很多时候只是想取出jsonString 中的某个String 进行解析处理。我想知道这是否是一个BUG。

下面是我使用的版本
com.alibaba
fastjson
1.2.17

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions