php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #81112 Special json_encode behavior for SplFixedArray
Submitted: 2021-06-06 20:04 UTC Modified: 2021-06-07 12:51 UTC
From: parsonswy at gmail dot com Assigned:
Status: Closed Package: SPL related
PHP Version: 8.0.7 OS: Ubuntu 20.04.2
Private report: No CVE-ID: None
 [2021-06-06 20:04 UTC] parsonswy at gmail dot com
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"]

Patches

Add a Patch

Pull Requests

Pull requests:

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-06-07 12:51 UTC] nikic@php.net
Adding a JsonSerializable implementation to SplFixedArray would make sense to me.
 [2021-06-08 13:33 UTC] nikic@php.net
The following pull request has been associated:

Patch Name: Implement JsonSerializable for SplFixedArray
On GitHub:  https://github.com/php/php-src/pull/7117
Patch:      https://github.com/php/php-src/pull/7117.patch
 [2021-06-14 08:08 UTC] git@php.net
Automatic comment on behalf of nikic
Revision: https://github.com/php/php-src/commit/805471e86ba6dbe1f7d359b0b8d20f4f203e034a
Log: Fix bug #81112: Implement JsonSerializable for SplFixedArray
 [2021-06-14 08:08 UTC] git@php.net
-Status: Open +Status: Closed
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 15:01:29 2024 UTC