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

[FEATURE]全局设置好日期格式后,不需要再在context中进行设置了 #2426

Closed
tywo45 opened this issue Apr 11, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@tywo45
Copy link

tywo45 commented Apr 11, 2024

请描述您的需求或者改进建议

在设置JSON.configWriterDateFormat("yyyy-MM-dd HH:mm:ss");后,仍然需要进行对context的设置,否则timestamp序列化时,会输出整数串

请描述你建议的实现方案

全局设置好日期格式后,应该不需要再设置context

描述您考虑过的替代方案

附加信息

@tywo45 tywo45 added the enhancement New feature or request label Apr 11, 2024
@wenshao wenshao added this to the 2.0.49 milestone Apr 12, 2024
wenshao added a commit that referenced this issue Apr 13, 2024
@wenshao
Copy link
Member

wenshao commented Apr 13, 2024

  @Test
    public void test() {
        String defaultWriterFormat = JSONFactory.getDefaultWriterFormat();
        try {
            String dateFormat = "yyyy-MM-dd'T'HH:mm:ss";
            JSON.configWriterDateFormat(dateFormat);

            JSONWriter.Context writeContext = JSONFactory.createWriteContext();
            assertEquals(dateFormat, writeContext.getDateFormat());

            Date date = new Date(1712988987882L);
            String json = JSON.toJSONString(date, writeContext);
            assertEquals("\"2024-04-13T14:16:27\"", json);

            java.sql.Timestamp ts = new Timestamp(date.getTime());
            String json1 = JSON.toJSONString(date, writeContext);
            assertEquals("\"2024-04-13T14:16:27\"", json1);
        } finally {
            JSON.configWriterDateFormat(defaultWriterFormat);
        }
        assertEquals(defaultWriterFormat, JSONFactory.getDefaultWriterFormat());
    }

设置过全局的dateFormat之后不需要额外设置,不过你要注意构造Context和设置全局dateFormat的先后顺序

@wenshao wenshao removed this from the 2.0.49 milestone Apr 13, 2024
wenshao added a commit that referenced this issue Apr 13, 2024
@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
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants