Skip to content

[BUG]JSONException: level too large : 2048 #3657

Description

@zhangzhen91

问题描述

您好,长链表结构序列化字符串报以下错误

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. 执行以下方法可复现
public class FlameTreeNode {
    private String n;
    private long s;
    private long v;
    private List<FlameTreeNode> 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<FlameTreeNode> getC() {
        return this.c;
    }

    public void setC(List<FlameTreeNode> 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貌似大于实际链表长度也不行,必须要大很多才行,麻烦帮忙看一下

相关日志输出

请复制并粘贴任何相关的日志输出。

附加信息

此链表无循环

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions