Using the JSON serializer makes the messages a lot more readable. But there are still instances where this is not enough or might even be a step back from the default serializer. For example when using UUIDs. When you want to have this, you need to define a custom serializer.
I've setup a custom serializer anyway to deserialize DTOs from JSON and to return a json serialized response. This is configured in my CustomSerializer class.
Whenever I need a serializer I use it through the CustomSerializer.
You can setup the serializer wherever you want. The nice advantage here is that I can make sure, that everything within the app is serialized the same way.
Now to the Messenger. The only thing that we need to do is to supply this serializer to the messenger. Therefore we will build a custom transport class like the following:
final class CustomTransportSerializer extends Serializer
public function __construct(CustomSerializer $customSerializer)
We simply extend the JSON serializer from Symfony and inject our serializer instead of the default Symfony one. Then we only need to configure it in our messenger.yaml.