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

JSONArray converting incorrect input string to array #871

Closed
ssharan27 opened this issue Feb 28, 2024 · 7 comments
Closed

JSONArray converting incorrect input string to array #871

ssharan27 opened this issue Feb 28, 2024 · 7 comments

Comments

@ssharan27
Copy link

Hi, I recently came across a strange issue in one of my java applications. I have an input string that looks like [1,2];[3,4]. This is clearly not a JSON array and should result in an exception. However when I do the following:

String inputString = "[1,2];[3,4]";
List<Object> resultArray = new JSONArray(inputString).toList();

This gives me a List object with value [1,2].

I checked this in the latest release as well by adding the following test to the JSONArrayTest class and it still failed:

    @Test(expected = JSONException.class)
    public void testInvalidJSONArray() {
        List<Object> x = new JSONArray("[1,2];[3,4]").toList();
        System.out.println(x);
    }

I am not sure but it seems like the array object creation is stopping at the first ] character even when there are characters left to read. Can you please take a look at this?

@stleary
Copy link
Owner

stleary commented Feb 28, 2024

@ssharan27 Thanks for raising this issue. It turns out that since the beginning, the parser has been forgiving of unparseable content at the end of JSON docs. In this case, :[3,4] is considered invalid text, and is therefore ignored. Since the first part - [1,2] is a valid JSONArray, that is the only part that is parsed.

WIth this in mind, you can try some different variants and see if the results are consistent.

@stleary stleary closed this as completed Feb 28, 2024
@ssharan27
Copy link
Author

Gotcha! Thanks @stleary ! This means I cannot rely on this constructor call alone to automatically detect a valid JSON (with strictness, that is) and then convert into array. The input needs to be validated before parsing. Is there any recommended way to do this?

Also, would you consider adding examples as well where invalid JSON is parsed successfully in the docs or the readme itself? The docs do mention the forgiving nature but I think an example there would be helpful. I say this because a lot of examples/posts/SO answers I saw regarding this seem to ignore this behavior.

@stleary
Copy link
Owner

stleary commented Mar 1, 2024

No objections if someone wants to work on this.

  1. Update the readme (or add a new markup doc linked from the readme), explaining how forgiving mode works, with examples.

  2. Implement strict mode using JSONParserConfiguration to enforce not allowing invalid chars at end of file. If possible, disallow implied quotes as well (this is when a key or string value is not surrounded by quotes).

rikkarth added a commit to rikkarth/JSON-java-fork that referenced this issue Mar 15, 2024
rikkarth added a commit to rikkarth/JSON-java-fork that referenced this issue Mar 15, 2024
…tion

test(stleary#871-strictMode): initial test implementation
rikkarth added a commit to rikkarth/JSON-java-fork that referenced this issue Mar 15, 2024
rikkarth added a commit to rikkarth/JSON-java-fork that referenced this issue Mar 15, 2024
rikkarth added a commit to rikkarth/JSON-java-fork that referenced this issue Mar 15, 2024
@rikkarth
Copy link
Contributor

Hi,

I'm currently working on this and the only thing that is left to do is the quotes part and adding a few more unit tests.

I will provide feedback as soon as I have more.

@rikkarth
Copy link
Contributor

Tests for non compliant JSON arrays after implementation.
image

rikkarth added a commit to rikkarth/JSON-java-fork that referenced this issue Mar 15, 2024
rikkarth added a commit to rikkarth/JSON-java-fork that referenced this issue Mar 15, 2024
rikkarth added a commit to rikkarth/JSON-java-fork that referenced this issue Mar 15, 2024
@rikkarth
Copy link
Contributor

PR - #877

rikkarth added a commit to rikkarth/JSON-java-fork that referenced this issue Mar 16, 2024
rikkarth added a commit to rikkarth/JSON-java-fork that referenced this issue Mar 16, 2024
rikkarth added a commit to rikkarth/JSON-java-fork that referenced this issue Mar 17, 2024
rikkarth added a commit to rikkarth/JSON-java-fork that referenced this issue Mar 22, 2024
rikkarth added a commit to rikkarth/JSON-java-fork that referenced this issue Mar 22, 2024
rikkarth added a commit to rikkarth/JSON-java-fork that referenced this issue Mar 30, 2024
rikkarth added a commit to rikkarth/JSON-java-fork that referenced this issue Mar 30, 2024
rikkarth added a commit to rikkarth/JSON-java-fork that referenced this issue Mar 30, 2024
rikkarth added a commit to rikkarth/JSON-java-fork that referenced this issue Mar 30, 2024
rikkarth added a commit to rikkarth/JSON-java-fork that referenced this issue Mar 30, 2024
test(stleary#871-strictMode): adjusted related tests, add more test cases for non-compliant quotes in strict mode
rikkarth added a commit to rikkarth/JSON-java-fork that referenced this issue Apr 7, 2024
chore: removed PII from json sample
chore: JSONParserConfiguration.java cleanup
chore: JSONTokener.java nextValue partial rollback
@stleary
Copy link
Owner

stleary commented Apr 7, 2024

Closing due to implementation completed.

@stleary stleary closed this as completed Apr 7, 2024
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

No branches or pull requests

3 participants