alibaba / alibaba/fastjson2

[BUG]fastJson2.x兼容版本,@JSONField(serialzeFeatures = {SerializerFeature.DisableCircularReferenceDetect}) 未生效

Open
#3,170 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

### 问题描述
*简要描述您碰到的问题。*
fastJson2.x兼容版本,@JSONField(serialzeFeatures = {SerializerFeature.DisableCircularReferenceDetect}) 未生效

### 环境信息
- OS信息: [e.g.:macOS 14.5 M1 PRO 16 GB]
- JDK信息: [e.g.:Openjdk 11.0.23]
- 版本信息:[e.g.:Fastjson2 2.0.53]

### 重现步骤
public class App {
public static class People {
private Long hairNums;
private String name;
private Integer age;
@JSONField(serialzeFeatures = {SerializerFeature.DisableCircularReferenceDetect})
private People son;
@JSONField(format = "yyyy-MM-dd")
private Date applyStartTime;

public People(Integer age, String name, Long hairNums) {
this.age = age;
this.name = name;
this.hairNums = hairNums;
}

public Integer getAge() {
return age;
}

public String getName() {
return name;
}

public Long getHairNums() {
return hairNums;
}

public People getSon() {
return son;
}

public void setSon(People son) {
this.son = son;
}

public Date getApplyStartTime() {
return applyStartTime;
}

public void setApplyStartTime(Date applyStartTime) {
this.applyStartTime = applyStartTime;
}
}

public static class IntegerToStringFilter implements ValueFilter {
@Override
public Object process(Object o, String s, Object v) {
if (Objects.isNull(v)) {
return null;
}
if (v instanceof Integer) {
return v.toString();
}
return v;
}
}

public static void main( String[] args )
{
IntegerToStringFilter integerToStringFilter = new IntegerToStringFilter();
SerializeConfig stringSerializeConfig = new SerializeConfig();
stringSerializeConfig.put(Integer.class, ToStringSerializer.instance);
stringSerializeConfig.put(Long.class, ToStringSerializer.instance);

People one = new People(10, "aaa", 10000000L);
one.setApplyStartTime(new Date());
People two = new People(40, "bbb", 0L);
two.setSon(one);
List peopleList = new ArrayList<>();
peopleList.add(one);
peopleList.add(two);
System.out.println(JSON.toJSONString(peopleList, stringSerializeConfig, integerToStringFilter));
}
}

### 期待的正确结果
[{"age":"10","applyStartTime":"2024-11-22","hairNums":"10000000","name":"aaa"},{"age":"40","hairNums":"0","name":"bbb","son":{"age":"10","applyStartTime":"2024-11-22","hairNums":"10000000","name":"aaa"}}]

### 相关日志输出
[{"age":"10","applyStartTime":"2024-11-22","hairNums":"10000000","name":"aaa"},{"age":"40","hairNums":"0","name":"bbb","son":{"$ref":"$[0]"}}]

Contributor guide

Open the contributing guide

Research direction

Start with the supplied App.main reproduction, especially the @JSONField annotation on People.son and the JSON.toJSONString call using SerializeConfig. Run it against the fastjson2 compatibility API and compare the output with the expected JSON; the issue is done when DisableCircularReferenceDetect is honored and son is serialized as an object instead of a $ref.

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
Clearly specified
Newbie friendliness
40/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.