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

JsonReader consumes/skips-over trailing content in input stream #18

Open
jon-hanson opened this issue Apr 25, 2019 · 4 comments
Open

JsonReader consumes/skips-over trailing content in input stream #18

jon-hanson opened this issue Apr 25, 2019 · 4 comments

Comments

@jon-hanson
Copy link

The JsonReader seems to be greedy, in that it consumes any content after the first JSON value it finds in the input stream. E.g., in the following example, the trailing " 0" content is skipped over.

    public static void main(String[] args) throws IOException {
        StringReader sr = new StringReader("[] 0"); // Note, string has trailing content.
        JsonReader jr = Json.createReader(sr);
        JsonStructure js = jr.read();
        if (sr.read() != -1) {
            throw null;
        }
        System.out.println(js);
    }

Is this intentional? It would seem more logical to either not consume the trailing content, or to throw a parsing exception.

@lukasj lukasj transferred this issue from jakartaee/jsonp-api Jun 8, 2021
@niks
Copy link

niks commented Feb 20, 2023

Its the JsonTokenizer that is greedily filling its buffer. This makes it impossible to detect additional data that should not be there.

@jon-hanson
Copy link
Author

Which is a bug, do you agree?

@niks
Copy link

niks commented Feb 21, 2023

I'm not sure if this has to be classified as bug. It's rather undefined behavior as the specification does not make any guarantees. It actually doesn't say anything about how many bytes/characters are read.

But this behavior was definitely unexpected by me. And it prevents several use cases where someone wants to read additional data from the same stream. As there is also no information about how many bytes/character have been read, it is also not trivial to work around this by using a BufferedReader/BufferedInputStream with mark() and reset().

@jon-hanson
Copy link
Author

Regardless of whether the spec does or doesn't make any guarantees, this behaviour is almost certainly not by design and seems to be, as you describe, an unanticipated side-effect of the implementation of JsonTokenizer It also caused me issues, to the extent that I ended up ditching this parser and writing my own. Given that it is problematic and (most likely) unintended, I would class it as a bug.

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

2 participants