Skip to content

Fix strict mode failure when JSONTokener.next() and back() are called before parsing - #1074

Open
XIAYM-gh wants to merge 3 commits into
stleary:masterfrom
XIAYM-gh:master
Open

Fix strict mode failure when JSONTokener.next() and back() are called before parsing#1074
XIAYM-gh wants to merge 3 commits into
stleary:masterfrom
XIAYM-gh:master

Conversation

@XIAYM-gh

@XIAYM-gh XIAYM-gh commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

This PR fixes #944, where strict mode syntax check isn't working when next() and back() are called first.

ctors are modified, but the backward compatibility is kept.
Besides, I've added isAtStart() in JSONTokener, which shouldn't be a compatibility issue :)

All tests had passed. Thanks ahead for reviewing this!

@stleary

stleary commented Sep 3, 2026

Copy link
Copy Markdown
Owner

@XIAYM-gh I will resolve the sonarqube issues separately, you can ignore them

Comment thread src/main/java/org/json/JSONArray.java Outdated
Comment thread src/main/java/org/json/JSONObject.java Outdated
Comment thread src/main/java/org/json/JSONTokener.java Outdated
Comment thread src/test/java/org/json/junit/JSONArrayTest.java
Comment thread src/test/java/org/json/junit/JSONObjectTest.java
@XIAYM-gh

XIAYM-gh commented Sep 3, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for noticing! I'll start working on this in a few hours. (it's 5:21 AM in my timezone right now, and probably I will sleep more)

@XIAYM-gh

XIAYM-gh commented Sep 4, 2026

Copy link
Copy Markdown
Contributor Author

The problems should be settled now. Please review the new changes.

Comment thread src/main/java/org/json/JSONTokener.java Outdated
Comment thread src/main/java/org/json/JSONArray.java Outdated
Comment thread src/main/java/org/json/JSONObject.java Outdated
@XIAYM-gh

XIAYM-gh commented Sep 9, 2026

Copy link
Copy Markdown
Contributor Author

Thanks! I'm currently occupied by military training as a freshman, so the free time I have is really few. But I will get on working this soon.

@stleary

stleary commented Sep 10, 2026

Copy link
Copy Markdown
Owner

@XIAYM-gh The requested changes look good, but I think you have included some unrelated changes related to JSONConverter that should be removed. Going forward, you might want to consider submitting your work from dev branches instead of the master branch.

@XIAYM-gh

Copy link
Copy Markdown
Contributor Author

Oh my - I was too busy and forgot to check my local code - I asked a LLM to create a reference implementation for me, and I had forgotten to switch to another branch. I will now clean them up.

@sonarqubecloud

Copy link
Copy Markdown

@XIAYM-gh

Copy link
Copy Markdown
Contributor Author

Okay, I've deleted the commit. I'll definitely check more carefully next time.

@stleary stleary changed the title Fixes corner case where strict mode isn't working when JSONTokener's next() and back() are called first Fix strict mode failure when JSONTokener.next() and back() are called before parsing Sep 10, 2026
@stleary

stleary commented Sep 10, 2026

Copy link
Copy Markdown
Owner

What problem does this code solve?
Strict mode may fail if JSONTokener.next(), back() are called before parsing a JSONArray or JSONObject

Risks
Low

Changes to the Existing Behavior
Strict mode handling will work correctly even if JSONTokener methods are called before parsing

Changes to the API
No

Will this require a new release?
No

Should the documentation be updated?
No

Unit Tests
New unit tests were added

Refactoring
Minimal comment refactoring

Review status
APPROVED

Starting 3-day comment window

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JSONObject and JSONArray initial flag for tokener may be flawed

2 participants