json icon indicating copy to clipboard operation
json copied to clipboard

About jsonSerialize()

Open Tigrov opened this issue 2 years ago • 0 comments

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? ✔️

Tigrov avatar Sep 13 '23 07:09 Tigrov