Skip to content

php jsonc implementation#22890

Open
otar wants to merge 2 commits into
php:masterfrom
otar:claude/php-jsonc-implementation-lm6vm0
Open

php jsonc implementation#22890
otar wants to merge 2 commits into
php:masterfrom
otar:claude/php-jsonc-implementation-lm6vm0

Conversation

@otar

@otar otar commented Jul 26, 2026

Copy link
Copy Markdown

No description provided.

claude added 2 commits July 24, 2026 20:27
Introduce two new flags accepted by json_decode() and json_validate():

- JSON_ALLOW_COMMENTS: treats // single-line comments (terminated by
  newline or end of input) and /* ... */ block comments as whitespace
  anywhere whitespace is allowed. Block comments do not nest; an
  unterminated /* is reported as JSON_ERROR_SYNTAX at the comment
  opener. Newlines inside block comments advance the line counter so
  error locations stay accurate. Comment-like sequences inside strings
  are untouched.

- JSON_ALLOW_TRAILING_COMMAS: permits exactly one trailing comma after
  the last object member or array element ([1,] and {"a":1,}), while
  [,], {,}, [1,,2] and [1,,] remain syntax errors.

Comments are handled in the re2c scanner (rules match unconditionally,
the flag is checked in the action so the flag-off error is byte
identical to the previous behavior), trailing commas via new
"member ','" / "element ','" productions in the bison grammar with the
flag checked at reduce time, keeping the grammar conflict-free and the
flag-off error code and line:column identical to before. The partially
built container is freed explicitly on the YYERROR path since bison
does not run destructors for the RHS of the failing rule.

Using both flags together makes json_decode() a superset of JSONC as
used by VS Code configuration files, tsconfig.json and similar formats.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TUEZ4aW8xZqzutZBAxX9UJ
The new "member ','" / "element ','" reduces are installed as bison
default actions in their states, so a scanner error token arriving
right after a comma (control character, invalid UTF-8, unpaired UTF-16
surrogate) executed the semantic action before the parse error was
detected, overwriting the scanner's specific error code with
JSON_ERROR_SYNTAX when the flag was unset. Preserve an already-set
errcode, mirroring php_json_yyerror(), so json_decode("[1,\x01]")
keeps reporting JSON_ERROR_CTRL_CHAR exactly as before this feature.

Also terminate // comments at a bare CR in addition to LF, matching
how the scanner already treats CR ("\r"? "\n" line terminators, CR as
whitespace) and how JSONC editors treat line comments.

Extend the tests to pin: the preserved error codes above, empty
containers whose only content is a comment, CRLF and bare-CR comment
termination, comment bodies being opaque to UTF-8 validation, a
comment in colon position, [1,} state mismatch, a comment between an
opener and a lone comma, JSON_THROW_ON_ERROR with trailing commas, and
CRLF line counting inside block comments.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TUEZ4aW8xZqzutZBAxX9UJ
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants