-
Notifications
You must be signed in to change notification settings - Fork 8.1k
JSON: Add opt-in comments and trailing comma flags #23336
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
Draft
otar
wants to merge
2
commits into
php:master
Choose a base branch
from
otar:feature/json-comments-and-trailing-comma-parsing
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+553
−7
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| --TEST-- | ||
| json_decode() and json_validate() with JSON_ALLOW_COMMENTS | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| function decode_and_validate(string $json): void { | ||
| $value = json_decode($json, true, 512, JSON_ALLOW_COMMENTS); | ||
| echo json_encode($value, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n"; | ||
| var_dump(json_validate($json, 512, JSON_ALLOW_COMMENTS)); | ||
| } | ||
|
|
||
| /* Exercise every whitespace position in objects and arrays. */ | ||
| decode_and_validate('/* before root */ { | ||
| /* after object opener */ | ||
| "a" /* after key */ : /* after colon */ | ||
| [ /* after array opener */ 1 /* before comma */, | ||
| /* after comma */ 2 /* before array closer */ ] | ||
| /* after value */, /* after member comma */ | ||
| "b"/* before colon */:/* after colon */3/* before object closer */ | ||
| } /* after root */'); | ||
|
|
||
| decode_and_validate('[1 // before comma | ||
| , 2 // before closer | ||
| ]'); | ||
| decode_and_validate('/* object */{/* only trivia */}'); | ||
| decode_and_validate('/* array */[/* only trivia */]'); | ||
|
|
||
| /* Line comments end at LF, CR, CRLF, or end-of-input. */ | ||
| decode_and_validate("// LF\n1"); | ||
| decode_and_validate("// CR\r2"); | ||
| decode_and_validate("// CRLF\r\n3"); | ||
| decode_and_validate("4// EOF"); | ||
| decode_and_validate("// U+2028 \u{2028} and U+2029 \u{2029}\n6"); | ||
| decode_and_validate("// embedded NUL: a\x00b\n7"); | ||
|
|
||
| /* Comment delimiters inside strings are ordinary string content. */ | ||
| decode_and_validate('["//", "/* not a comment */", "https://php.net/a/*b*/"]'); | ||
|
|
||
| /* Valid Unicode, Unicode line separators, controls, and NUL are comment text. */ | ||
| decode_and_validate("/* héllo \u{2028} world \u{2029} \x01\x0c\x00 */ 5"); | ||
| decode_and_validate('/* ' . str_repeat('x', 65536) . ' */ 8'); | ||
|
|
||
| ?> | ||
| --EXPECT-- | ||
| {"a":[1,2],"b":3} | ||
| bool(true) | ||
| [1,2] | ||
| bool(true) | ||
| [] | ||
| bool(true) | ||
| [] | ||
| bool(true) | ||
| 1 | ||
| bool(true) | ||
| 2 | ||
| bool(true) | ||
| 3 | ||
| bool(true) | ||
| 4 | ||
| bool(true) | ||
| 6 | ||
| bool(true) | ||
| 7 | ||
| bool(true) | ||
| ["//","/* not a comment */","https://php.net/a/*b*/"] | ||
| bool(true) | ||
| 5 | ||
| bool(true) | ||
| 8 | ||
| bool(true) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| --TEST-- | ||
| JSON_ALLOW_COMMENTS rejects malformed or misplaced comments | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| function decode_error(string $json, int $flags = JSON_ALLOW_COMMENTS): void { | ||
| var_dump(json_decode($json, true, 512, $flags)); | ||
| echo json_last_error(), ": ", json_last_error_msg(), "\n"; | ||
| } | ||
|
|
||
| /* Strict parsing is unchanged. */ | ||
| decode_error('// comment\n1', 0); | ||
| decode_error('/* comment */ 1', 0); | ||
|
|
||
| /* Comments are trivia, not values or token fragments. */ | ||
| decode_error('// comment only'); | ||
| decode_error('/* comment only */'); | ||
| decode_error('# comment\n1'); | ||
| decode_error('/'); | ||
| decode_error('tr/* comment */ue'); | ||
| decode_error('1e/* comment */2'); | ||
| decode_error('-/* comment */1'); | ||
| decode_error('{"a" /* missing colon */ 1}'); | ||
|
|
||
| /* Block comments do not nest; the first closing delimiter wins. */ | ||
| decode_error('/* outer /* inner */ */ 1'); | ||
|
|
||
| /* Unterminated block comments report their opening delimiter. */ | ||
| decode_error('/* unterminated'); | ||
| decode_error("{\n \"a\": 1,\n /* unterminated\n still open"); | ||
|
|
||
| try { | ||
| json_decode("[1,\n /* unterminated", true, 512, | ||
| JSON_ALLOW_COMMENTS | JSON_THROW_ON_ERROR); | ||
| } catch (JsonException $e) { | ||
| echo get_class($e), ': ', $e->getCode(), ': ', $e->getMessage(), "\n"; | ||
| } | ||
|
|
||
| ?> | ||
| --EXPECT-- | ||
| NULL | ||
| 4: Syntax error near location 1:1 | ||
| NULL | ||
| 4: Syntax error near location 1:1 | ||
| NULL | ||
| 4: Syntax error near location 1:16 | ||
| NULL | ||
| 4: Syntax error near location 1:19 | ||
| NULL | ||
| 4: Syntax error near location 1:1 | ||
| NULL | ||
| 4: Syntax error near location 1:1 | ||
| NULL | ||
| 4: Syntax error near location 1:1 | ||
| NULL | ||
| 4: Syntax error near location 1:2 | ||
| NULL | ||
| 4: Syntax error near location 1:1 | ||
| NULL | ||
| 4: Syntax error near location 1:26 | ||
| NULL | ||
| 4: Syntax error near location 1:22 | ||
| NULL | ||
| 4: Syntax error near location 1:1 | ||
| NULL | ||
| 4: Syntax error near location 3:3 | ||
| JsonException: 4: Syntax error near location 2:2 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,57 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| --TEST-- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| JSON_ALLOW_COMMENTS interactions with malformed UTF-8 flags | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| --FILE-- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <?php | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| function show_decode(string $json, int $flags): void { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var_dump(json_decode($json, true, 512, JSON_ALLOW_COMMENTS | $flags)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| echo json_last_error(), "\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $badBlockComment = "/* bad: \x80 */ [\"a\x80b\"]"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $badLineComment = "// bad: \x80\n1"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| show_decode($badBlockComment, 0); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| show_decode($badBlockComment, JSON_INVALID_UTF8_IGNORE); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| show_decode($badBlockComment, JSON_INVALID_UTF8_SUBSTITUTE); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| show_decode($badLineComment, 0); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| show_decode($badLineComment, JSON_INVALID_UTF8_IGNORE); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var_dump(json_validate($badBlockComment, 512, JSON_ALLOW_COMMENTS)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| echo json_last_error(), "\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var_dump(json_validate( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $badBlockComment, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 512, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| JSON_ALLOW_COMMENTS | JSON_INVALID_UTF8_IGNORE | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| )); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| echo json_last_error(), "\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| json_decode($badLineComment, flags: JSON_ALLOW_COMMENTS | JSON_THROW_ON_ERROR); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } catch (JsonException $e) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| echo $e->getCode(), ': ', $e->getMessage(), "\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ?> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| --EXPECT-- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| NULL | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| array(1) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| [0]=> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| string(2) "ab" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| array(1) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| [0]=> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| string(5) "a�b" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| NULL | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| int(1) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| bool(false) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| bool(true) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5: Malformed UTF-8 characters, possibly incorrectly encoded near location 1:9 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+31
to
+57
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.