Skip to content

Commit

Permalink
bug fix for valid, fix #87
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Apr 26, 2022
1 parent 12cfe67 commit 9ea4d39
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
11 changes: 10 additions & 1 deletion core/src/main/java/com/alibaba/fastjson2/JSONReaderStr.java
Original file line number Diff line number Diff line change
Expand Up @@ -989,6 +989,8 @@ protected void skipString() {
}

if (ch == ',') {
comma = true;

if (offset >= end) {
ch = EOI;
return;
Expand Down Expand Up @@ -1320,11 +1322,16 @@ public void skipValue() {
switch (ch) {
case '[': {
next();
for (; ; ) {
for (int i = 0; ; ++i) {
if (ch == ']') {
next();
break;
}

if (i != 0 && !comma) {
throw new JSONValidException("offset " + this.offset);
}
comma = false;
skipValue();
}
break;
Expand Down Expand Up @@ -1391,6 +1398,8 @@ public void skipValue() {
}

if (ch == ',') {
comma = true;

if (offset >= length) {
ch = EOI;
return;
Expand Down
7 changes: 7 additions & 0 deletions core/src/test/java/com/alibaba/fastjson2/JSONReaderUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.alibaba.fastjson2;

public class JSONReaderUtils {
public static JSONReader createJSONReader(String str) {
return new JSONReaderStr(JSONFactory.createReadContext(), str, 0, str.length());
}
}
51 changes: 51 additions & 0 deletions core/src/test/java/com/alibaba/fastjson2/issues/Issue87.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.alibaba.fastjson2.issues;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONException;
import com.alibaba.fastjson2.JSONReader;
import com.alibaba.fastjson2.JSONReaderUtils;
import org.junit.jupiter.api.Test;

import java.nio.charset.StandardCharsets;
Expand All @@ -26,6 +29,37 @@ public void valid_1() {
JSON.isValid(errorJson));
}

@Test
public void valid_raw_str() {
String errorJson = "[{\"a\":1}{\"b\":2}null undefined 676]";

boolean valid = true;
JSONReader jsonReader = JSONReaderUtils.createJSONReader(errorJson);
try {
jsonReader.skipValue();
} catch (JSONException error) {
valid = false;
}

assertFalse(valid);
}

@Test
public void valid_raw_str_1() {
String errorJson = "[\"a\",\"b\"]";

boolean valid = true;
JSONReader jsonReader = JSONReaderUtils.createJSONReader(errorJson);
try {
jsonReader.skipValue();
} catch (JSONException error) {
valid = false;
}

assertTrue(valid);
}


@Test
public void valid_utf8() {
byte[] errorJson = "[{\"a\":1}{\"b\":2}null undefined 676]".getBytes(StandardCharsets.UTF_8);
Expand All @@ -42,4 +76,21 @@ public void valid_utf8_1() {
assertTrue(
JSON.isValid(errorJson));
}

@Test
public void valid_ascii() {
byte[] errorJson = "[{\"a\":1}{\"b\":2}null undefined 676]".getBytes(StandardCharsets.UTF_8);
assertFalse(
JSON.isValid(errorJson, 0, errorJson.length, StandardCharsets.US_ASCII));
assertFalse(
JSON.isValidArray(errorJson)
);
}

@Test
public void valid_ascii_1() {
byte[] errorJson = "[\"a\",\"b\"]".getBytes(StandardCharsets.UTF_8);
assertTrue(
JSON.isValid(errorJson, 0, errorJson.length, StandardCharsets.US_ASCII));
}
}

0 comments on commit 9ea4d39

Please sign in to comment.