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

Reading JSON which contains certain extraneous values breaks parsing #24

Closed
noodlecollie opened this issue Jun 20, 2022 · 6 comments · Fixed by #25
Closed

Reading JSON which contains certain extraneous values breaks parsing #24

noodlecollie opened this issue Jun 20, 2022 · 6 comments · Fixed by #25

Comments

@noodlecollie
Copy link
Contributor

I discovered this issue when I was trying to parse a small subset of data from a large JSON message. If I created a set of nested structs with members corresponding to only the data I needed, the parse would fail with an ExpectedObjectStart error at an arbitrary location. However, if I added in all of the values present in the message, it would parse correctly.

I spent some time investigating this and have managed to come up with a minimal reproducible example. In the example the parsing doesn't explicitly fail, but rather the parser seems to get out of sync and places incorrect values into structs. I believe that the parser getting out of sync was the cause of my original issue.

The fundamental issue seems to be surrounding nested objects. Take the following JSON:

{
    "object1":
    {
        "value": 1,
        "nested_object":
        {
            "nested_1":
            {
                    "some_value": "foo"
            },
            "nested_2":
            {
                    "some_value": "bar"
            }
        }
    },
    "object2":
    {
            "value": 2
    }
}

If this message is parsed using structs, where every key-value pair is catered for, then parsing succeeds as expected. However, if nested_object is omitted from the struct definition, this triggers the parsing issue. Note that the issue does not occur unless the nested_object has at least two other nested JSON objects within it.

The attached C++ project encapsulates the minimal reproducible example. In the case where the nested object fails to parse correctly. the struct representing object2 contains a value of 0 when it should contain a value of 2.

json_struct MRE.zip

@noodlecollie
Copy link
Contributor Author

After further investigation, I reckon something is up with the implementation of skipArrayOrObject(). I'll continue looking.

@noodlecollie
Copy link
Contributor Author

I've got an implementation locally that seems to fix the issue, I'll make a PR for it.

@jorgen
Copy link
Owner

jorgen commented Jun 20, 2022

Hi, thank you for your time and effort. I really appreciate it!

@jorgen
Copy link
Owner

jorgen commented Jun 20, 2022

I took the liberty of pushing c2b4906 under your name. Its the testcase you attached, reworked in to a unit test.

@jorgen
Copy link
Owner

jorgen commented Jun 20, 2022

Ok, I was reviewing the code once more, and I compare it with what is done when creating the skiplists for the map object. I have pushed a1874db, its removes the recursion and just loops until done. I don't think its a performance issue, but I like the implementation better. I hope you don't mind that I have overwritten your fix.

@noodlecollie
Copy link
Contributor Author

That's fine by me! I'll make sure it works on my end as well.

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

Successfully merging a pull request may close this issue.

2 participants