Add global handlers for all data types#1587
Conversation
|
Hello. Best, Marcin. |
|
Hi, In some cases, the default serialization behavior needs to be customized. For example, in my current use case, if a DTO is marked with a specific attribute (e.g., #[CustomDTO]), I need to apply custom serialization logic—such as inspecting property attributes and executing specific logic when a custom attribute is found. My current implementation maps a handler to every DTO within a given namespace. This approach relies heavily on Composer’s autoloading and requires cache revalidation whenever changes occur in that namespace (e.g., when a new DTO is created). Allowing handlers to apply to all types would enable seamless customization of the default serialization process without the need for complex workarounds. |
|
I think adding custom Handler for all classes will create multiple issues - for example with depth exclusions that might not work as expected. |
|
The issue I encountered with events during deserialization is that they operate on an already deserialized instance. If the DTO is readonly, modifying its properties directly isn't possible. Instead, I would need to create a new instance with the updated data. However, the event system doesn't allow replacing the deserialized result. Maybe there is another way to achieve this? What do you think? |
|
Have you checked |
|
Yes, I tried using pre_deserialize also. And I think it still isn't useful in my use-case, since I can't change the data directly |
|
Hi! Any updates? |
|
Can you try the following please? public function onPreSerializeEnum(PreSerializeEvent $event): void
{
$oldType = $event->getType();
$event->setType('*', $oldType);
}Register custom handler for Soo in that way I believe you should achieve your goals. :) |
|
Hi! Thank you for your help! I tried using subscriber like this, and it looks like it changes the type for the serialization process. I still want to keep the type, but I want to fallback to default serialization handler ("*") if no specific handler was provided. That's why unfortunately this won't work for me😔 |
|
Hi! Any updates? |
|
Hi! Best. |
|
Hi, @goetas! Any updates? |
Allow custom handlers for all types. ("type" => "*").