Skip to content

Check for trailing garbage inside json (rework #80)#149

Closed
DimitriPapadopoulos wants to merge 2 commits into
json-parser:masterfrom
DimitriPapadopoulos:rework_tmegow
Closed

Check for trailing garbage inside json (rework #80)#149
DimitriPapadopoulos wants to merge 2 commits into
json-parser:masterfrom
DimitriPapadopoulos:rework_tmegow

Conversation

@DimitriPapadopoulos

Copy link
Copy Markdown
Contributor

Initial attempt to rework and understand #80.

@DimitriPapadopoulos

Copy link
Copy Markdown
Contributor Author

I have squashed the first commits from #80:

and left out the last commit (for now) because it is the one that introduces unwanted formatting changes:

  • d63b9d8 Allow for empty objects and empty arrays

@DimitriPapadopoulos DimitriPapadopoulos force-pushed the rework_tmegow branch 2 times, most recently from 4a274a9 to 16f46c3 Compare November 5, 2021 08:32
@DimitriPapadopoulos DimitriPapadopoulos changed the title Check for trailing garbage inside json Check for trailing garbage inside json (rework #80) Nov 5, 2021
@DimitriPapadopoulos DimitriPapadopoulos force-pushed the rework_tmegow branch 3 times, most recently from 5cc8dd7 to a3cfd80 Compare November 5, 2021 08:54
@LB--

LB-- commented Nov 6, 2021

Copy link
Copy Markdown
Member

Thanks for cleaning it up. It's still unclear to me what this checks for or why it is so complicated. We should first create the test JSON files that fail in order to understand what a solution would be.

Also, I'm sort of concerned that the exclusion of the "Allow for empty objects and empty arrays" commit hasn't resulted in a test failure... do we not have tests with empty objects and empty arrays?

@DimitriPapadopoulos

Copy link
Copy Markdown
Contributor Author

Still unclear to me too! I'm cleaning up the patch in the hope of understanding what #80 is supposed to fix. Then we can write a relevant test. Then perhaps we can suggest simpler code.

@tmegow tmegow left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DimitriPapadopoulos I see your comment here about parsers being more lenient than linters. I didn't realize this is the case; today I learned this from you!

My goal with these changes is to parse AND lint ECMA-404 JSON which doesn't strictly accept trailing commas (like the example below).

I appreciate you creating this cleaned-up PR. Also, thank you for the review + suggestions on #80. I will happily add your suggestions. Can I be given permission to edit this branch?

Still unclear to me too! I'm cleaning up the patch in the hope of understanding what #80 is supposed to fix. Then we can write a relevant test. Then perhaps we can suggest simpler code.

Do these logs help clarify my goal?

$ cat ../tests/invalid-0011.json # an example relevant test
{
    "a": "b",
    "c": "d",
    "foo": [
        "a",
    ],
    "trailing-comma": {},
}

$ git checkout master && gcc -o test_json -I.. test_json.c ../json.c -lm && ./test_json ../tests/invalid-0011.json # This parses successfully with trailing commas in objects/arrays.

Already on 'master'
Your branch is up to date with 'origin/master'.
{
    "a": "b",
    "c": "d",
    "foo": [
        "a",
    ],
    "trailing-comma": {},
}

--------------------------------

 object[0].name = a
  string: b
 object[1].name = c
  string: d
 object[2].name = foo
  array
   string: a
 object[3].name = trailing-comma

$ git checkout rework && gcc -o test_json -I.. test_json.c ../json.c -lm && ./test_json ../tests/invalid-0011.json # I desire this to fail because linting fails

Switched to branch 'rework'
{
    "a": "b",
    "c": "d",
    "foo": [
        "a",
    ],
    "trailing-comma": {},
}

--------------------------------

Unable to parse data

To this review, I added the content from the d63b9d8 Allow for empty objects and empty arrays commit which was excluded from the Draft PR. These lines fix the fact that the trailing_garbage function+invocation broke empty object/array parsing. Also trailing_garbage can probably be renamed to trailing_comma (%s/trailing_garbage/trailing_comma/g) because commas are the only reserved character which can wind up at the end of objects/arrays and end up with this successful parsing.

Because I desire a version of this parser which also does this particular linting, have you already thought of a different (improved) strategy to achieve this goal? I am curious to see any improvements on my attempt.

Comment thread json.c Outdated
@DimitriPapadopoulos

Copy link
Copy Markdown
Contributor Author

I think it's best if you can :

  • remove all formatting changes from the master branch of tmegow/json-parser, so that the intent of the patch is clear and not lost in the noise from the formatting changes,
  • then move these changes to a specific branch in your repo, such as lint or trailing_garbage,
  • apply any additional changes,
  • update your master branch from upstream,
  • rebase on master because there have been changes since 2019.

tmegow and others added 2 commits December 7, 2021 12:18
Co-authored-by: Thad Megow <thad@sharpspring.com>
@DimitriPapadopoulos

DimitriPapadopoulos commented Dec 7, 2021

Copy link
Copy Markdown
Contributor Author

That being said, note that json-parser is defined as a "very low footprint JSON parser", so I don't know if the changes required for the linter will be applied. I guess it depends on the footprint of the additional code. In any case the decision does not belong to me.

@tmegow

tmegow commented Dec 7, 2021

Copy link
Copy Markdown

@DimitriPapadopoulos I created #156 , may I request a review? Perhaps this draft can be closed.

@DimitriPapadopoulos

Copy link
Copy Markdown
Contributor Author

Superseded by #156.

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 this pull request may close these issues.

3 participants