Skip to content

Conversation

@eleumik
Copy link
Contributor

@eleumik eleumik commented Sep 10, 2025

fix array content starting with ',' in strict mode

fix array content starting with ',' in strict mode
@sonarqubecloud
Copy link

if (nextChar == 0) {
// array is unclosed. No ']' found, instead EOF
throw x.syntaxError("Expected a ',' or ']'");
} else if (nextChar==',' && jsonParserConfiguration.isStrictMode()) {
Copy link
Owner

@stleary stleary Sep 13, 2025

Choose a reason for hiding this comment

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

Please add at least one test case to JSONArrayTest.java to provide coverage for the new code
For example, this tests both jsonobject and jsonarray, with and without strict mode. It also handles the 'TestWithStrictMode' gradle task where strict mode is always set.

    @Test
    public void foo() {
        String s = "{\"a\":[ ,4,null,8]}";
        JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration();
        if (jsonParserConfiguration.isStrictMode()) {
            // expect an error during strict mode testing
            assertThrows(JSONException.class, () -> { new JSONObject(s); });
        } else {
            new JSONObject(s);
        }
        assertThrows(JSONException.class, () -> { new JSONObject(s, new JSONParserConfiguration().withStrictMode(true)); });
        String s2 = "[,4,null,8]";
        if (jsonParserConfiguration.isStrictMode()) {
            // expect an error during strict mode testing
            assertThrows(JSONException.class, () -> { new JSONArray(s2); });
        } else {
            new JSONArray(s2);
        }
        assertThrows(JSONException.class, () -> { new JSONArray(s2, new JSONParserConfiguration().withStrictMode(true)); });
    }

@stleary
Copy link
Owner

stleary commented Sep 13, 2025

What problem does this code solve?
Strict mode was not checking for leadihng ',' in JSONArray

Does the code still compile with Java6?
Yes

Risks
Low

Changes to the API?
No

Will this require a new release?
No

Should the documentation be updated?
No

Does it break the unit tests?
No, but new unit tests are needed. Code review action item left

Was any code refactored in this commit?
No

Review status
APPROVED

Starting 3-day comment window

@stleary
Copy link
Owner

stleary commented Sep 13, 2025

@eleumik PR is approved, but please add unit test coverage if you get a chance. Otherwise, I will add it after the merge.

Copy link
Owner

@stleary stleary left a comment

Choose a reason for hiding this comment

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

Open comments will be addressed in a future commit

@stleary stleary merged commit b258ea3 into stleary:master Sep 19, 2025
8 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.

2 participants