On Fri, Mar 25, 2016 at 1:45 PM, Nikita Popov <nikita....@gmail.com> wrote:

> Hi internals,
>
> Currently we do not allow (*) creating empty property names on objects,
> i.e.
>
>     $obj->{''} = 42;
>
> is illegal. While empty property names are unlikely to be useful per se,
> they are problematic for deserialization of foreign formats like JSON. To
> avoid this issue {"": null} will currently decode to a property named
> "_empty_" rather than "". Notably, this means that JSON decode and encode
> do not round-trip (as we do not convert _empty_ back to an empty name while
> encoding).
>
> There is no technical reason (that I can see) for keeping this arbitrary
> restriction. I believe that the original reason for the restriction was our
> use of NUL-prefixed property names for name mangling, combined with the
> fact that an empty string at the C level happens to "start" with a NUL
> byte.
>
> A patch to drop the restriction and allow empty property names:
> https://github.com/php/php-src/pull/1836 It does not touch the JSON
> handling, as there are BC concerns involved there, I leave that to the
> ext/json maintainer.
>
> Any objections to changing this?
>
> Regards,
> Nikita
>
> (*) There are roundabout ways to create them anyway.
>

​Currently, this succeeds:

$x = json_decode('{"_empty_": "no", "": "foo"}', true);

While this does not:

$x = json_decode('{"_empty_": "no", "": "foo"}');

​https://3v4l.org/FJHVVhttps://3v4l.org/15Sfm​

I'm personally 50/50 on it. ​I think allowing an empty property is kind of
weird, but not the weirdest behavior PHP allows. Overall, it might
(ironically enough!) make working with JSON _more_ consistent, and probably
have other benefits that I can't even imagine at the moment.

Scott Arciszewski
Chief Development Officer
Paragon Initiative Enterprises <https://paragonie.com/>​

Reply via email to