adding JsonSerializable to some core objects would make it possible to create more meaningfull arrays from objects.
https://www.php.net/manual/en/jsonserializable.jsonserialize.php
like the kirby\cms\page
class with its kirby\cms\template
could serialize into an array that has the props of the page. one of them would be the template object.
$props = json_encode($page);
currently the the page array will have a key for template with an empty array, but thats rather useless.
count($props['template']) === 0;
the idea is to get props of “last leaf”-objects, like the template, but not to resolve all down the object-tree, possibly recursive. so not resolving of stuff like the parent
prop.
Most of our core objects have a toArray()
method already. I think we should build on top of that. Sometimes the existing toArray()
implementation might not be fully ideal yet, we are open for pull requests with improvements in this area.
JsonSerializable
can make sense on top of that, but it would require a bit of extra boilerplate code. I think for most classes I’d prefer to stick with toArray()
, which can then be used not only for JSON serialization, but also for other data export formats.