alibaba / alibaba/fastjson2

对OffsetDateTime类型做自定义序列化格式配置不生效

Open
#3,205 0 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Java
Stars
4.4k
Forks
613
Avg merge
1d 22h
Merged PRs (30d)
6

Description

### 问题描述
对OffsetDateTime类型做自定义序列化格式配置不生效

### 环境信息

- OS信息: win11 24H2 Ryzen 9 5900X
- JDK信息: Oracle JDK23.0.1
- 版本信息:2.0.53

### 重现步骤
+ fastjson2结合springboot3中,使用 JSON.register 注册全局序列化格式无效,无论设置什么格式,最终都会变成 yyyy-MM-dd HH:mm:ss

```java
JSON.register(OffsetDateTime.class, (jsonWriter, object, _, _, _) -> {
if (object == null) {
jsonWriter.writeNull();
return;
}
jsonWriter.writeString(TimeUtil.format((OffsetDateTime) object, "yyyy-MM-dd'T'HH:mm:ss.SSSZ"));
```

+ 当删除上面的全局设置后,则恢复成默认的序列化格式
+ 但是对其他类型比如:LocalDate、LocalDateTime等设置序列化的格式是可以生效的
+ 在配置切面使用JSON.toJSONString()去打印返参的时候是可以正常使用自定义配置的格式打印,但是在请求接口中的格式就不正确

![image](https://github.com/user-attachments/assets/ddf68f7f-33d8-464d-a0c8-6f39cb71499b)
![image](https://github.com/user-attachments/assets/c12644ce-3844-43e5-b056-eec6854cf8cf)

- 切面在接口返回前打印返回结果,正常显示

![image](https://github.com/user-attachments/assets/c9e01c21-05cd-4fba-ac10-45c74a717251)

Contributor guide

Open the contributing guide

Research direction

Start with the JSON.register(OffsetDateTime.class, ...) registration and compare it with JSON.toJSONString() and the Spring Boot 3 HTTP response path. Reproduce the differing formats using the reported OffsetDateTime example; the issue is done when the registered format is honored in the request response as it is in direct serialization.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring-boot
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.