json
json copied to clipboard
About jsonSerialize()
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? | ✔️ |