I've got a nullable property, and I want to serialize it to a specific value even if it's null. It probably can be done by triggering a handler with the specified type even if the value is null
App\Entity\Item:
exclusion_policy: ALL
category:
type: App\Entity\Category
App\Entity\Category:
exclusion_policy: ALL
name: ~
slug: ~
class CategoryHandler implements SubscribingHandlerInterface
{
public static function getSubscribingMethods(): array
{
return [
[
'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION,
'format' => 'json',
'type' => Category::class,
'method' => 'toJson',
],
];
}
public function toJson(JsonSerializationVisitor $visitor, ?Category $category, array $type, Context $context): string
{
if ($category === null) {
return ['name' => 'UNKNOWN', 'slug' => null];
} else {
// IF IT'S NOT NULL, USE DEFAULT SERIALIZATION FLOW
}
}
}
I've got a nullable property, and I want to serialize it to a specific value even if it's null. It probably can be done by triggering a handler with the specified type even if the value is null
Right now, output is always:
And I want it to be: