Usage¶
This bundle exposes the service tsantos_serializer
which contains a
reference to :class:TSantos\Serializer\SerializerInterface
:
$serializer = $container->get('tsantos_serializer');
$serializer = $serializer->serialize($post);
$serializer = $serializer->deserialize($post, Post::class);
Note
The format used by the serializer is the one configured in your configuration file. Please go to the Configuration Reference page to see more information about this configuration.
Note
You can create and register new formats. Please, read the dedicated documentation about encoders at Encoder / Decoder page.
Auto-wiring the Serializer¶
Instead of fetching the serializer directly from the container, you can define the serializer as a dependency:
// src/Controller/DefaultController.php
use TSantos\Serializer\SerializerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
class DefaultController
{
private $serializer;
public function __construct(SerializerInterface $serializer)
{
$this->serializer = $serializer;
}
public function indexAction(): JsonResponse
{
$post = ...;
return JsonResponse::fromJsonString($this->serializer->serialize($post));
}
}