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] JSON.toJSON(Bean) with Date #2550

Closed
Cooper-Zhong opened this issue May 10, 2024 · 3 comments
Closed

[BUG] JSON.toJSON(Bean) with Date #2550

Cooper-Zhong opened this issue May 10, 2024 · 3 comments
Labels
bug Something isn't working fixed
Milestone

Comments

@Cooper-Zhong
Copy link
Contributor

问题描述

可能是修改了对date的处理,以下代码在fastjson 2.0.49通过,在fastjson2-2.0.49 和 fastjson 2.0.50-SNAPSHOT中异常,也许有问题。

环境信息

  • OS信息: [MacOS 12.7.4 M1 Pro 16 GB]
  • JDK信息: [Openjdk 17.0.6]
  • 版本信息:[Fastjson 2.0.49/2.0.50-SNAPSHOT]

重现步骤

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.annotation.JSONField;
import org.junit.jupiter.api.Test;

import java.util.Date;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class MutatedIssue460_326_1 {

    @Test
    public void mutatedTest1() {
        Bean1 bean = new Bean1();
        bean.date = new Date(1655097829796L);
        String str = JSON.toJSON(bean).toString();
        assertEquals("{\"date\":1655097829796}", str);

        Bean1 bean1 = JSON.parseObject(str, Bean1.class);
        assertEquals(bean.date.getTime(), bean1.date.getTime());
        Bean1 bean2 = JSON.parseObject(str).toJavaObject(Bean1.class);
        assertEquals(bean.date.getTime(), bean2.date.getTime());
    }

    public static class Bean1 {
        @JSONField(format = "millis")
        public Date date;
    }

    @Test
    public void mutatedTest2() {
        Bean2 bean = new Bean2();
        bean.date = new Date(1655097829796L);
        String str = JSON.toJSON(bean).toString();
        assertEquals("{\"date\":1655097829796}", str);

        Bean2 bean1 = JSON.parseObject(str, Bean2.class);
        assertEquals(bean.date.getTime(), bean1.date.getTime());
        Bean2 bean2 = JSON.parseObject(str).toJavaObject(Bean2.class);
        assertEquals(bean.date.getTime(), bean2.date.getTime());
    }

    public static class Bean2 {
        @JSONField(format = "millis")
        public Date date;
    }

    @Test
    public void mutatedTest1fj() {
        Bean1fj bean = new Bean1fj();
        bean.date = new Date(1655097829796L);
        String str = com.alibaba.fastjson.JSON.toJSON(bean).toString();
        assertEquals("{\"date\":1655097829796}", str);

        Bean1fj bean1 = com.alibaba.fastjson.JSON.parseObject(str, Bean1fj.class);
        assertEquals(bean.date.getTime(), bean1.date.getTime());
        Bean1fj bean2 = com.alibaba.fastjson.JSON.parseObject(str).toJavaObject(Bean1fj.class);
        assertEquals(bean.date.getTime(), bean2.date.getTime());
    }

    public static class Bean1fj {
        @com.alibaba.fastjson.annotation.JSONField(format = "millis")
        public Date date;
    }

    @Test
    public void mutatedTest2fj() {
        Bean2fj bean = new Bean2fj();
        bean.date = new Date(1655097829796L);
        String str = com.alibaba.fastjson.JSON.toJSON(bean).toString();
        assertEquals("{\"date\":1655097829796}", str);

        Bean2fj bean1 = com.alibaba.fastjson.JSON.parseObject(str, Bean2fj.class);
        assertEquals(bean.date.getTime(), bean1.date.getTime());
        Bean2fj bean2 = com.alibaba.fastjson.JSON.parseObject(str).toJavaObject(Bean2fj.class);
        assertEquals(bean.date.getTime(), bean2.date.getTime());
    }

    public static class Bean2fj {
        @com.alibaba.fastjson.annotation.JSONField(format = "millis")
        public Date date;
    }
}

期待的正确结果

(可能源码实现修改了?)期望正常序列化

相关日志输出

java.lang.IllegalArgumentException: Unknown pattern letter: i

at java.base/java.time.format.DateTimeFormatterBuilder.parsePattern(DateTimeFormatterBuilder.java:1882)
at java.base/java.time.format.DateTimeFormatterBuilder.appendPattern(DateTimeFormatterBuilder.java:1772)
at java.base/java.time.format.DateTimeFormatter.ofPattern(DateTimeFormatter.java:566)
at com.alibaba.fastjson2.util.DateUtils.format(DateUtils.java:10993)
at com.alibaba.fastjson2.writer.ObjectWriterAdapter.toJSONObject(ObjectWriterAdapter.java:619)
at com.alibaba.fastjson2.JSON.toJSON(JSON.java:3726)
at com.alibaba.fastjson2.JSON.toJSON(JSON.java:3700)
at MutatedIssue460_326_1.mutatedTest1(MutatedIssue460_326_1.java:18)
@Cooper-Zhong Cooper-Zhong added the bug Something isn't working label May 10, 2024
@wenshao wenshao added this to the 2.0.50 milestone May 10, 2024
@wenshao wenshao added the fixed label May 10, 2024
@wenshao
Copy link
Member

wenshao commented May 10, 2024

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson/2.0.50-SNAPSHOT/
问题已修复,请帮忙用2.0.50-SNAPSHOT版本验证下

@Cooper-Zhong
Copy link
Contributor Author

验证已修复,辛苦wenshao

@wenshao
Copy link
Member

wenshao commented May 12, 2024

https://github.com/alibaba/fastjson2/releases/tag/2.0.50
2.0.50 has been released, please use the new version

@wenshao wenshao closed this as completed May 12, 2024
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