Req #81112 [NEW]: Special json_encode behavior for SplFixedArray
| From: | parsonswy at gmail dot com | Date: | Sun, 06 Jun 2021 20:04:08 +0000 |
| Subject: | Req #81112 [NEW]: Special json_encode behavior for SplFixedArray | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-234234@lists.php.net to get a copy of this message | ||
From: parsonswy at gmail dot com
Operating system: Ubuntu 20.04.2
PHP version: 8.0.7
Package: SPL related
Bug Type: Feature/Change Request
Bug description:Special json_encode behavior for SplFixedArray
Description:
------------
SplFixedArray is a class and it's objects are therefore serialized as
objects ('{}') by json_encode(), not arrays ('[]'). I've marked this as
a feature change because I believe this is technically what is supposed
to happen since SplFixedArrays are objects, but I also think this
current behavior is not intuitive or useful. SplFixedArray only accepts
numeric indices and should always be representable as a JSON array. This
would be consistent with the behavior of the primitive array type which
will serialize as an array so long as there are no non-integer keys.
My current use case is developing a RESTful API which is queried by
Javascript clients. It is annoying to either cast SplFixedArray objects
to (array)s before serializing them, or convert them client side. Index
access is the same in Javascript whether the key is '0' or the Integer
0, but having a map instead of an array means functions like Array.map()
do not work.
I couldn't find any discussion in an existing bug ticket where a
decision about this special case had been made. This ticket
https://bugs.php.net/bug.php?id=76186 is
similar, but I think
SplFixedArrays warrant a separate decision as ArrayObjects support
Map-like access with non-numeric keys and SplFixedArrays do not.
Test script:
---------------
$spl_array = new SplFixedArray(1);
$std_array = [];
echo json_encode($spl_array) . PHP_EOL;
echo json_encode( (array) $spl_array ) . PHP_EOL;
echo json_encode($std_array);
$std_array[] = ['0 value'];
echo json_encode($std_array) . PHP_EOL;
Expected result:
----------------
[null] // SplFixedArray intuitive behavior
[null] // Explicit array cast work around to achieve intuitive
behavior
// normal array behavior, when representable as array
[]
["0_value"]
Actual result:
--------------
{"0":null} // SplFixedArray current behavior
[null] // Explicit array cast work around to achieve intuitive
behavior
// normal array behavior, when representable as array
[]
["0_value"]
--
Edit bug report at https://bugs.php.net/bug.php?id=81112&edit=1
--
Fix committed: https://bugs.php.net/fix.php?id=81112&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=81112&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=81112&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=81112&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=81112&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=81112&r=support
Expected behavior: https://bugs.php.net/fix.php?id=81112&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=81112&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=81112&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=81112&r=globals
PHP version support discontinued: https://bugs.php.net/fix.php?id=81112&r=phptooold
Daylight Savings: https://bugs.php.net/fix.php?id=81112&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=81112&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=81112&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=81112&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=81112&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=81112&r=mysqlcfg