Skip to content

Commit 46534b5

Browse files
author
rikkarth
committed
feat(stleary#871-strictMode): removed allowSingleQuotes
test(stleary#871-strictMode): adjusted related tests, add more test cases for non-compliant quotes in strict mode
1 parent c0918c2 commit 46534b5

File tree

3 files changed

+18
-37
lines changed

3 files changed

+18
-37
lines changed

src/main/java/org/json/JSONParserConfiguration.java

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@
66
public class JSONParserConfiguration extends ParserConfiguration {
77

88
/** Original Configuration of the JSON Parser. */
9-
public static final JSONParserConfiguration ORIGINAL
10-
= new JSONParserConfiguration();
9+
public static final JSONParserConfiguration ORIGINAL = new JSONParserConfiguration();
1110

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

1614
/**
1715
* Used to indicate whether to overwrite duplicate key or not.
@@ -97,24 +95,6 @@ public JSONParserConfiguration withStrictMode(final boolean mode) {
9795
return clone;
9896
}
9997

100-
/**
101-
* Allows single quotes mode configuration for JSON parser when strictMode is on.
102-
* <p>
103-
* If this option is set to true when strict Mode is enabled, the parser will allow single quoted fields.
104-
* <p>
105-
* This option is false by default.
106-
*
107-
* @param mode a boolean value indicating whether single quotes should be allowed or not
108-
* @return a new JSONParserConfiguration instance with the updated strict mode setting
109-
*/
110-
public JSONParserConfiguration allowSingleQuotes(final boolean mode) {
111-
JSONParserConfiguration clone = this.clone();
112-
clone.strictMode = this.strictMode;
113-
clone.allowSingleQuotes = mode;
114-
115-
return clone;
116-
}
117-
11898
/**
11999
* The parser's behavior when meeting duplicate keys, controls whether the parser should
120100
* overwrite duplicate keys or not.
@@ -138,14 +118,4 @@ public boolean isOverwriteDuplicateKey() {
138118
public boolean isStrictMode() {
139119
return this.strictMode;
140120
}
141-
142-
/**
143-
* Retrieves the allow single quotes option.
144-
* <p>
145-
* Allow Single Quotes, when enabled during strict mode, instructs the parser to allow single quoted JSON fields.
146-
* The parser will not throw a JSONException if compliant single quoted fields are found in the JSON structure.
147-
*
148-
* @return the current allow single quotes setting.
149-
*/
150-
public boolean isAllowSingleQuotes() {return this.allowSingleQuotes;}
151121
}

src/main/java/org/json/JSONTokener.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -472,9 +472,8 @@ private JSONArray getJsonArray() {
472472

473473
Object nextSimpleValue(char c, JSONParserConfiguration jsonParserConfiguration) {
474474
boolean strictMode = jsonParserConfiguration.isStrictMode();
475-
boolean allowSingleQuotes = jsonParserConfiguration.isAllowSingleQuotes();
476475

477-
if(strictMode && !allowSingleQuotes && c == '\''){
476+
if(strictMode && c == '\''){
478477
throw this.syntaxError("Single quote wrap not allowed in strict mode");
479478
}
480479

src/test/java/org/json/junit/JSONParserConfigurationTest.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,24 +118,36 @@ public void givenInvalidInputArray_testStrictModeFalse_shouldNotThrowAnyExceptio
118118
}
119119

120120
@Test
121-
public void givenUnbalancedQuotes_testStrictModeTrueAndAllowSingleQuotes_shouldThrowJsonExceptionWtihConcreteErrorDescription() {
121+
public void givenNonCompliantQuotes_testStrictModeTrue_shouldThrowJsonExceptionWithConcreteErrorDescription() {
122122
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
123-
.withStrictMode(true).allowSingleQuotes(true);
123+
.withStrictMode(true);
124124

125125
String testCaseOne = "[\"abc', \"test\"]";
126126
String testCaseTwo = "['abc\", \"test\"]";
127+
String testCaseThree = "['abc']";
128+
String testCaseFour = "[{'testField': \"testValue\"}]";
127129

128130
JSONException jeOne = assertThrows(JSONException.class,
129131
() -> new JSONArray(testCaseOne, jsonParserConfiguration));
130132
JSONException jeTwo = assertThrows(JSONException.class,
131133
() -> new JSONArray(testCaseTwo, jsonParserConfiguration));
134+
JSONException jeThree = assertThrows(JSONException.class,
135+
() -> new JSONArray(testCaseThree, jsonParserConfiguration));
136+
JSONException jeFour = assertThrows(JSONException.class,
137+
() -> new JSONArray(testCaseFour, jsonParserConfiguration));
132138

133139
assertEquals(
134140
"Field contains unbalanced quotes. Starts with \" but ends with single quote. at 6 [character 7 line 1]",
135141
jeOne.getMessage());
136142
assertEquals(
137-
"Field contains unbalanced quotes. Starts with ' but ends with double quote. at 6 [character 7 line 1]",
143+
"Single quote wrap not allowed in strict mode at 2 [character 3 line 1]",
138144
jeTwo.getMessage());
145+
assertEquals(
146+
"Single quote wrap not allowed in strict mode at 2 [character 3 line 1]",
147+
jeThree.getMessage());
148+
assertEquals(
149+
"Single quote wrap not allowed in strict mode at 3 [character 4 line 1]",
150+
jeFour.getMessage());
139151
}
140152

141153
@Test

0 commit comments

Comments
 (0)