Skip to content

JSON: Add opt-in comments and trailing comma flags - #23336

Draft
otar wants to merge 2 commits into
php:masterfrom
otar:feature/json-comments-and-trailing-comma-parsing
Draft

JSON: Add opt-in comments and trailing comma flags#23336
otar wants to merge 2 commits into
php:masterfrom
otar:feature/json-comments-and-trailing-comma-parsing

Conversation

@otar

@otar otar commented Aug 17, 2026

Copy link
Copy Markdown

Configuration files often use a narrow JSON extension with JS-style comments, trailing commas, or both. PHP currently requires a separate parser or a string-aware preprocessing pass. This PR adds native parser support. Strict JSON remains the default.

Public API

Constant Value Accepted by Effect
JSON_ALLOW_COMMENTS 1 << 23 json_decode(), json_validate() Allows // and non-nesting /* ... */ comments at JSON whitespace boundaries.
JSON_ALLOW_TRAILING_COMMAS 1 << 24 json_decode(), json_validate() Allows one final comma in a nonempty array or object.

The flags are independent. Neither flag applies to json_encode() or is enabled by default.

Semantics

  • Line comments end at LF, CRLF, lone CR, or EOF. Block comments do not nest and end at the first */.
  • Comments are allowed before and after the root value and between tokens. They are not allowed inside a token. Comment-like text in a JSON string is unchanged.
  • U+2028/U+2029 and valid controls, including embedded NUL, are comment content. Malformed UTF-8 follows the existing decode flags. Validation still accepts JSON_INVALID_UTF8_IGNORE, but not JSON_INVALID_UTF8_SUBSTITUTE.
  • Unterminated block comments produce JSON_ERROR_SYNTAX at the opening slash.
  • A trailing comma is allowed only after the last element or member of a nonempty container. Empty or missing-element forms such as [,], {,}, and [1,,] remain invalid.
  • The implementation retains existing behavior for depth, duplicate keys, bigints, associative objects, exceptions, error codes, and original-input line and column numbers.

@otar
otar requested review from bukka and kocsismate as code owners August 17, 2026 17:07
@otar
otar marked this pull request as draft August 17, 2026 17:08
@otar

otar commented Aug 17, 2026

Copy link
Copy Markdown
Author

Sorry @bukka and @kocsismate, I accidentally tagged you on this PR. Please ignore this draft...

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.

1 participant