<?php
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\PropertyNormalizer;
$jsonEncoder = new JsonEncoder();
$normalizer = new PropertyNormalizer();
$serializer = new Serializer([$normalizer], [$jsonEncoder]);
$array = ['id' => 1, 'foo' => 'bar'];
class Foo
{
private $id;
private $foo;
}
$serialized = $serializer->serialize($array, 'json');
$foo = new Foo();
$unserialized = $serializer->deserialize(
$serialized,
Foo::class,
'json',
[
'object_to_populate' => $foo,
]
);
dump(
$unserialized === $foo,
$unserialized
);