|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #60924 ArrayObject should implement JsonSerializable interface
Submitted: 2012-01-29 17:45 UTC Modified: 2020-12-07 18:13 UTC
Avg. Score:4.6 ± 1.0
Reproduced:6 of 6 (100.0%)
Same Version:2 (33.3%)
Same OS:4 (66.7%)
From: chris at cmbuckley dot co dot uk Assigned: cmb (profile)
Status: Closed Package: SPL related
PHP Version: 5.4.0RC6 OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
Solve the problem:
40 + 21 = ?
Subscribe to this entry?

 [2012-01-29 17:45 UTC] chris at cmbuckley dot co dot uk
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($ao instanceof JsonSerializable);

Expected result:
string(13) "{"foo":"bar"}"
string(13) "{"foo":"bar"}"

Actual result:
string(13) "{"foo":"bar"}"
PHP Fatal error:  Call to undefined method ArrayObject::jsonSerialize() in - on line 6


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2020-12-07 15:41 UTC]
-Status: Open +Status: Feedback -Package: Class/Object related +Package: SPL related -Assigned To: +Assigned To: cmb
 [2020-12-07 15:41 UTC]
Being able to be json_encode()d, is the standard behavior of PHP
objects.  Why should ArrayObject implement JsonSerializable?
 [2020-12-07 18:02 UTC] chris at cmbuckley dot co dot uk
-Status: Feedback +Status: Assigned
 [2020-12-07 18:02 UTC] 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 18:13 UTC]
-Status: Assigned +Status: Closed
 [2020-12-07 18:13 UTC]
Thanks for the swift reply! :)
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Sat Jan 22 11:03:43 2022 UTC