feat(#871-strictMode): removed allowSingleQuotes

test(#871-strictMode): adjusted related tests, add more test cases for non-compliant quotes in strict mode
This commit is contained in:
rikkarth 2024-03-30 18:44:51 +00:00
parent c0918c2428
commit 46534b56ad
No known key found for this signature in database
GPG Key ID: 11E5F28B0AED6AC7
3 changed files with 18 additions and 37 deletions

View File

@ -6,12 +6,10 @@ package org.json;
public class JSONParserConfiguration extends ParserConfiguration { public class JSONParserConfiguration extends ParserConfiguration {
/** Original Configuration of the JSON Parser. */ /** Original Configuration of the JSON Parser. */
public static final JSONParserConfiguration ORIGINAL public static final JSONParserConfiguration ORIGINAL = new JSONParserConfiguration();
= new JSONParserConfiguration();
/** Original configuration of the JSON Parser except that values are kept as strings. */ /** Original configuration of the JSON Parser except that values are kept as strings. */
public static final JSONParserConfiguration KEEP_STRINGS public static final JSONParserConfiguration KEEP_STRINGS = new JSONParserConfiguration().withKeepStrings(true);
= new JSONParserConfiguration().withKeepStrings(true);
/** /**
* Used to indicate whether to overwrite duplicate key or not. * Used to indicate whether to overwrite duplicate key or not.
@ -97,24 +95,6 @@ public class JSONParserConfiguration extends ParserConfiguration {
return clone; return clone;
} }
/**
* Allows single quotes mode configuration for JSON parser when strictMode is on.
* <p>
* If this option is set to true when strict Mode is enabled, the parser will allow single quoted fields.
* <p>
* This option is false by default.
*
* @param mode a boolean value indicating whether single quotes should be allowed or not
* @return a new JSONParserConfiguration instance with the updated strict mode setting
*/
public JSONParserConfiguration allowSingleQuotes(final boolean mode) {
JSONParserConfiguration clone = this.clone();
clone.strictMode = this.strictMode;
clone.allowSingleQuotes = mode;
return clone;
}
/** /**
* The parser's behavior when meeting duplicate keys, controls whether the parser should * The parser's behavior when meeting duplicate keys, controls whether the parser should
* overwrite duplicate keys or not. * overwrite duplicate keys or not.
@ -138,14 +118,4 @@ public class JSONParserConfiguration extends ParserConfiguration {
public boolean isStrictMode() { public boolean isStrictMode() {
return this.strictMode; return this.strictMode;
} }
/**
* Retrieves the allow single quotes option.
* <p>
* Allow Single Quotes, when enabled during strict mode, instructs the parser to allow single quoted JSON fields.
* The parser will not throw a JSONException if compliant single quoted fields are found in the JSON structure.
*
* @return the current allow single quotes setting.
*/
public boolean isAllowSingleQuotes() {return this.allowSingleQuotes;}
} }

View File

@ -472,9 +472,8 @@ public class JSONTokener {
Object nextSimpleValue(char c, JSONParserConfiguration jsonParserConfiguration) { Object nextSimpleValue(char c, JSONParserConfiguration jsonParserConfiguration) {
boolean strictMode = jsonParserConfiguration.isStrictMode(); boolean strictMode = jsonParserConfiguration.isStrictMode();
boolean allowSingleQuotes = jsonParserConfiguration.isAllowSingleQuotes();
if(strictMode && !allowSingleQuotes && c == '\''){ if(strictMode && c == '\''){
throw this.syntaxError("Single quote wrap not allowed in strict mode"); throw this.syntaxError("Single quote wrap not allowed in strict mode");
} }

View File

@ -118,24 +118,36 @@ public class JSONParserConfigurationTest {
} }
@Test @Test
public void givenUnbalancedQuotes_testStrictModeTrueAndAllowSingleQuotes_shouldThrowJsonExceptionWtihConcreteErrorDescription() { public void givenNonCompliantQuotes_testStrictModeTrue_shouldThrowJsonExceptionWithConcreteErrorDescription() {
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration() JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
.withStrictMode(true).allowSingleQuotes(true); .withStrictMode(true);
String testCaseOne = "[\"abc', \"test\"]"; String testCaseOne = "[\"abc', \"test\"]";
String testCaseTwo = "['abc\", \"test\"]"; String testCaseTwo = "['abc\", \"test\"]";
String testCaseThree = "['abc']";
String testCaseFour = "[{'testField': \"testValue\"}]";
JSONException jeOne = assertThrows(JSONException.class, JSONException jeOne = assertThrows(JSONException.class,
() -> new JSONArray(testCaseOne, jsonParserConfiguration)); () -> new JSONArray(testCaseOne, jsonParserConfiguration));
JSONException jeTwo = assertThrows(JSONException.class, JSONException jeTwo = assertThrows(JSONException.class,
() -> new JSONArray(testCaseTwo, jsonParserConfiguration)); () -> new JSONArray(testCaseTwo, jsonParserConfiguration));
JSONException jeThree = assertThrows(JSONException.class,
() -> new JSONArray(testCaseThree, jsonParserConfiguration));
JSONException jeFour = assertThrows(JSONException.class,
() -> new JSONArray(testCaseFour, jsonParserConfiguration));
assertEquals( assertEquals(
"Field contains unbalanced quotes. Starts with \" but ends with single quote. at 6 [character 7 line 1]", "Field contains unbalanced quotes. Starts with \" but ends with single quote. at 6 [character 7 line 1]",
jeOne.getMessage()); jeOne.getMessage());
assertEquals( assertEquals(
"Field contains unbalanced quotes. Starts with ' but ends with double quote. at 6 [character 7 line 1]", "Single quote wrap not allowed in strict mode at 2 [character 3 line 1]",
jeTwo.getMessage()); jeTwo.getMessage());
assertEquals(
"Single quote wrap not allowed in strict mode at 2 [character 3 line 1]",
jeThree.getMessage());
assertEquals(
"Single quote wrap not allowed in strict mode at 3 [character 4 line 1]",
jeFour.getMessage());
} }
@Test @Test