Skip to content

Commit

Permalink
Merge pull request #937 from michael-ameri/remove-references
Browse files Browse the repository at this point in the history
Update JSONParserConfiguration usage in JSONTokener, JSONArray, and JSONObject
  • Loading branch information
stleary authored Jan 15, 2025
2 parents 391c869 + ca1c683 commit 8c427d9
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 54 deletions.
35 changes: 10 additions & 25 deletions src/main/java/org/json/JSONArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,6 @@ public class JSONArray implements Iterable<Object> {
*/
private final ArrayList<Object> myArrayList;

// strict mode checks after constructor require access to this object
private JSONTokener jsonTokener;

// strict mode checks after constructor require access to this object
private JSONParserConfiguration jsonParserConfiguration;

/**
* Construct an empty JSONArray.
*/
Expand Down Expand Up @@ -102,14 +96,7 @@ public JSONArray(JSONTokener x) throws JSONException {
public JSONArray(JSONTokener x, JSONParserConfiguration jsonParserConfiguration) throws JSONException {
this();

if (this.jsonParserConfiguration == null) {
this.jsonParserConfiguration = jsonParserConfiguration;
}
if (this.jsonTokener == null) {
this.jsonTokener = x;
this.jsonTokener.setJsonParserConfiguration(this.jsonParserConfiguration);
}

boolean isInitial = x.getPrevious() == 0;
if (x.nextClean() != '[') {
throw x.syntaxError("A JSONArray text must start with '['");
}
Expand Down Expand Up @@ -156,11 +143,19 @@ public JSONArray(JSONTokener x, JSONParserConfiguration jsonParserConfiguration)
x.back();
break;
case ']':
if (isInitial && jsonParserConfiguration.isStrictMode() &&
x.nextClean() != 0) {
throw x.syntaxError("Strict mode error: Unparsed characters found at end of input text");
}
return;
default:
throw x.syntaxError("Expected a ',' or ']'");
}
}
} else {
if (isInitial && jsonParserConfiguration.isStrictMode() && x.nextClean() != 0) {
throw x.syntaxError("Strict mode error: Unparsed characters found at end of input text");
}
}
}

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

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

/**
Expand Down
37 changes: 10 additions & 27 deletions src/main/java/org/json/JSONObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,6 @@ public Class<? extends Map> getMapType() {
*/
public static final Object NULL = new Null();

// strict mode checks after constructor require access to this object
private JSONTokener jsonTokener;

// strict mode checks after constructor require access to this object
private JSONParserConfiguration jsonParserConfiguration;

/**
* Construct an empty JSONObject.
*/
Expand Down Expand Up @@ -217,18 +211,11 @@ public JSONObject(JSONTokener x) throws JSONException {
*/
public JSONObject(JSONTokener x, JSONParserConfiguration jsonParserConfiguration) throws JSONException {
this();

if (this.jsonParserConfiguration == null) {
this.jsonParserConfiguration = jsonParserConfiguration;
}
if (this.jsonTokener == null) {
this.jsonTokener = x;
this.jsonTokener.setJsonParserConfiguration(this.jsonParserConfiguration);
}

char c;
String key;

boolean isInitial = x.getPrevious() == 0;

if (x.nextClean() != '{') {
throw x.syntaxError("A JSONObject text must begin with '{'");
}
Expand All @@ -238,6 +225,9 @@ public JSONObject(JSONTokener x, JSONParserConfiguration jsonParserConfiguration
case 0:
throw x.syntaxError("A JSONObject text must end with '}'");
case '}':
if (isInitial && jsonParserConfiguration.isStrictMode() && x.nextClean() != 0) {
throw x.syntaxError("Strict mode error: Unparsed characters found at end of input text");
}
return;
default:
key = x.nextSimpleValue(c).toString();
Expand Down Expand Up @@ -288,6 +278,9 @@ public JSONObject(JSONTokener x, JSONParserConfiguration jsonParserConfiguration
x.back();
break;
case '}':
if (isInitial && jsonParserConfiguration.isStrictMode() && x.nextClean() != 0) {
throw x.syntaxError("Strict mode error: Unparsed characters found at end of input text");
}
return;
default:
throw x.syntaxError("Expected a ',' or '}'");
Expand Down Expand Up @@ -456,11 +449,6 @@ public JSONObject(Object object, String ... names) {
*/
public JSONObject(String source) throws JSONException {
this(source, new JSONParserConfiguration());
// Strict mode does not allow trailing chars
if (this.jsonParserConfiguration.isStrictMode() &&
this.jsonTokener.nextClean() != 0) {
throw new JSONException("Strict mode error: Unparsed characters found at end of input text");
}
}

/**
Expand All @@ -478,12 +466,7 @@ public JSONObject(String source) throws JSONException {
* duplicated key.
*/
public JSONObject(String source, JSONParserConfiguration jsonParserConfiguration) throws JSONException {
this(new JSONTokener(source), jsonParserConfiguration);
// Strict mode does not allow trailing chars
if (this.jsonParserConfiguration.isStrictMode() &&
this.jsonTokener.nextClean() != 0) {
throw new JSONException("Strict mode error: Unparsed characters found at end of input text");
}
this(new JSONTokener(source, jsonParserConfiguration), jsonParserConfiguration);
}

/**
Expand Down Expand Up @@ -1280,7 +1263,7 @@ public BigDecimal optBigDecimal(String key, BigDecimal defaultValue) {
static BigDecimal objectToBigDecimal(Object val, BigDecimal defaultValue) {
return objectToBigDecimal(val, defaultValue, true);
}

/**
* @param val value to convert
* @param defaultValue default value to return is the conversion doesn't work or is null.
Expand Down
20 changes: 18 additions & 2 deletions src/main/java/org/json/JSONTokener.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ public class JSONTokener {
*
* @param reader A reader.
*/
public JSONTokener(Reader reader) {
public JSONTokener(Reader reader, JSONParserConfiguration jsonParserConfiguration) {
this.jsonParserConfiguration = jsonParserConfiguration;
this.reader = reader.markSupported()
? reader
: new BufferedReader(reader);
Expand All @@ -53,13 +54,24 @@ public JSONTokener(Reader reader) {
this.line = 1;
}

public JSONTokener(Reader reader) {
this(reader, new JSONParserConfiguration());
}

/**
* Construct a JSONTokener from an InputStream. The caller must close the input stream.
* @param inputStream The source.
*/
public JSONTokener(InputStream inputStream) {
this(new InputStreamReader(inputStream, Charset.forName("UTF-8")));
this(inputStream, new JSONParserConfiguration());
}

/**
* Construct a JSONTokener from an InputStream. The caller must close the input stream.
* @param inputStream The source.
*/
public JSONTokener(InputStream inputStream, JSONParserConfiguration jsonParserConfiguration) {
this(new InputStreamReader(inputStream, Charset.forName("UTF-8")),jsonParserConfiguration);
}


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

public JSONTokener(String s, JSONParserConfiguration jsonParserConfiguration) {
this(new StringReader(s), jsonParserConfiguration);
}

/**
* Getter
* @return jsonParserConfiguration
Expand Down

0 comments on commit 8c427d9

Please sign in to comment.