Serializer – What does the serializer object needs of normalizers to deserialize json into object

I want to convert this json

[
    {
        "name": "1",
        "created": "2019-12-08T15:43:00.000Z"
    },
    {
        "name": "2",
        "created": "2019-12-08T15:43:00.000Z"
    }
]

into a single object.

class Persons
{
    /**
     * @var array<Person>
     */
    public array $persons = [];
}

class Person
{
    public string $name = '';
    public DateTimeInterface $created;
}

And im using this as my serializer

$encoder = [
    new JsonEncoder(),
];

$normalizer = [
    new DateTimeNormalizer(),
    new ObjectNormalizer(),
];

$serializer = new Serializer($normalizer, $encoder);

When running this

$persons = $serializer->deserialize($json, Persons::class, 'json');
var_dump($persons);

$persons is a empty array

So which normalizers do I need for this to work?

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *