2 Methods to Ignore Properties During Serialization in Symfony 7

2 Methods to Ignore Properties During Serialization in Symfony 7

Symfony provides the Serializer component which allows converting objects to specific format (e.g. JSON, XML) and vice versa. By default, all properties are included during object serialization.

This tutorial provides 2 methods how to ignore properties during serialization in Symfony 7 application.

Method 1 - 'Ignore' attribute

Symfony provides the #[Ignore] attribute that allows to specify which properties should be ignored when serializing an object.

src/Entity/User.php

<?php

namespace App\Entity;

use Symfony\Component\Serializer\Attribute\Ignore;

class User
{
    private string $username;

    #[Ignore]
    private string $password;

    public function getUsername(): string { return $this->username; }
    public function setUsername(string $username): void { $this->username = $username; }

    public function getPassword(): string { return $this->password; }
    public function setPassword(string $password): void { $this->password = $password; }
}

In our case, password property is ignored during User object serialization.

src/Controller/TestController.php

<?php

namespace App\Controller;

use App\Entity\User;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\SerializerInterface;

class TestController
{
    #[Route('/')]
    public function index(SerializerInterface $serializer): Response
    {
        $user = new User();
        $user->setUsername('admin');
        $user->setPassword('pwd123');

        $json = $serializer->serialize($user, JsonEncoder::FORMAT);

        return new Response($json); // Output: {"username":"admin"}
    }
}

Method 2 - Context

The serialize method of the Serializer class accepts the third context argument, which allows specifying options for normalizers and encoders. We can pass an array with the names of the properties to ignore using the AbstractNormalizer::IGNORED_ATTRIBUTES key to the serialize method.

src/Controller/TestController.php

<?php

namespace App\Controller;

use App\Entity\User;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Symfony\Component\Serializer\SerializerInterface;

class TestController
{
    #[Route('/')]
    public function index(SerializerInterface $serializer): Response
    {
        $user = new User();
        $user->setUsername('admin');
        $user->setPassword('pwd123');

        $json = $serializer->serialize(
            $user,
            JsonEncoder::FORMAT,
            [AbstractNormalizer::IGNORED_ATTRIBUTES => ['password']]
        );

        return new Response($json); // Output: {"username":"admin"}
    }
}

When context argument is used, we don't need to define an extra attribute in the class that will be serialized.

src/Entity/User.php

<?php

namespace App\Entity;

class User
{
    private string $username;
    private string $password;

    public function getUsername(): string { return $this->username; }
    public function setUsername(string $username): void { $this->username = $username; }

    public function getPassword(): string { return $this->password; }
    public function setPassword(string $password): void { $this->password = $password; }
}

Leave a Comment

Cancel reply

Your email address will not be published.