alibaba / alibaba/fastjson2

[BUG] 自定义LocalDateTime Reader无效

Open
#2,194 2 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

### 问题描述
*简要描述您碰到的问题。*

### 环境信息
*请填写以下信息:*

- OS信息: MacBook M1 Pro
- JDK信息: 1.8.0_312
- 版本信息:2.0.44

### 重现步骤
JSON.register(LocalDateTime.class, new LocalDateTimeReader(new SmartDateFormat())); // SmartDateFormat自己随便定一个,能识别多种格式就行

@Data
public class Person {
private Integer age = 10;
private Sex sex = Sex.F;

@JSONField(format = "yyyyMMddHHmmss")
private LocalDateTime localDateTime = LocalDateTime.now();

private LocalDate localDate = LocalDate.now();
private LocalTime localTime = LocalTime.now();
private DateTime dateTime = new DateTime();

private Integer count;
}

## main
val person1 = new Person();
val str1 = JSON.toJSONString(person1);
val p = JSON.parseObject(str1,Person.class); // 这里报错,报错堆栈可以看到并未使用自定义的SmartDateFormat来解析日期数据

Contributor guide

Open the contributing guide

Research direction

Reproduce the case starting with JSON.register, LocalDateTimeReader, the @JSONField(format = "yyyyMMddHHmmss") field, and JSON.parseObject. Trace the deserialization path and verify that the registered SmartDateFormat-based reader is used for LocalDateTime; the issue is done when the example parses successfully with the custom reader.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.