alibaba / alibaba/fastjson2

[BUG] 当有 provider::register 类型处理后,@JSONField 注解失效了

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

在 fastjson1 里是不会失效的;在 jackjson 里可通过代码不失效。。。以下为复现代码:

```java
public class FormatTest {
public static void main(String[] args){
//
// 当有 Provider::register 类型处理后,@JSONField 注解失效了
//
JSONWriter.Context context = new JSONWriter.Context();
context.getProvider().register(Date.class, new ObjectWriter() {
@Override
public void write(JSONWriter jsonWriter, Object o, Object o1, Type type, long l) {
jsonWriter.writeInt64(((Date)o).getTime());
}
});

CustomDateDo dateDo = new CustomDateDo();

dateDo.setDate(new Date(1673861993477L));
dateDo.setDate2(new Date(1673861993477L));

String json = JSON.toJSONString(dateDo, context);
System.out.println(json); //{"date":1673861993477,"date2":1673861993477}

assert "{\"date\":1673861993477,\"date2\":\"2023-01-16 17:39:53\"}".equals(json);
}

public static class CustomDateDo {
private Date date;

@JSONField(format = "yyyy-MM-dd HH:mm:ss")
private Date date2;

public void setDate(Date date) {
this.date = date;
}

public void setDate2(Date date2) {
this.date2 = date2;
}

public Date getDate() {
return date;
}

public Date getDate2() {
return date2;
}
}
}
```

Contributor guide

Open the contributing guide

Research direction

Start with JSONWriter.Context, provider.register, JSON.toJSONString, and the @JSONField(format) example shown in the report. Reproduce the output difference between date and date2, then trace why the registered Date writer affects both fields; done means the assertion produces the formatted date2 value while date remains a timestamp.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend-api-design
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.