Skip to content

Commit

Permalink
validate support single quote, for issue #2059
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Nov 30, 2023
1 parent 25166fa commit 34a2e46
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 3 deletions.
37 changes: 36 additions & 1 deletion core/src/main/java/com/alibaba/fastjson2/JSON.java
Original file line number Diff line number Diff line change
Expand Up @@ -3275,6 +3275,26 @@ static boolean isValid(String text) {
}
}

/**
* Verify that the json string is legal json text
*
* @param text the specified string will be validated
* @param features the specified features is applied to parsing
* @return {@code true} or {@code false}
*/
static boolean isValid(String text, JSONReader.Feature... features) {
if (text == null || text.isEmpty()) {
return false;
}

try (JSONReader jsonReader = JSONReader.of(text, JSONFactory.createReadContext(features))) {
jsonReader.skipValue();
return jsonReader.isEnd() && !jsonReader.comma;
} catch (JSONException error) {
return false;
}
}

/**
* Verify that the json char array is legal json text
*
Expand Down Expand Up @@ -3379,6 +3399,21 @@ static boolean isValid(byte[] bytes) {
}
}

/**
* Verify that the json byte array is legal json text
*
* @param bytes the specified array will be validated
* @param charset the specified charset of the bytes
* @return {@code true} or {@code false}
*/
static boolean isValid(byte[] bytes, Charset charset) {
if (bytes == null || bytes.length == 0) {
return false;
}

return isValid(bytes, 0, bytes.length, charset);
}

/**
* Verify that the json byte array is a legal JsonArray
*
Expand Down Expand Up @@ -3407,7 +3442,7 @@ static boolean isValidArray(byte[] bytes) {
* @param bytes the specified array will be validated
* @param offset the starting index of array
* @param length the specified length of array
* @param charset the specified charset of the stream
* @param charset the specified charset of the bytes
* @return {@code true} or {@code false}
*/
static boolean isValid(byte[] bytes, int offset, int length, Charset charset) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4313,6 +4313,7 @@ public final void skipValue() {
}
break;
}
case '\'':
case '"': {
skipString();
break;
Expand Down
6 changes: 4 additions & 2 deletions core/src/main/java/com/alibaba/fastjson2/JSONReaderUTF8.java
Original file line number Diff line number Diff line change
Expand Up @@ -5448,9 +5448,10 @@ protected void readString0() {

@Override
public final boolean skipName() {
if (ch != '"') {
if (ch != '"' && ch != '\'') {
throw new JSONException("not support unquoted name");
}
char quote = ch;

int offset = this.offset;
for (; ; ) {
Expand All @@ -5460,7 +5461,7 @@ public final boolean skipName() {
continue;
}

if (c == '"') {
if (c == quote) {
offset++;
c = bytes[offset];

Expand Down Expand Up @@ -5523,6 +5524,7 @@ public final void skipValue() {
}
break;
}
case '\'':
case '"': {
skipString();
break;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.alibaba.fastjson2.issues_2000;

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

import java.nio.charset.StandardCharsets;

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

public class Issue2059 {
@Test
public void test() {
String str1 = "{'queryParam':{ 'STATUS': '1','MENU_TYPE': '11'}}";
assertTrue(
JSON.isValid(str1));
assertTrue(
JSON.isValid(str1.toCharArray()));
assertTrue(
JSON.isValid(
str1.getBytes(StandardCharsets.UTF_8)));
assertTrue(
JSON.isValid(
str1.getBytes(StandardCharsets.UTF_8),
StandardCharsets.ISO_8859_1));
assertTrue(
JSON.isValid(
str1.getBytes(StandardCharsets.UTF_8),
StandardCharsets.US_ASCII));
}
}

0 comments on commit 34a2e46

Please sign in to comment.