php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #79972 Implement JsonSerializable interface in stdClass
Submitted: 2020-08-13 13:15 UTC Modified: 2020-08-13 13:58 UTC
From: fodor dot laszlo at gmail dot com Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 7.4.9 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:
MUST BE VALID
Solve the problem:
40 - 27 = ?
Subscribe to this entry?

 
 [2020-08-13 13:15 UTC] fodor dot laszlo at gmail dot com
Description:
------------
It would be great if stdClass would implement JsonSerializable for multiple reasons.
First of all, since the interface has been introduced a lot of methods are expecting JsonSerializable as a parameter to work with by type-hinting the parameter.
And sometimes these methods should work properly with an array as well.
If stdClass would implement JsonSerializable interface a simple (object) casting would be enough to pass an array to such method.

Another reason is testing. Currently I need to set up a Mock object to test a method what expects a JsonSerializable. Instead I could just use a simple stdClass with some dummy data in it


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-08-13 13:23 UTC] brzuchal@php.net
-Status: Open +Status: Not a bug
 [2020-08-13 13:23 UTC] brzuchal@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.

The stdClass is a class which don't implement any interface on purpose
cause it has no behaviour. Whenever you cast to an (object) or use
json_decode() you get stdClass objects initially with no behaviour as well.

Thank you for your interest in PHP.
 [2020-08-13 13:31 UTC] nikic@php.net
I think it's worth mentioning here that using JsonSerializable as a type hint makes very little sense. The JsonSerializable class is a bit of a misnomer, and should really be something like HasCustomJsonSerializationLogic. Your default assumption should be that *everything* is JSON serializable.
 [2020-08-13 13:32 UTC] fodor dot laszlo at gmail dot com
Yes, as I stated this is more of a feature request.
 [2020-08-13 13:43 UTC] fodor dot laszlo at gmail dot com
nikic@php.net
Not sure I get what you mean.
As I understood the point of this interface is to be able to decide if a given object van be serialized into json or not. And reason it has been added to PHP is that before everyone added the some into their projects by hand anyway.
If I define a method what will for example generate a json response by the given input I expect that the given input can be serialized hence I typehint them.
Otherwise it could be literally used with anything and would just generate an empty json object.
Serializable? For sure, but doesn't make much sense.
I agree that the name `HasCustomJsonSerializationLogic` you proposed would be better. Used something like that before JsonSerializable has been implemented in PHP
 [2020-08-13 13:49 UTC] nikic@php.net
> Otherwise it could be literally used with anything and would just generate an empty json object.

The default JSON serialization behavior is to just serialize the properties. If you have a class

class Point {
    public int $x;
    public int $y;
    public int $z;
}

then the natural serialization for that is {"x": 0, "y": 1, "z": 2}, which is what you get by default -- you do need to and should not implement JsonSerializable in that case. You should treat any value under the assumption that it can be serialized (but check for encoding errors).

(In fact, a reasonable use of JsonSerializable would be to make the implementation throw unconditionally if your object is not serializable -- the fact that it implements JsonSerializable clearly doesn't indicate serializability in that case.)
 [2020-08-13 13:58 UTC] fodor dot laszlo at gmail dot com
I get that.
The thing is that in all the projects I saw in the past years, public properties are quite rare. Everything is hidden and can be only accessed via the defined methods
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 09:01:28 2024 UTC