fix(JSONReader): Add reference detection disable flag

Add support for disabling reference detection via a new feature flag (MASK_DISABLE_REFERENCE_DETECT). This improves performance in cases where reference tracking is unnecessary.
This commit is contained in:
wenshao 2025-02-21 10:25:38 +08:00
parent 9a1141ba36
commit a632e14b37
5 changed files with 37 additions and 3 deletions

View File

@ -16,6 +16,8 @@ public class EishayParseUTF8BytesTest {
// zulu8.62.0.19 : 703 746 710 706 700 682 717 698 526 500 474 445 425
// zulu11.52.13 : 579 565 552 541 554 553 554 538 420 424 434 370
// zulu17.40.19 : 600 604 597 593 578 567 447 420 380 379
// zulu21.37.17 : 364
// graalvm 21+35.1 : 403
}
}

View File

@ -2432,8 +2432,13 @@ public abstract class JSONReader
break;
case '{':
if (isReference()) {
addResolveTask(object, name, JSONPath.of(readReference()));
val = null;
String path = readReference();
if (path.startsWith("$") || path.equals("..") || path.equals(".")) {
addResolveTask(object, name, JSONPath.of(path));
val = null;
} else {
val = path;
}
} else {
val = readObject();
}
@ -4328,6 +4333,7 @@ public abstract class JSONReader
protected static final long MASK_TRIM_STRING = 1L << 14;
protected static final long MASK_EMPTY_STRING_AS_NULL = 1L << 27;
protected static final long MASK_DISABLE_REFERENCE_DETECT = 1L << 33;
public enum Feature {
FieldBased(1),
@ -4456,7 +4462,12 @@ public abstract class JSONReader
/**
* @since 2.0.53
*/
UseDoubleForDecimals(1L << 32L);
UseDoubleForDecimals(1L << 32L),
/**
* @since 2.0.56
*/
DisableReferenceDetect(MASK_DISABLE_REFERENCE_DETECT);
public final long mask;

View File

@ -387,6 +387,9 @@ final class JSONReaderUTF16
}
public final boolean isReference() {
if ((context.features & MASK_DISABLE_REFERENCE_DETECT) != 0) {
return false;
}
// should be codeSize <= FreqInlineSize 325, current is 276
final char[] chars = this.chars;
char ch = this.ch;

View File

@ -6855,6 +6855,9 @@ class JSONReaderUTF8
@Override
public final boolean isReference() {
// should be codeSize <= FreqInlineSize 325, current : 284
if ((context.features & MASK_DISABLE_REFERENCE_DETECT) != 0) {
return false;
}
final byte[] bytes = this.bytes;
int ch = this.ch;
if (ch != '{') {

View File

@ -0,0 +1,15 @@
package com.alibaba.fastjson2.issues_3300;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONReader;
import org.junit.jupiter.api.Test;
public class Issue3347 {
@Test
public void test() {
String json2 = "{\"*/*\":{\"schema\":{\"$ref\":\"Error-ModelName{namespace='javax.servlet.http', name='HttpServletResponse'}\"}}}";
System.out.println(json2);
JSONObject jsonObject4 = JSON.parseObject(json2, JSONReader.Feature.DisableReferenceDetect);
}
}