Skip to content

JSONObject/JSONArray ctors override JSONParserConfiguration for strict mode - #1077

Merged
stleary merged 2 commits into
stleary:masterfrom
Mahmoodifar:fix/parser-configuration-precedence
Sep 21, 2026
Merged

stleary merged 2 commits into
stleary:masterfrom
Mahmoodifar:fix/parser-configuration-precedence

Conversation

@Mahmoodifar

@Mahmoodifar Mahmoodifar commented Sep 18, 2026

Copy link
Copy Markdown
Contributor

When a JSONObject or JSONArray constructor receives both a JSONTokener and an explicit parser configuration, nested values still use the tokener's previous configuration. For example, strict parsing can accept single-quoted values.

Apply the supplied configuration throughout construction, including nested values, using a package-private setter. Restore the tokener's original configuration in a finally block on both success and failure, and document this behavior.

Fixes #945.

Regression coverage includes strict and lenient overrides, nested duplicate keys, sibling values, empty containers, malformed input, trailing content, and reuse of the tokener after construction.

Validation on Java 17:

  • mvn -B -ntp clean test
  • mvn -B -ntp test -Ptest-strict-mode javadoc:jar@attach-javadocs

Both suites: 804 tests, 6 skipped, no failures or errors. Javadocs built successfully. Five configuration-restoration tests failed before the follow-up fix.

Comment thread src/main/java/org/json/JSONArray.java Outdated
*/
JSONArray(JSONTokener x, JSONParserConfiguration jsonParserConfiguration, boolean isInitial) throws JSONException {
this();
x.setJsonParserConfiguration(jsonParserConfiguration);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please restore the original JSONParserConfiguration to JSONTokener before returning.

Comment thread src/main/java/org/json/JSONObject.java Outdated
*/
JSONObject(JSONTokener x, JSONParserConfiguration jsonParserConfiguration, boolean isInitial) throws JSONException {
this();
x.setJsonParserConfiguration(jsonParserConfiguration);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please restore the original JSONParserConfiguration to JSONTokener before returning.

Comment thread src/main/java/org/json/JSONArray.java Outdated
*/
JSONArray(JSONTokener x, JSONParserConfiguration jsonParserConfiguration, boolean isInitial) throws JSONException {
this();
x.setJsonParserConfiguration(jsonParserConfiguration);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JSONTokener.setParserConfiguration() is deprecated. Please add and use a new method that does the same thing, but make it package-private.

@stleary

stleary commented Sep 18, 2026

Copy link
Copy Markdown
Owner

@Mahmoodifar Thanks for the PR. Looks good, a couple of minor items to address.

@sonarqubecloud

Copy link
Copy Markdown

@stleary

stleary commented Sep 18, 2026

Copy link
Copy Markdown
Owner

What problem does this code solve?
Fixes issue where JSONObject/JSONArray ctor and JSONTokener have different parser config values for strict mode. Now the ctor takes precedence. The original JSONTokener strict mode setting is restored before returning.

Risks
Low

Changes to the Existing Behavior
Yes. This will change the existing behavior when parsing with a JSONTokener which does not have the same strict mode setting as the JSONObject or JSONArray. This is an acceptable corner-case behavior change.

Changes to the API
No

Will this require a new release?
No

Should the documentation be updated?
No, but a future readme update should document strict mode.

Unit Tests
New unit tests were added

Refactoring
No

Review status
APPROVED

Starting 3-day comment window

@stleary
stleary merged commit 4f822e0 into stleary:master Sep 21, 2026
9 checks passed
@stleary stleary changed the title Apply explicit parser configuration to nested values JSONObject/JSONArray ctors override JSONParserConfiguration for strict mode Sep 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Which JSONParserConfiguration to use in JSONObject/JSONArray constructors

2 participants