Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore strict mode text parsing #921

Merged
merged 6 commits into from
Dec 21, 2024
Merged

Conversation

stleary
Copy link
Owner

@stleary stleary commented Dec 15, 2024

What problem does this code solve?

The default JSON-Java library is quite permissive in allowing invalid JSON docs to be parsed successfully. The "strict mode" feature is for users who need the library to enforce valid JSON text.

A few months ago, strict mode was added but it was not fully completed. As a result, the code was backed out temporarily and is now restored, with some updates to complete the implementation.

Strict mode is only available when parsing JSON from a text document.
To enable strict mode, use code like the following:

        JSONParserConfiguration jsonParserConfiguration = 
                new JSONParserConfiguration()
                .withStrictMode(true);
        JSONObject jsonObject = new JSONObject(myString, jsonParserConfiguration));

What exactly is excluded in strict mode:

  • Unparsed text at the end of JSON documents is not allowed
  • JSONArrays must not have multiple consecutive commas
  • JSONArray and JSONObject content must not terminate with a comma
  • JSONObjects must not use a semicolon to separate a key from a value
  • JSONObject keys and string values in JSONArrays or JSONObjects must be surrounded by double quotes

To test for regressions, update the JSONParserConfiguration class to have strict mode enabled by default. Then build the project and run the unit tests, which should all complete successfully.

Does the code still compile with Java6?
Yes

Risks
Low

Changes to the API?
The original strict mode API has been restored.

Will this require a new release?
No

Should the documentation be updated?
No

Does it break the unit tests?
No. Some tests that used invalid JSON text have been fixed. A few tests that specifically use invalid text are skipped when strict mode is defaulted. (see regression testing discussion above). New unit tests were added.

Was any code refactored in this commit?
The internal implementation of strict mode has been updated.

Review status
APPROVED - by myself

Starting 3-day comment window

@stleary stleary changed the title Restore jsonparserconfiguration Restore strict mode text parsing Dec 15, 2024
@stleary stleary merged commit ac40a6f into master Dec 21, 2024
12 checks passed
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.

1 participant