Bug->Req #76186 [Opn]: ArrayObject should be able to be Json encoded as an Array
| From: | requinix@php.net | Date: | Thu, 05 Apr 2018 14:39:47 +0000 |
| Subject: | Bug->Req #76186 [Opn]: ArrayObject should be able to be Json encoded as an Array | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-214620@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=76186&edit=1
ID: 76186
Updated by: requinix@php.net
Reported by: lionel at lionelmartin dot eu
-Summary: ArrayIterator is always Json encoded as an Object
+Summary: ArrayObject should be able to be Json encoded as an
Array
Status: Open
-Type: Bug
+Type: Feature/Change Request
Package: SPL related
Operating System: Linux
-PHP Version: 5.6.35
+PHP Version: 7.1
Block user comment: N
Private report: N
New Comment:
In both cases you can use iterator_to_array(). https://3v4l.org/09dT1
ArrayObject is an object so this behavior makes sense, but having it implement JsonSerializable
sounds reasonable.
@fractalesque: I think the idea is that an array and an ArrayObject both containing the same data
should be serialized the same way. Yes, the class would have special handling just for this case. I
assume internally it could just be a matter of adding a JsonSerializable::jsonSerialize() that
returns the private $storage array.
However ArrayIterator isn't as easy because it's an iterator, so keys (and values) are not
known ahead of time. That would mean processing the iterator twice: once to identify whether it can
constitute a JSON array, then again to actually serialize the data. Of course the first pass would
have to store the values in a temporary array in case the iterator being serialized can't run
again, and that temporary array would be what gets serialized in the second pass.
Which makes it all the same as using iterator_to_array().
In my opinion, iterators should always be serialized as objects, for simplicity, and if the
developer knows the data is suitable then they can/should use iterator_to_array() explicitly. But I
think it's a topic debatable enough to warrant discussing on the internals mailing list, if
you'd like to start that. http://php.net/mailing-lists.php
Previous Comments:
------------------------------------------------------------------------
[2018-04-05 14:14:01] fractalesque at gmail dot com
I fail to understand the "bug" part of your issue. It really is expected that an
ArrayObject representation of an array be encoded as an object in json.
The contrary would be quite unsettling, actually.
------------------------------------------------------------------------
[2018-04-05 14:01:52] lionel at lionelmartin dot eu
Description:
------------
When using json_encode on an ArrayIterator (or ArrayObject), the result is always an object contrary
to its json_encoded array representation.
Test script:
---------------
$array = ['my', 'test', 'array'];
$arrayObject = new \ArrayObject($array);
$arrayIterator = $arrayObject->getIterator();
echo json_encode($array);
echo json_encode($arrayObject);
echo json_encode($arrayIterator);
Expected result:
----------------
["my","test","array"]
["my","test","array"]
["my","test","array"]
Actual result:
--------------
["my","test","array"]
{"0":"my","1":"test","2":"array"}
{"0":"my","1":"test","2":"array"}
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=76186&edit=1