Skip to content

Commit a632e14

Browse files
committed
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.
1 parent 9a1141b commit a632e14

File tree

5 files changed

+37
-3
lines changed

5 files changed

+37
-3
lines changed

benchmark/src/test/java/com/alibaba/fastjson2/benchmark/eishay/EishayParseUTF8BytesTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ public static void fastjson2() {
1616
// zulu8.62.0.19 : 703 746 710 706 700 682 717 698 526 500 474 445 425
1717
// zulu11.52.13 : 579 565 552 541 554 553 554 538 420 424 434 370
1818
// zulu17.40.19 : 600 604 597 593 578 567 447 420 380 379
19+
// zulu21.37.17 : 364
20+
// graalvm 21+35.1 : 403
1921
}
2022
}
2123

core/src/main/java/com/alibaba/fastjson2/JSONReader.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2432,8 +2432,13 @@ public Map<String, Object> readObject() {
24322432
break;
24332433
case '{':
24342434
if (isReference()) {
2435-
addResolveTask(object, name, JSONPath.of(readReference()));
2436-
val = null;
2435+
String path = readReference();
2436+
if (path.startsWith("$") || path.equals("..") || path.equals(".")) {
2437+
addResolveTask(object, name, JSONPath.of(path));
2438+
val = null;
2439+
} else {
2440+
val = path;
2441+
}
24372442
} else {
24382443
val = readObject();
24392444
}
@@ -4328,6 +4333,7 @@ public void config(Feature feature, boolean state) {
43284333

43294334
protected static final long MASK_TRIM_STRING = 1L << 14;
43304335
protected static final long MASK_EMPTY_STRING_AS_NULL = 1L << 27;
4336+
protected static final long MASK_DISABLE_REFERENCE_DETECT = 1L << 33;
43314337

43324338
public enum Feature {
43334339
FieldBased(1),
@@ -4456,7 +4462,12 @@ public enum Feature {
44564462
/**
44574463
* @since 2.0.53
44584464
*/
4459-
UseDoubleForDecimals(1L << 32L);
4465+
UseDoubleForDecimals(1L << 32L),
4466+
4467+
/**
4468+
* @since 2.0.56
4469+
*/
4470+
DisableReferenceDetect(MASK_DISABLE_REFERENCE_DETECT);
44604471

44614472
public final long mask;
44624473

core/src/main/java/com/alibaba/fastjson2/JSONReaderUTF16.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,9 @@ public final byte[] readHex() {
387387
}
388388

389389
public final boolean isReference() {
390+
if ((context.features & MASK_DISABLE_REFERENCE_DETECT) != 0) {
391+
return false;
392+
}
390393
// should be codeSize <= FreqInlineSize 325, current is 276
391394
final char[] chars = this.chars;
392395
char ch = this.ch;

core/src/main/java/com/alibaba/fastjson2/JSONReaderUTF8.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6855,6 +6855,9 @@ public final byte[] readHex() {
68556855
@Override
68566856
public final boolean isReference() {
68576857
// should be codeSize <= FreqInlineSize 325, current : 284
6858+
if ((context.features & MASK_DISABLE_REFERENCE_DETECT) != 0) {
6859+
return false;
6860+
}
68586861
final byte[] bytes = this.bytes;
68596862
int ch = this.ch;
68606863
if (ch != '{') {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.alibaba.fastjson2.issues_3300;
2+
3+
import com.alibaba.fastjson2.JSON;
4+
import com.alibaba.fastjson2.JSONObject;
5+
import com.alibaba.fastjson2.JSONReader;
6+
import org.junit.jupiter.api.Test;
7+
8+
public class Issue3347 {
9+
@Test
10+
public void test() {
11+
String json2 = "{\"*/*\":{\"schema\":{\"$ref\":\"Error-ModelName{namespace='javax.servlet.http', name='HttpServletResponse'}\"}}}";
12+
System.out.println(json2);
13+
JSONObject jsonObject4 = JSON.parseObject(json2, JSONReader.Feature.DisableReferenceDetect);
14+
}
15+
}

0 commit comments

Comments
 (0)