Req #60924 [Asn->Csd]: ArrayObject should implement JsonSerializable interface
| From: | cmb@php.net | Date: | Mon, 07 Dec 2020 18:13:17 +0000 |
| Subject: | Req #60924 [Asn->Csd]: ArrayObject should implement JsonSerializable interface | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-230921@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=60924&edit=1
ID: 60924
Updated by: cmb@php.net
Reported by: chris at cmbuckley dot co dot uk
Summary: ArrayObject should implement JsonSerializable
interface
-Status: Assigned
+Status: Closed
Type: Feature/Change Request
Package: SPL related
PHP Version: 5.4.0RC6
Assigned To: cmb
Block user comment: N
Private report: N
New Comment:
Thanks for the swift reply! :)
Previous Comments:
------------------------------------------------------------------------
[2020-12-07 18:02:59] chris at cmbuckley dot co dot uk
Casting my mind back to the intention 8 years ago, I believe the idea was flawed. I think my thought
was something along the lines of "if something can be JSON-serialised, it should implement
JsonSerializable". Though as you say, all PHP objects can be JSON-serialised, so application
code should transparently use
json_encode($obj) and a class should only use
JsonSerializable if it should customise the JSON output.
Feel free to close the ticket.
------------------------------------------------------------------------
[2020-12-07 15:41:04] cmb@php.net
Being able to be json_encode()d, is the standard behavior of PHP
objects. Why should ArrayObject implement JsonSerializable?
------------------------------------------------------------------------
[2012-01-29 17:45:37] chris at cmbuckley dot co dot uk
Description:
------------
PHP 5.4 brings the new JsonSerializable interface. ArrayObject already works with json_encode as
expected but does not explicitly implement this interface.
Test script:
---------------
$ao = new ArrayObject(array('foo' => 'bar'));
var_dump(json_encode($ao));
var_dump($ao instanceof JsonSerializable);
var_dump($ao->jsonSerialize());
Expected result:
----------------
string(13) "{"foo":"bar"}"
bool(true)
string(13) "{"foo":"bar"}"
Actual result:
--------------
string(13) "{"foo":"bar"}"
bool(false)
PHP Fatal error: Call to undefined method ArrayObject::jsonSerialize() in - on line 6
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=60924&edit=1