[RFC][Draft] Add json_encode indent parameter

php.internals

Timon de Groot

5 years ago
Hi internals, I'd like to present my RFC for adding the indent parameter to the json_encode function:   https://wiki.php.net/rfc/json_encode_indentation The `string|int $indent = 4` parameter adds the ability to specify the indentation for the JSON encoder. Amount of spaces can be specified by a number and a string can be specified to set a custom indentation like `\t` or whatever. As this is my first RFC (and contribution) for PHP, I'm all kind of new to the process, standards, etc. Please let me know if I could do something better. Looking forward to feedback/input on the RFC.
-- Kind regards, Timon de Groot

Nikita Popov

5 years ago
On Thu, Jun 3, 2021 at 6:11 PM Timon de Groot <tdegroot96@gmail.com> wrote:
> Hi internals, > > I'd like to present my RFC for adding the indent parameter to the > json_encode function: > https://wiki.php.net/rfc/json_encode_indentation > > The `string|int $indent = 4` parameter adds the ability to specify the > indentation for > the JSON encoder. Amount of spaces can be specified by a number and a > string can be > specified to set a custom indentation like `\t` or whatever. > > As this is my first RFC (and contribution) for PHP, I'm all kind of new > to the process, standards, etc. > Please let me know if I could do something better. > > Looking forward to feedback/input on the RFC. >
As has been brought up in https://github.com/php/php-src/pull/7093#issuecomment-855170601, the string version of the $indent parameter allows you to create invalid JSON, something that json_encode() does not currently allow. I tend to agree that it would make sense to validate that the indent only contains whitespace characters, and not 🚀. Regards, Nikita

Jakub Zelenka

5 years ago
On Mon, Jun 7, 2021 at 11:53 AM Nikita Popov <nikita.ppv@gmail.com> wrote:
> On Thu, Jun 3, 2021 at 6:11 PM Timon de Groot <tdegroot96@gmail.com> > wrote: > > > Hi internals, > > > > I'd like to present my RFC for adding the indent parameter to the > > json_encode function: > > https://wiki.php.net/rfc/json_encode_indentation > > > > The `string|int $indent = 4` parameter adds the ability to specify the > > indentation for > > the JSON encoder. Amount of spaces can be specified by a number and a > > string can be > > specified to set a custom indentation like `\t` or whatever. > > > > As this is my first RFC (and contribution) for PHP, I'm all kind of new > > to the process, standards, etc. > > Please let me know if I could do something better. > > > > Looking forward to feedback/input on the RFC. > > > > As has been brought up in > https://github.com/php/php-src/pull/7093#issuecomment-855170601, the > string > version of the $indent parameter allows you to create invalid JSON, > something that json_encode() does not currently allow. I tend to agree that > it would make sense to validate that the indent only contains whitespace > characters, and not 🚀. > >
I agree with this, it should validated to be also consistent with UTF-8 validation. For example if it's not valid utf8, it returns false (unless ignoring or substitution is enabled). In any case it never returns invalid json atm. I don't think it's a good idea to break that assumption. Cheers Jakub