[BUG]JSONException: level too large : 2048
- Dominant language
- Java
- Stars
- 4.4k
- Forks
- 613
- Avg merge
- 1d 22h
- Merged PRs (30d)
- 6
Description
### 问题描述
*您好,长链表结构序列化字符串报以下错误*
```
Exception in thread "main" com.alibaba.fastjson2.JSONException: level too large : 2048
at com.alibaba.fastjson2.JSONWriterUTF16.startObject(JSONWriterUTF16.java:87)
at com.alibaba.fastjson2.writer.OWG_1_4_FlameTreeNode.write(Unknown Source)
at com.alibaba.fastjson2.writer.FieldWriterList.writeList(FieldWriterList.java:253)
at com.alibaba.fastjson2.writer.OWG_1_4_FlameTreeNode.write(Unknown Source)
at com.alibaba.fastjson2.writer.FieldWriterList.writeList(FieldWriterList.java:253)
at com.alibaba.fastjson2.writer.OWG_1_4_FlameTreeNode.write(Unknown Source)
at com.alibaba.fastjson2.writer.FieldWriterList.writeList(FieldWriterList.java:253)
at com.alibaba.fastjson2.writer.OWG_1_4_FlameTreeNode.write(Unknown Source)
at com.alibaba.fastjson2.writer.FieldWriterList.writeList(FieldWriterList.java:253)
at com.alibaba.fastjson2.writer.OWG_1_4_FlameTreeNode.write(Unknown Source)
at com.alibaba.fastjson2.writer.FieldWriterList.writeList(FieldWriterList.java:253)
```
### 环境信息
*请填写以下信息:*
- OS信息: mac
- JDK信息: Openjdk 1.8.0_312
- 版本信息:fastjson 2.0.57
### 重现步骤
*如何操作可以重现该问题:*
1. 执行以下方法可复现
```java
public class FlameTreeNode {
private String n;
private long s;
private long v;
private List c;
public FlameTreeNode(String name, long self, long total) {
this.n = name;
this.s = self;
this.v = total;
this.c = new ArrayList();
}
public FlameTreeNode() {
}
public void addChild(FlameTreeNode child) {
this.c.add(child);
}
public String getN() {
return this.n;
}
public void setN(String n) {
this.n = n;
}
public long getS() {
return this.s;
}
public void setS(long s) {
this.s = s;
}
public long getV() {
return this.v;
}
public void setV(long v) {
this.v = v;
}
public List getC() {
return this.c;
}
public void setC(List c) {
this.c = c;
}
public int countNodes() {
int count = 1;
for(FlameTreeNode child : this.c) {
count += child.countNodes();
}
return count;
}
public static void main(String[] args) throws Exception {
FlameTreeNode root = new FlameTreeNode("test", 1, 1);
FlameTreeNode parent = root;
for (int i = 0; i < 2050; i++) {
FlameTreeNode item = new FlameTreeNode("test", 1, 1);
parent.addChild(item);
parent = item;
}
System.out.println(root.countNodes());
System.out.println(JSON.toJSONString(root));
}
}
```
### 期待的正确结果
*如果存在2048限制,是否可以通过方法配置maxLevel? 实际场景中存在不少超过此阈值的. 另我尝试用反射修改maxLevel,发现maxLevel貌似大于实际链表长度也不行,必须要大很多才行,麻烦帮忙看一下*
### 相关日志输出
*请复制并粘贴任何相关的日志输出。*
#### 附加信息
*此链表无循环*
Contributor guide
Research direction
Start with the supplied FlameTreeNode main and JSON.toJSONString call, then read JSONWriterUTF16.java and FieldWriterList.java around the reported stack frames. Trace how nesting depth is checked for a non-cyclic chain over 2048 levels. Done means the reproduction has a supported outcome and regression coverage or documented configuration addresses it.
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
- Mostly clear
- Newbie friendliness
- 38/100