Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2452,7 +2452,10 @@ static class Cache {
}

public String toJSONString() {
return com.alibaba.fastjson2.JSON.toJSONString(this, JSONWriter.Feature.ReferenceDetection);
if ((DEFAULT_GENERATE_FEATURE & SerializerFeature.DisableCircularReferenceDetect.mask) == 0) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

是不是直接调用jSON.tojosnstring(this)更好

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you mean com.alibaba.fastjson.JSON.toJSONString(this), I checked it against the current implementation: the calls are not equivalent.

The compatibility provider uses Fastjson1xWriterModule. For a JSONObject or JSONArray subclass, that writer calls JSONAware.toJSONString() again. In a standalone JDK 21 probe, giving either subclass the suggested delegating method produces StackOverflowError in both standard and reflect modes. The exact base classes work, which is why the simple object/array examples do not expose it.

The static compatibility call also changes the output for an overridden toJSONString(), QuoteFieldNames, WriteMapNullValue and a custom compatibility writer. The current implementation already delegates to com.alibaba.fastjson2.JSON.toJSONString(...); it selects the overload for DisableCircularReferenceDetect while preserving the existing provider and other behavior. I have kept that narrower change here.

中文

如果指的是兼容层 com.alibaba.fastjson.JSON.toJSONString(this),实测并不等价。它的 writer 会对 JSONObject/JSONArray 子类回调 JSONAware.toJSONString();给子类换成这个委托实现后,独立 JDK 21 探针在普通和 reflect 模式都出现栈溢出。直接使用两个基类则正常,因此简单示例看不出这个差异。

静态兼容入口还会改变自定义 toJSONString()、QuoteFieldNames、WriteMapNullValue 和兼容层自定义 writer 的输出。当前实现已经调用 core 的 com.alibaba.fastjson2.JSON.toJSONString(...),只是按 DisableCircularReferenceDetect 选择重载,保留原 provider 和其他行为,所以这里先保留这个较小的修复。

return com.alibaba.fastjson2.JSON.toJSONString(this, JSONWriter.Feature.ReferenceDetection);
}
return com.alibaba.fastjson2.JSON.toJSONString(this);
}

public String toString(SerializerFeature... features) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import com.alibaba.fastjson2.JSONException;
import com.alibaba.fastjson2.JSONFactory;
import com.alibaba.fastjson2.JSONReader;
import com.alibaba.fastjson2.JSONWriter;
import com.alibaba.fastjson2.reader.ObjectReader;
import com.alibaba.fastjson2.reader.ObjectReaderProvider;
import com.alibaba.fastjson2.util.Wrapper;
Expand Down Expand Up @@ -1024,7 +1023,7 @@ public <T> T toJavaObject(Class<T> clazz, ParserConfig config, int features) {

@Override
public String toString() {
return com.alibaba.fastjson2.JSON.toJSONString(this, JSONWriter.Feature.ReferenceDetection);
return super.toJSONString();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
package com.alibaba.fastjson.issue_7900;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class Issue7932 {
static final String OBJECT_NO_REF = "{\"listA\":[\"a\",\"b\"],\"listB\":[\"a\",\"b\"]}";
static final String OBJECT_REF = "{\"listA\":[\"a\",\"b\"],\"listB\":{\"$ref\":\"$.listA\"}}";
static final String ARRAY_NO_REF = "[[\"a\",\"b\"],[\"a\",\"b\"]]";
static final String ARRAY_REF = "[[\"a\",\"b\"],{\"$ref\":\"$[0]\"}]";
static final String OBJECT_UNQUOTED_REF = "{listA:[\"a\",\"b\"],listB:{\"$ref\":\"$.listA\"}}";
static final String OVERRIDE_DOC = "{\"overridden\":true}";

static class CustomObject extends JSONObject {
CustomObject() {
super(true);
}
}

static class CustomArray extends JSONArray {
}

static class OverrideObject extends JSONObject {
OverrideObject() {
super(true);
}

@Override
public String toJSONString() {
return OVERRIDE_DOC;
}
}

static List<String> sharedList() {
return new ArrayList<String>(Arrays.asList("a", "b"));
}

static void putSharedList(JSONObject object) {
List<String> shared = sharedList();
object.put("listA", shared);
object.put("listB", shared);
assertSame(shared, object.get("listA"));
assertSame(shared, object.get("listB"));
}

static void addSharedList(JSONArray array) {
List<String> shared = sharedList();
array.add(shared);
array.add(shared);
assertSame(shared, array.get(0));
assertSame(shared, array.get(1));
}

static JSONObject objectWithSharedList() {
JSONObject object = new JSONObject(true);
putSharedList(object);
return object;
}

static JSONArray arrayWithSharedList() {
JSONArray array = new JSONArray();
addSharedList(array);
return array;
}

@Test
public void disabledJSONObjectDefaults() {
int features = JSON.DEFAULT_GENERATE_FEATURE;
try {
JSON.DEFAULT_GENERATE_FEATURE = features | SerializerFeature.DisableCircularReferenceDetect.getMask();
JSONObject object = objectWithSharedList();

assertEquals(OBJECT_NO_REF, JSON.toJSONString(object), "static entry point");
assertEquals(OBJECT_NO_REF, object.toJSONString(), "JSON#toJSONString()");
assertEquals(OBJECT_NO_REF, object.toString(), "JSONObject#toString()");
} finally {
JSON.DEFAULT_GENERATE_FEATURE = features;
}
}

@Test
public void disabledJSONArrayDefaults() {
int features = JSON.DEFAULT_GENERATE_FEATURE;
try {
JSON.DEFAULT_GENERATE_FEATURE = features | SerializerFeature.DisableCircularReferenceDetect.getMask();
JSONArray array = arrayWithSharedList();

assertEquals(ARRAY_NO_REF, JSON.toJSONString(array), "static entry point");
assertEquals(ARRAY_NO_REF, array.toString(), "JSONArray#toString()");
assertEquals(ARRAY_NO_REF, array.toJSONString(), "inherited JSON#toJSONString()");
} finally {
JSON.DEFAULT_GENERATE_FEATURE = features;
}
}

@Test
public void defaultReferenceBehavior() {
int features = JSON.DEFAULT_GENERATE_FEATURE;
try {
JSON.DEFAULT_GENERATE_FEATURE = features & ~SerializerFeature.DisableCircularReferenceDetect.getMask();

JSONObject object = objectWithSharedList();
String objectStatic = JSON.toJSONString(object);
assertEquals(OBJECT_REF, objectStatic, "static entry point");
assertTrue(objectStatic.contains("$ref"));
assertEquals(objectStatic, object.toJSONString(), "JSON#toJSONString()");
assertEquals(objectStatic, object.toString(), "JSONObject#toString()");

JSONArray array = arrayWithSharedList();
String arrayStatic = JSON.toJSONString(array);
assertEquals(ARRAY_REF, arrayStatic, "static entry point");
assertEquals(arrayStatic, array.toString(), "JSONArray#toString()");
assertEquals(arrayStatic, array.toJSONString(), "inherited JSON#toJSONString()");
} finally {
JSON.DEFAULT_GENERATE_FEATURE = features;
}
}

// unrelated-feature control: only DisableCircularReferenceDetect is honored by the instance paths
@Test
public void unrelatedQuoteFieldNamesUnchanged() {
int features = JSON.DEFAULT_GENERATE_FEATURE;
try {
JSON.DEFAULT_GENERATE_FEATURE = features & ~SerializerFeature.QuoteFieldNames.getMask();
JSONObject object = objectWithSharedList();

assertEquals(OBJECT_UNQUOTED_REF, JSON.toJSONString(object), "static entry point honors QuoteFieldNames");
assertEquals(OBJECT_REF, object.toJSONString(), "JSON#toJSONString() keeps core quoting");
assertEquals(OBJECT_REF, object.toString(), "JSONObject#toString() keeps core quoting");
} finally {
JSON.DEFAULT_GENERATE_FEATURE = features;
}
}

@Test
public void customJSONObjectSubclass() {
int features = JSON.DEFAULT_GENERATE_FEATURE;
try {
JSON.DEFAULT_GENERATE_FEATURE = features | SerializerFeature.DisableCircularReferenceDetect.getMask();
CustomObject object = new CustomObject();
putSharedList(object);
assertEquals(OBJECT_NO_REF, JSON.toJSONString(object), "static entry point");
assertEquals(OBJECT_NO_REF, object.toJSONString(), "JSON#toJSONString()");
assertEquals(OBJECT_NO_REF, object.toString(), "JSONObject#toString()");

JSON.DEFAULT_GENERATE_FEATURE = features & ~SerializerFeature.DisableCircularReferenceDetect.getMask();
CustomObject detected = new CustomObject();
putSharedList(detected);
assertEquals(OBJECT_REF, JSON.toJSONString(detected), "static entry point");
assertEquals(OBJECT_REF, detected.toJSONString(), "JSON#toJSONString()");
assertEquals(OBJECT_REF, detected.toString(), "JSONObject#toString()");
} finally {
JSON.DEFAULT_GENERATE_FEATURE = features;
}
}

@Test
public void customJSONArraySubclass() {
int features = JSON.DEFAULT_GENERATE_FEATURE;
try {
JSON.DEFAULT_GENERATE_FEATURE = features | SerializerFeature.DisableCircularReferenceDetect.getMask();
CustomArray array = new CustomArray();
addSharedList(array);
assertEquals(ARRAY_NO_REF, JSON.toJSONString(array), "static entry point");
assertEquals(ARRAY_NO_REF, array.toString(), "JSONArray#toString()");
assertEquals(ARRAY_NO_REF, array.toJSONString(), "inherited JSON#toJSONString()");

JSON.DEFAULT_GENERATE_FEATURE = features & ~SerializerFeature.DisableCircularReferenceDetect.getMask();
CustomArray detected = new CustomArray();
addSharedList(detected);
assertEquals(ARRAY_REF, JSON.toJSONString(detected), "static entry point");
assertEquals(ARRAY_REF, detected.toString(), "JSONArray#toString()");
assertEquals(ARRAY_REF, detected.toJSONString(), "inherited JSON#toJSONString()");
} finally {
JSON.DEFAULT_GENERATE_FEATURE = features;
}
}

// recursion guard: JSONAware static route keeps the override, toString() goes through super
@Test
public void customOverrideToJSONString() {
int features = JSON.DEFAULT_GENERATE_FEATURE;
try {
JSON.DEFAULT_GENERATE_FEATURE = features | SerializerFeature.DisableCircularReferenceDetect.getMask();
OverrideObject object = new OverrideObject();
putSharedList(object);

assertEquals(OVERRIDE_DOC, JSON.toJSONString(object), "static JSONAware route");
assertEquals(OVERRIDE_DOC, object.toJSONString(), "override kept");
assertEquals(OBJECT_NO_REF, object.toString(), "JSONObject#toString() uses super");
} finally {
JSON.DEFAULT_GENERATE_FEATURE = features;
}
}
}
Loading