Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony/serializer Normalize Object in Object

Hi all need little help I didn't find a response about recursive normalize object

    class User
    {
        public $email;
        public $userId;
        public $userName;
        /**
         * @var CustomAttributes
         */
        public $customAttributes;
    }
    class CustomAttributes
    {
        public $someStuff;
        public $someStuffHere;
    }

I just want to transform this into an array snake_case via normalize() of symfony component

$normalizer = new PropertyNormalizer(null, new CamelCaseToSnakeCaseNameConverter());
$user_normalize = $normalizer->normalize($user);

but I have this error

In AbstractObjectNormalizer.php line 129: Cannot normalize attribute "customAttributes" because the injected serializer is not a normalizer

Thx for your help

like image 246
Jean-Baptiste Noblot Avatar asked Jun 10 '26 12:06

Jean-Baptiste Noblot


1 Answers

It is because you forgot to add $serializer = new Serializer([$normalizer]) bit. See the working example below.

IMPLEMENTATION

use App\User;
use App\CustomAttributes;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
use Symfony\Component\Serializer\Normalizer\PropertyNormalizer;
use Symfony\Component\Serializer\Serializer;

class SnakeCaseUtility
{
    public function convert(): array
    {
        $classMetadataFactory = null;
        $nameConverter = new CamelCaseToSnakeCaseNameConverter();

        $normalizer = new PropertyNormalizer($classMetadataFactory, $nameConverter);
        $serializer = new Serializer([$normalizer]);

        $normalizedUser = $serializer->normalize($this->getUser(), 'json');

        return $normalizedUser;
    }

    private function getUser(): User
    {
        $customAttributes = new CustomAttributes();
        $customAttributes->someStuff = 'Some stuff';
        $customAttributes->someStuffHere = 'Some stuff here';

        $user = new User();
        $user->userId = 123;
        $user->userName = 'Hello World';
        $user->email = '[email protected]';
        $user->customAttributes = $customAttributes;

        return $user;
    }
}

TEST

use App\SnakeCaseUtility;
use PHPUnit\Framework\TestCase;

class SnakeCaseUtilityTest extends TestCase
{
    public function testSnakeCase(): void
    {
        $expected = [
            'email' => '[email protected]',
            'user_id' => 123,
            'user_name' => 'Hello World',
            'custom_attributes' => [
                'some_stuff' => 'Some stuff',
                'some_stuff_here' => 'Some stuff here',
            ]
        ];

        $this->assertSame($expected, (new SnakeCaseUtility())->convert());
    }
}

RESULT

$ vendor/bin/phpunit --filter SnakeCaseUtilityTest tests/SnakeCaseUtilityTest.php 
PHPUnit 7.5.1 by Sebastian Bergmann and contributors.

.                                                                   1 / 1 (100%)

Time: 83 ms, Memory: 4.00MB

OK (1 test, 1 assertion)
like image 199
BentCoder Avatar answered Jun 12 '26 02:06

BentCoder