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] JSONWriter.Feature.BrowserCompatible 导致 LocalDateTime 类型未按预期格式进行格式化处理 #2183

Closed
SkiffBoy opened this issue Jan 12, 2024 · 7 comments
Assignees
Labels
bug Something isn't working fixed
Milestone

Comments

@SkiffBoy
Copy link

问题描述

JSONWriter.Feature.BrowserCompatible 未对日期型数据进行格式化:
Map 中 键类型为 LocalDateTime 的数据

环境信息

  • OS信息: [MacOS 12.7 MacBook M1 ]
  • JDK信息: [Oracle OpenJDK 1.8.0_391]
  • 版本信息:[Fastjson2 2.0.44]

重现步骤

  1. 使用 JSONWriter.Feature.BrowserCompatible 作为序列化要求
  2. 序列化 LocalDateTime 作为 key 的 Map 类型数据
  3. 出现 日期型 key 未按照指定格式进行格式化 的问题

预期输出结果:
{"2024-01-12 19:55:57":1705060557443,"now":"2024-01-12 19:55:57"}
实际输出结果:
{"2024-01-12T19:55:57.443":1705060557443,"now":"2024-01-12 19:55:57"}

@Test
public void testBrowserCompatible() {
    // 全局设置:默认日期时间格式、默认时区
    JSON.configReaderZoneId(ZoneId.of("Asia/Shanghai"));
    JSON.configWriterZoneId(ZoneId.of("Asia/Shanghai"));
    JSON.configReaderDateFormat("yyyy-MM-dd HH:mm:ss");
    JSON.configWriterDateFormat("yyyy-MM-dd HH:mm:ss");

    Map data = new HashMap();
    data.put("now", LocalDateTime.now());
    data.put(LocalDateTime.now(), System.currentTimeMillis());

    final String json1 = JSONObject.toJSONString(data, JSONWriter.Feature.BrowserCompatible);
    final String json2 = JSONObject.toJSONString(data);
    System.out.println(json1);
    System.out.println(json2);
    Assertions.assertEquals(json1, json2, "日期格式处理异常");
}

期待的正确结果

期望 JSONWriter.Feature.BrowserCompatible 不会影响日期格式的序列化 yyyy-MM-dd HH:mm:ss

相关日志输出

org.opentest4j.AssertionFailedError: 日期格式处理异常 ==> 
预期:{"2024-01-12T19:55:57.443":1705060557443,"now":"2024-01-12 19:55:57"}
实际:{"2024-01-12 19:55:57":1705060557443,"now":"2024-01-12 19:55:57"}

附加信息

image
@SkiffBoy SkiffBoy added the bug Something isn't working label Jan 12, 2024
@rowstop
Copy link
Contributor

rowstop commented Jan 15, 2024

BrowserCompatible在Map里的作用等同于 WriteNonStringKeyAsString
从这里com.alibaba.fastjson2.writer.ObjectWriterImplMap#write可以看到,会直接toString

@SkiffBoy
Copy link
Author

感谢解答,目前如果要对日期型 key,Fastjson 可以统一处理吗

@rowstop
Copy link
Contributor

rowstop commented Jan 19, 2024

感谢解答,目前如果要对日期型 key,Fastjson 可以统一处理吗

我是觉得直接 toString 有点草率了,因为序列化之后可能就是String,应该获取序列化之后的结果,根据结果判定需不需要转 string

@rowstop
Copy link
Contributor

rowstop commented Jan 19, 2024

我能想到的就是根据前后的 offset 偏移量取出序列化后的值,然后可能才去重写这部分序列化的结果

@wenshao wenshao added this to the 2.0.46 milestone Jan 19, 2024
@wenshao wenshao self-assigned this Jan 19, 2024
@rowstop
Copy link
Contributor

rowstop commented Jan 19, 2024

@wenshao 这个你在做了吗?我这边做了一版,试了下没问题,要不要我提个 PR 看下?

@rowstop
Copy link
Contributor

rowstop commented Jan 19, 2024

已经提了 PR,看下是否合适

@wenshao
Copy link
Member

wenshao commented Jan 29, 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

3 participants