3

I have an entity which contains a DateTimeInterface type field called completedAt, and I want to deserialize a JSON object to this entity object.

I tried DateTimeNormalizer and ObjectNormalizer, but they both gave me an error saying that I can't assign string/array to DateTime object.

For example, I have this entity,

class Task
{
    private $id;
    private $name;

    /**
     * @var DateTimeInterface
     */
    private $completedAt;
}

and how I create the JSON object in my test using PHPUnit,

$rawData = json_encode([
    'name' => 'test-task',
    'completedAt' => Carbon::now()->toRfc3339String(),
    // I tried some other formats here, such as datetime array, datetime string, etc.
]);

In my controller, I tried this (It's a test, not my real code):

public function update(Request $request, Task $task, SerializerInterface $serializer): Response
{
    $task = new Task();
    $task->setName('test');
    $task->setCompletedAt(Carbon::now()->toDateTime());

    $json = $serializer->serialize($task, 'json');

    $serializer = new Serializer([new ObjectNormalizer(), new DateTimeNormalizer()], [new JsonEncoder()]); // array of needed normalizer
    var_dump($serializer->deserialize($json, Task::class, 'json'));
}

The I got this error,

NotNormalizableValueException Expected argument of type "DateTimeInterface", "string" given at property path "completedAt".

How can I deserialize that JSON object by using multiple normalizers?

2
  • Could you show us the JSON payload you're sending? Commented Mar 28, 2019 at 8:06
  • @thomas.drbg Updated the description. Thanks. Commented Mar 28, 2019 at 23:13

2 Answers 2

1

If you want your json data to be transformed to an entity you create and you use the symfony serialzer

$serializer = new Serializer([new ObjectNormalizer()]); // array of needed normalizer
$serializer->denormalise($json_data,YourEntity::class);

should work just fine

https://symfony.com/doc/current/components/serializer.html

Sign up to request clarification or add additional context in comments.

3 Comments

Tried your method directly from my controller, but it still gives me this error: Expected argument of type "DateTimeInterface", "string" given at property path "completedAt".
Can we have a look a your controller ?
I found the reason! I don't have this package: symfony/property-info. Thanks for the help man.
0

Thanks for the answer under this question: deserializing json to embedded object in Symfony 3.2 - Expected argument of type "Acme\StoreBundle\Document\Gps", "array" given

By following the Symfony doc: https://symfony.com/doc/current/components/serializer.html#recursive-denormalization-and-type-safety

I found I didn't install this package: symfony/property-info

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.