Skip to content

Commit ca1c683

Browse files
committed
remove field references to JSONTokener and JSONParserConfiguration in JSONArray
and JSONObject
1 parent 391c869 commit ca1c683

File tree

3 files changed

+38
-54
lines changed

3 files changed

+38
-54
lines changed

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

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,6 @@ public class JSONArray implements Iterable<Object> {
6767
*/
6868
private final ArrayList<Object> myArrayList;
6969

70-
// strict mode checks after constructor require access to this object
71-
private JSONTokener jsonTokener;
72-
73-
// strict mode checks after constructor require access to this object
74-
private JSONParserConfiguration jsonParserConfiguration;
75-
7670
/**
7771
* Construct an empty JSONArray.
7872
*/
@@ -102,14 +96,7 @@ public JSONArray(JSONTokener x) throws JSONException {
10296
public JSONArray(JSONTokener x, JSONParserConfiguration jsonParserConfiguration) throws JSONException {
10397
this();
10498

105-
if (this.jsonParserConfiguration == null) {
106-
this.jsonParserConfiguration = jsonParserConfiguration;
107-
}
108-
if (this.jsonTokener == null) {
109-
this.jsonTokener = x;
110-
this.jsonTokener.setJsonParserConfiguration(this.jsonParserConfiguration);
111-
}
112-
99+
boolean isInitial = x.getPrevious() == 0;
113100
if (x.nextClean() != '[') {
114101
throw x.syntaxError("A JSONArray text must start with '['");
115102
}
@@ -156,11 +143,19 @@ public JSONArray(JSONTokener x, JSONParserConfiguration jsonParserConfiguration)
156143
x.back();
157144
break;
158145
case ']':
146+
if (isInitial && jsonParserConfiguration.isStrictMode() &&
147+
x.nextClean() != 0) {
148+
throw x.syntaxError("Strict mode error: Unparsed characters found at end of input text");
149+
}
159150
return;
160151
default:
161152
throw x.syntaxError("Expected a ',' or ']'");
162153
}
163154
}
155+
} else {
156+
if (isInitial && jsonParserConfiguration.isStrictMode() && x.nextClean() != 0) {
157+
throw x.syntaxError("Strict mode error: Unparsed characters found at end of input text");
158+
}
164159
}
165160
}
166161

@@ -176,11 +171,6 @@ public JSONArray(JSONTokener x, JSONParserConfiguration jsonParserConfiguration)
176171
*/
177172
public JSONArray(String source) throws JSONException {
178173
this(source, new JSONParserConfiguration());
179-
// Strict mode does not allow trailing chars
180-
if (this.jsonParserConfiguration.isStrictMode() &&
181-
this.jsonTokener.nextClean() != 0) {
182-
throw jsonTokener.syntaxError("Strict mode error: Unparsed characters found at end of input text");
183-
}
184174
}
185175

186176
/**
@@ -195,12 +185,7 @@ public JSONArray(String source) throws JSONException {
195185
* If there is a syntax error.
196186
*/
197187
public JSONArray(String source, JSONParserConfiguration jsonParserConfiguration) throws JSONException {
198-
this(new JSONTokener(source), jsonParserConfiguration);
199-
// Strict mode does not allow trailing chars
200-
if (this.jsonParserConfiguration.isStrictMode() &&
201-
this.jsonTokener.nextClean() != 0) {
202-
throw jsonTokener.syntaxError("Strict mode error: Unparsed characters found at end of input text");
203-
}
188+
this(new JSONTokener(source, jsonParserConfiguration), jsonParserConfiguration);
204189
}
205190

206191
/**

src/main/java/org/json/JSONObject.java

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,6 @@ public Class<? extends Map> getMapType() {
152152
*/
153153
public static final Object NULL = new Null();
154154

155-
// strict mode checks after constructor require access to this object
156-
private JSONTokener jsonTokener;
157-
158-
// strict mode checks after constructor require access to this object
159-
private JSONParserConfiguration jsonParserConfiguration;
160-
161155
/**
162156
* Construct an empty JSONObject.
163157
*/
@@ -217,18 +211,11 @@ public JSONObject(JSONTokener x) throws JSONException {
217211
*/
218212
public JSONObject(JSONTokener x, JSONParserConfiguration jsonParserConfiguration) throws JSONException {
219213
this();
220-
221-
if (this.jsonParserConfiguration == null) {
222-
this.jsonParserConfiguration = jsonParserConfiguration;
223-
}
224-
if (this.jsonTokener == null) {
225-
this.jsonTokener = x;
226-
this.jsonTokener.setJsonParserConfiguration(this.jsonParserConfiguration);
227-
}
228-
229214
char c;
230215
String key;
231216

217+
boolean isInitial = x.getPrevious() == 0;
218+
232219
if (x.nextClean() != '{') {
233220
throw x.syntaxError("A JSONObject text must begin with '{'");
234221
}
@@ -238,6 +225,9 @@ public JSONObject(JSONTokener x, JSONParserConfiguration jsonParserConfiguration
238225
case 0:
239226
throw x.syntaxError("A JSONObject text must end with '}'");
240227
case '}':
228+
if (isInitial && jsonParserConfiguration.isStrictMode() && x.nextClean() != 0) {
229+
throw x.syntaxError("Strict mode error: Unparsed characters found at end of input text");
230+
}
241231
return;
242232
default:
243233
key = x.nextSimpleValue(c).toString();
@@ -288,6 +278,9 @@ public JSONObject(JSONTokener x, JSONParserConfiguration jsonParserConfiguration
288278
x.back();
289279
break;
290280
case '}':
281+
if (isInitial && jsonParserConfiguration.isStrictMode() && x.nextClean() != 0) {
282+
throw x.syntaxError("Strict mode error: Unparsed characters found at end of input text");
283+
}
291284
return;
292285
default:
293286
throw x.syntaxError("Expected a ',' or '}'");
@@ -456,11 +449,6 @@ public JSONObject(Object object, String ... names) {
456449
*/
457450
public JSONObject(String source) throws JSONException {
458451
this(source, new JSONParserConfiguration());
459-
// Strict mode does not allow trailing chars
460-
if (this.jsonParserConfiguration.isStrictMode() &&
461-
this.jsonTokener.nextClean() != 0) {
462-
throw new JSONException("Strict mode error: Unparsed characters found at end of input text");
463-
}
464452
}
465453

466454
/**
@@ -478,12 +466,7 @@ public JSONObject(String source) throws JSONException {
478466
* duplicated key.
479467
*/
480468
public JSONObject(String source, JSONParserConfiguration jsonParserConfiguration) throws JSONException {
481-
this(new JSONTokener(source), jsonParserConfiguration);
482-
// Strict mode does not allow trailing chars
483-
if (this.jsonParserConfiguration.isStrictMode() &&
484-
this.jsonTokener.nextClean() != 0) {
485-
throw new JSONException("Strict mode error: Unparsed characters found at end of input text");
486-
}
469+
this(new JSONTokener(source, jsonParserConfiguration), jsonParserConfiguration);
487470
}
488471

489472
/**
@@ -1280,7 +1263,7 @@ public BigDecimal optBigDecimal(String key, BigDecimal defaultValue) {
12801263
static BigDecimal objectToBigDecimal(Object val, BigDecimal defaultValue) {
12811264
return objectToBigDecimal(val, defaultValue, true);
12821265
}
1283-
1266+
12841267
/**
12851268
* @param val value to convert
12861269
* @param defaultValue default value to return is the conversion doesn't work or is null.

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ public class JSONTokener {
4040
*
4141
* @param reader A reader.
4242
*/
43-
public JSONTokener(Reader reader) {
43+
public JSONTokener(Reader reader, JSONParserConfiguration jsonParserConfiguration) {
44+
this.jsonParserConfiguration = jsonParserConfiguration;
4445
this.reader = reader.markSupported()
4546
? reader
4647
: new BufferedReader(reader);
@@ -53,13 +54,24 @@ public JSONTokener(Reader reader) {
5354
this.line = 1;
5455
}
5556

57+
public JSONTokener(Reader reader) {
58+
this(reader, new JSONParserConfiguration());
59+
}
5660

5761
/**
5862
* Construct a JSONTokener from an InputStream. The caller must close the input stream.
5963
* @param inputStream The source.
6064
*/
6165
public JSONTokener(InputStream inputStream) {
62-
this(new InputStreamReader(inputStream, Charset.forName("UTF-8")));
66+
this(inputStream, new JSONParserConfiguration());
67+
}
68+
69+
/**
70+
* Construct a JSONTokener from an InputStream. The caller must close the input stream.
71+
* @param inputStream The source.
72+
*/
73+
public JSONTokener(InputStream inputStream, JSONParserConfiguration jsonParserConfiguration) {
74+
this(new InputStreamReader(inputStream, Charset.forName("UTF-8")),jsonParserConfiguration);
6375
}
6476

6577

@@ -72,6 +84,10 @@ public JSONTokener(String s) {
7284
this(new StringReader(s));
7385
}
7486

87+
public JSONTokener(String s, JSONParserConfiguration jsonParserConfiguration) {
88+
this(new StringReader(s), jsonParserConfiguration);
89+
}
90+
7591
/**
7692
* Getter
7793
* @return jsonParserConfiguration

0 commit comments

Comments
 (0)