[BUG] Fastjson2 针对循环链接JSON.toJSONString 会抛异常
问题描述
Fastjson2 针对循环链接进行,JSON.toJSONString 时,会导致com.alibaba.fastjson2.JSONException: level too large : 2048 异常
环境信息
请填写以下信息:
- OS信息: [MacBook Pro 13-inch, 2020 2 GHz 四核Intel Core i5 16 GB OS: Sonoma 14.1.2 (23B92)]
- JDK信息: [1.8.0_221]
- 版本信息:[Fastjson2 2.0.47]
重现步骤
如何操作可以重现该问题:
- 构建一个简单的循环链表
- 给链表填充数据
- 出现
com.alibaba.fastjson2.JSONException: level too large : 2048错误
public class CircularLinkedList implements Serializable {
private Node head;
private Node current;
public CircularLinkedList() {
this.head = null;
this.current = null;
}
// 判断循环链表是否为空
public boolean isEmpty() {
return head == null;
}
// 获取当前节点
public Node getCurrentNode() {
return current;
}
// 获取当前节点数据
public Long getCurrentData() {
if (current == null) {
log.warn("CurrentNode is empty! ");
return -1L; // 返回一个特殊值表示当前节点为空
}
return current.data;
}
// 添加元素到循环链表尾部
public void addElement(Long data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
head.next = head; // 让链表首尾相接形成循环链表
current = head; // 设置当前节点为头节点
} else {
Node tail = head;
while (tail.next != head) {
tail = tail.next;
}
tail.next = newNode;
newNode.next = head; // 让新增的节点与头节点相连形成循环
}
}
public Node getNodeByData(Long data) {
if (head == null) {
return null;
}
Node current = head;
do {
if (Objects.equals(current.data, data)) {
return current;
}
current = current.next;
} while (current != head);
return null;
}
// 设置当前节点
public void setCurrentNode(Node node) {
if (node == null || !contains(node.data)) {
log.warn("Node not in CircularLinkedList");
return;
}
current = node;
}
// 删除指定元素
public void removeElement(int data) {
if (head == null) {
return;
}
if (head.data == data) {
Node lastNode = head;
while (lastNode.next != head) {
lastNode = lastNode.next;
}
if (head == head.next) {
head = null; // 只有一个节点时直接删除
} else {
head = head.next;
lastNode.next = head; // 删除头节点并将尾节点连接到新的头节点
}
current = head; // 更新当前节点为新的头节点
} else {
Node prev = head;
Node current = head.next;
while (current != head) {
if (current.data == data) {
prev.next = current.next; // 将上一个节点与当前节点的下一个节点相连,跳过当前节点实现删除
if (current == this.current) {
this.current = this.current.next; // 若删除的是当前节点,则更新当前节点为下一个节点
}
return;
}
prev = current;
current = current.next;
}
}
}
// 查询元素是否存在
public boolean contains(Long data) {
if (head == null) {
return false;
}
Node current = head;
do {
if (Objects.equals(current.data, data)) {
return true;
}
current = current.next;
} while (current != head);
return false;
}
// 打印循环链表元素
public String printList() {
if (head == null) {
log.warn("CircularLinkedList is empty.");
return StringUtils.EMPTY;
}
StringBuilder stringBuilder = new StringBuilder();
Node current = head;
do {
log.info(current.data + " ");
stringBuilder.append(current.data).append(" ");
current = current.next;
} while (current != head);
return stringBuilder.toString();
}
public static class Node implements Serializable {
public Long data;
public Node next;
public Node() {
}
public Node(Long data) {
this.data = data;
}
public Node(Long data, Node next) {
this.data = data;
this.next = next;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Node node = (Node) o;
return Objects.equals(data, node.data) && Objects.equals(next, node.next);
}
@Override
public int hashCode() {
return Objects.hash(data, next);
}
}
}
// 测试代码
public static void main(String[] args) {
CircularLinkedList circularLinkedList = new CircularLinkedList();
circularLinkedList.addElement(2L);
circularLinkedList.addElement(10L);
circularLinkedList.addElement(30L);
log.info("测试打印 {}",JSON.toJSONString(circularLinkedList));
}
期待的正确结果
在Fastjson1中,不会出现无限递归的现象
Fastjson1 中的结果: {"currentData":2,"currentNode":{"data":2,"next":{"data":10,"next":{"data":30,"next":{"$ref":"$.currentNode"}}}},"empty":false}
相关日志输出
Exception in thread "main" com.alibaba.fastjson2.JSONException: level too large : 2048 at com.alibaba.fastjson2.JSONWriterUTF16.startObject(JSONWriterUTF16.java:122) at com.alibaba.fastjson2.writer.OWG_2_2_Node.write(Unknown Source) at com.alibaba.fastjson2.writer.OWG_2_2_Node.write(Unknown Source) at com.alibaba.fastjson2.writer.OWG_2_2_Node.write(Unknown Source) at com.alibaba.fastjson2.writer.OWG_2_2_Node.write(Unknown Source)
JSON.toJSONString(circularLinkedList,JSONWriter.Feature.ReferenceDetection)
JSON.toJSONString(circularLinkedList,JSONWriter.Feature.ReferenceDetection)
谢了,我关闭下该问题