Req #79972 [Nab]: Implement JsonSerializable interface in stdClass
Edit report at https://bugs.php.net/bug.php?id=79972&edit=1
ID: 79972
User updated by: fodor dot laszlo at gmail dot com
Reported by: fodor dot laszlo at gmail dot com
Summary: Implement JsonSerializable interface in stdClass
Status: Not a bug
Type: Feature/Change Request
Package: Class/Object related
PHP Version: 7.4.9
Block user comment: N
Private report: N
New Comment:
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
Previous Comments:
------------------------------------------------------------------------
[2020-08-13 13:32:13] fodor dot laszlo at gmail dot com
Yes, as I stated this is more of a feature request.
------------------------------------------------------------------------
[2020-08-13 13:31:22] 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:23:35] 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:15:41] 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
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=79972&edit=1
Thread (7 messages)