[GIT-PULLS] [php-src] PR #23336: JSON: Add opt-in comments and trailing comma flags
[email protected] (otar)
| Newsgroups | php.git-pulls |
|---|---|
| Message-ID | <8sVuLtLndzgmAvLAiIdGLPRw4i6bhPoPcrMFWdJHxRE@main.internal.php.net> |
Pull Request: https://github.com/php/php-src/pull/23336
Author: otar
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.