You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
JSONArray converting incorrect input string to array #871
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?
@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.
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.
Update the readme (or add a new markup doc linked from the readme), explaining how forgiving mode works, with examples.
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).
Activity
stleary commentedon 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.
ssharan27 commentedon Feb 29, 2024
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 commentedon Mar 1, 2024
No objections if someone wants to work on this.
Update the readme (or add a new markup doc linked from the readme), explaining how forgiving mode works, with examples.
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 commentedon Mar 15, 2024
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 commentedon Mar 15, 2024
Tests for non compliant JSON arrays after implementation.

rikkarth commentedon Mar 15, 2024
PR - #877
stleary commentedon Apr 7, 2024
Closing due to implementation completed.