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

Iterating over an array inside other JSON can run off the end of the array and through the rest of the JSON #18

Open
tomwhoiscontrary opened this issue May 5, 2022 · 0 comments

Comments

@tomwhoiscontrary
Copy link

tomwhoiscontrary commented May 5, 2022

Say you have some JSON with an array somewhere inside, like this:

{"a": [1], "b": "the end"}

If you parse to the start of the array, then call parser.getArrayStream().iterator(), you get an iterator, which i believe should iterate over the contents of the array, and then stop. And indeed, you do. But if you then call hasNext() on the iterator again, it surprisingly returns true, and if you call next(), it returns a JsonValue containing the string "b" - this is the next object key after the end of the array!

Here's a JUnit 5 test:

        try (JsonParser parser = Json.createParser(new StringReader("{\"a\": [1], \"b\": \"the end\"}"))) {
            assertThat(parser.next(), equalTo(JsonParser.Event.START_OBJECT));

            assertThat(parser.next(), equalTo(JsonParser.Event.KEY_NAME));
            assertThat(parser.getString(), equalTo("a"));

            assertThat(parser.next(), equalTo(JsonParser.Event.START_ARRAY));
            Iterator<JsonValue> iterator = parser.getArrayStream().iterator();
            assertThat(iterator.hasNext(), equalTo(true));
            assertThat(iterator.next(), equalTo(json.createValue(1)));
            assertThat(iterator.hasNext(), equalTo(false));

            // this fails because the iterator starts iterating over the outer object!
            assertThat(iterator.hasNext(), equalTo(false));

            assertThat(parser.next(), equalTo(JsonParser.Event.KEY_NAME));
            assertThat(parser.getString(), equalTo("b"));

            assertThat(parser.next(), equalTo(JsonParser.Event.VALUE_STRING));
            assertThat(parser.getString(), equalTo("the end"));

            assertThat(parser.next(), equalTo(JsonParser.Event.END_OBJECT));
        }

This also happens when the array is nested inside another array. There, the iterator iterates over the other elements in the outer array.

Using Joy 2.1.0 with OpenJDK 17+35.

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

1 participant