Req #79972 [Nab]: Implement JsonSerializable interface in stdClass

From: Date: Thu, 13 Aug 2020 13:49:48 +0000
Subject: Req #79972 [Nab]: Implement JsonSerializable interface in stdClass
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-228580@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=79972&edit=1

 ID:                 79972
 Updated by:         nikic@php.net
 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:

> 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.)


Previous Comments:
------------------------------------------------------------------------
[2020-08-13 13:43:13] 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: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)

« previous php.bugs (#228580) next »