alibaba / alibaba/fastjson2

[BUG] 序列化的对象若包含 get开头的方法并且返回对象本身,JSONObject.toJSONString 报错

Open
#2,966 1 comment 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

### 问题描述
序列化的对象若包含 get开头的方法并且返回对象本身 Caused by: com.alibaba.fastjson2.JSONException: level too large : 2048

### 环境信息
- OS信息: win10
- JDK信息:17
- 版本信息:2.0.53

### 重现步骤
```java
@Data
@Accessors(chain = true)
public class DataSource2 implements java.io.Serializable {
private static final long serialVersionUID = -2484324543464304657L;
private String name;
private String sex;
private Integer age;

public static List getData() {
DataSource2 dataSource = new DataSource2()
.setName("张三")
.setSex("男")
.setAge(24);
return Arrays.asList(dataSource);
}

public DataSource2 getXXXXX() {
return new DataSource2();
}

public static void main(String[] args) {
System.out.println(JSONObject.toJSONString(DataSource2.getData()));
}

}
```

### 期待的正确结果
*序列化时,过滤掉没有字段对应的 getXXX 方法,比如我对象不存在某个字段,若对象中有 getXXX 方法,转换的 JSON 串也包含 xXX 字段*

### 相关日志输出
Exception in thread "main" com.alibaba.fastjson.JSONException: level too large : 2048
at com.alibaba.fastjson.JSON.toJSONString(JSON.java:1653)
at com.d.demo.java.DataSource2.main(DataSource2.java:61)
Caused by: com.alibaba.fastjson2.JSONException: level too large : 2048

Contributor guide

Open the contributing guide

Research direction

Start by running the supplied DataSource2 reproduction with JSONObject.toJSONString on fastjson2 version 2.0.53, then trace how getXXXXX is discovered during serialization. Done means serialization no longer recurses until "level too large: 2048" and methods without matching fields are excluded from the JSON output.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.