Skip to content

Commit 1f308db

Browse files
Sean LearySean Leary
Sean Leary
authored and
Sean Leary
committed
restore-jsonparserconfiguration: Restore methods to be used for strict mode
1 parent 2ee5bf1 commit 1f308db

File tree

2 files changed

+80
-17
lines changed

2 files changed

+80
-17
lines changed

src/main/java/org/json/JSONArray.java

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,22 @@ public JSONArray() {
8383
* If there is a syntax error.
8484
*/
8585
public JSONArray(JSONTokener x) throws JSONException {
86+
this(x, new JSONParserConfiguration());
87+
}
88+
89+
/**
90+
* Constructs a JSONArray from a JSONTokener and a JSONParserConfiguration.
91+
*
92+
* @param x A JSONTokener instance from which the JSONArray is constructed.
93+
* @param jsonParserConfiguration A JSONParserConfiguration instance that controls the behavior of the parser.
94+
* @throws JSONException If a syntax error occurs during the construction of the JSONArray.
95+
*/
96+
public JSONArray(JSONTokener x, JSONParserConfiguration jsonParserConfiguration) throws JSONException {
8697
this();
8798
if (x.nextClean() != '[') {
8899
throw x.syntaxError("A JSONArray text must start with '['");
89100
}
90-
101+
91102
char nextChar = x.nextClean();
92103
if (nextChar == 0) {
93104
// array is unclosed. No ']' found, instead EOF
@@ -104,27 +115,28 @@ public JSONArray(JSONTokener x) throws JSONException {
104115
this.myArrayList.add(x.nextValue());
105116
}
106117
switch (x.nextClean()) {
107-
case 0:
108-
// array is unclosed. No ']' found, instead EOF
109-
throw x.syntaxError("Expected a ',' or ']'");
110-
case ',':
111-
nextChar = x.nextClean();
112-
if (nextChar == 0) {
118+
case 0:
113119
// array is unclosed. No ']' found, instead EOF
114120
throw x.syntaxError("Expected a ',' or ']'");
115-
}
116-
if (nextChar == ']') {
121+
case ',':
122+
nextChar = x.nextClean();
123+
if (nextChar == 0) {
124+
// array is unclosed. No ']' found, instead EOF
125+
throw x.syntaxError("Expected a ',' or ']'");
126+
}
127+
if (nextChar == ']') {
128+
return;
129+
}
130+
x.back();
131+
break;
132+
case ']':
117133
return;
118-
}
119-
x.back();
120-
break;
121-
case ']':
122-
return;
123-
default:
124-
throw x.syntaxError("Expected a ',' or ']'");
134+
default:
135+
throw x.syntaxError("Expected a ',' or ']'");
125136
}
126137
}
127138
}
139+
128140
}
129141

130142
/**
@@ -138,7 +150,22 @@ public JSONArray(JSONTokener x) throws JSONException {
138150
* If there is a syntax error.
139151
*/
140152
public JSONArray(String source) throws JSONException {
141-
this(new JSONTokener(source));
153+
this(source, new JSONParserConfiguration());
154+
}
155+
156+
/**
157+
* Construct a JSONArray from a source JSON text.
158+
*
159+
* @param source
160+
* A string that begins with <code>[</code>&nbsp;<small>(left
161+
* bracket)</small> and ends with <code>]</code>
162+
* &nbsp;<small>(right bracket)</small>.
163+
* @param jsonParserConfiguration the parser config object
164+
* @throws JSONException
165+
* If there is a syntax error.
166+
*/
167+
public JSONArray(String source, JSONParserConfiguration jsonParserConfiguration) throws JSONException {
168+
this(new JSONTokener(source), jsonParserConfiguration);
142169
}
143170

144171
/**

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ public JSONParserConfiguration() {
1717
this.overwriteDuplicateKey = false;
1818
}
1919

20+
/**
21+
* This flag, when set to true, instructs the parser to enforce strict mode when parsing JSON text.
22+
* Garbage chars at the end of the doc, unquoted string, and single-quoted strings are all disallowed.
23+
*/
24+
private boolean strictMode;
25+
2026
@Override
2127
protected JSONParserConfiguration clone() {
2228
JSONParserConfiguration clone = new JSONParserConfiguration();
@@ -58,6 +64,23 @@ public JSONParserConfiguration withOverwriteDuplicateKey(final boolean overwrite
5864
return clone;
5965
}
6066

67+
/**
68+
* Sets the strict mode configuration for the JSON parser.
69+
* <p>
70+
* When strict mode is enabled, the parser will throw a JSONException if it encounters an invalid character
71+
* immediately following the final ']' character in the input. This is useful for ensuring strict adherence to the
72+
* JSON syntax, as any characters after the final closing bracket of a JSON array are considered invalid.
73+
*
74+
* @param mode a boolean value indicating whether strict mode should be enabled or not
75+
* @return a new JSONParserConfiguration instance with the updated strict mode setting
76+
*/
77+
public JSONParserConfiguration withStrictMode(final boolean mode) {
78+
JSONParserConfiguration clone = this.clone();
79+
clone.strictMode = mode;
80+
81+
return clone;
82+
}
83+
6184
/**
6285
* The parser's behavior when meeting duplicate keys, controls whether the parser should
6386
* overwrite duplicate keys or not.
@@ -67,4 +90,17 @@ public JSONParserConfiguration withOverwriteDuplicateKey(final boolean overwrite
6790
public boolean isOverwriteDuplicateKey() {
6891
return this.overwriteDuplicateKey;
6992
}
93+
94+
/**
95+
* Retrieves the current strict mode setting of the JSON parser.
96+
* <p>
97+
* Strict mode, when enabled, instructs the parser to throw a JSONException if it encounters an invalid character
98+
* immediately following the final ']' character in the input. This ensures strict adherence to the JSON syntax, as
99+
* any characters after the final closing bracket of a JSON array are considered invalid.
100+
*
101+
* @return the current strict mode setting. True if strict mode is enabled, false otherwise.
102+
*/
103+
public boolean isStrictMode() {
104+
return this.strictMode;
105+
}
70106
}

0 commit comments

Comments
 (0)