yiisoft / json

JSON encoding and decoding
https://www.yiiframework.com/
BSD 3-Clause "New" or "Revised" License
26 stars 8 forks source link

About jsonSerialize() #44

Open Tigrov opened 11 months ago

Tigrov commented 11 months ago

About jsonSerialize()

PHP documentation mentions: Returns data which can be serialized by json_encode()

processObject() should return the result of jsonSerialize() without processing:

private static function processObject(object $data)
{
    if ($data instanceof JsonSerializable) {
        return $data->jsonSerialize();
    }
    ...
}

But this will break BC.

If it is requered to pre-process data for JsonSerializable, this should be done inside jsonSerialize():

class SomeClass implements JsonSerializable
{
    ...
    public function jsonSerialize(): mixed
    {
        return Json:process($this->data);
    }
    ...
}
Q A
Is bugfix? ✔️
New feature?
Breaks BC? ✔️