Skip to content

Commit

Permalink
feat(#871-strictMode): removed allowSingleQuotes
Browse files Browse the repository at this point in the history
test(#871-strictMode): adjusted related tests, add more test cases for non-compliant quotes in strict mode
  • Loading branch information
rikkarth committed Mar 30, 2024
1 parent c0918c2 commit 46534b5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 37 deletions.
34 changes: 2 additions & 32 deletions src/main/java/org/json/JSONParserConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@
public class JSONParserConfiguration extends ParserConfiguration {

/** Original Configuration of the JSON Parser. */
public static final JSONParserConfiguration ORIGINAL
= new JSONParserConfiguration();
public static final JSONParserConfiguration ORIGINAL = new JSONParserConfiguration();

/** Original configuration of the JSON Parser except that values are kept as strings. */
public static final JSONParserConfiguration KEEP_STRINGS
= new JSONParserConfiguration().withKeepStrings(true);
public static final JSONParserConfiguration KEEP_STRINGS = new JSONParserConfiguration().withKeepStrings(true);

/**
* Used to indicate whether to overwrite duplicate key or not.
Expand Down Expand Up @@ -97,24 +95,6 @@ public JSONParserConfiguration withStrictMode(final boolean mode) {
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
* overwrite duplicate keys or not.
Expand All @@ -138,14 +118,4 @@ public boolean isOverwriteDuplicateKey() {
public boolean isStrictMode() {
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;}
}
3 changes: 1 addition & 2 deletions src/main/java/org/json/JSONTokener.java
Original file line number Diff line number Diff line change
Expand Up @@ -472,9 +472,8 @@ private JSONArray getJsonArray() {

Object nextSimpleValue(char c, JSONParserConfiguration jsonParserConfiguration) {
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");
}

Expand Down
18 changes: 15 additions & 3 deletions src/test/java/org/json/junit/JSONParserConfigurationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,24 +118,36 @@ public void givenInvalidInputArray_testStrictModeFalse_shouldNotThrowAnyExceptio
}

@Test
public void givenUnbalancedQuotes_testStrictModeTrueAndAllowSingleQuotes_shouldThrowJsonExceptionWtihConcreteErrorDescription() {
public void givenNonCompliantQuotes_testStrictModeTrue_shouldThrowJsonExceptionWithConcreteErrorDescription() {
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
.withStrictMode(true).allowSingleQuotes(true);
.withStrictMode(true);

String testCaseOne = "[\"abc', \"test\"]";
String testCaseTwo = "['abc\", \"test\"]";
String testCaseThree = "['abc']";
String testCaseFour = "[{'testField': \"testValue\"}]";

JSONException jeOne = assertThrows(JSONException.class,
() -> new JSONArray(testCaseOne, jsonParserConfiguration));
JSONException jeTwo = assertThrows(JSONException.class,
() -> new JSONArray(testCaseTwo, jsonParserConfiguration));
JSONException jeThree = assertThrows(JSONException.class,
() -> new JSONArray(testCaseThree, jsonParserConfiguration));
JSONException jeFour = assertThrows(JSONException.class,
() -> new JSONArray(testCaseFour, jsonParserConfiguration));

assertEquals(
"Field contains unbalanced quotes. Starts with \" but ends with single quote. at 6 [character 7 line 1]",
jeOne.getMessage());
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());
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
Expand Down

0 comments on commit 46534b5

Please sign in to comment.