Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

683
Views
Symfony Serialize Entity with Datetime / Deserialize fails

Im trying to save an Entity as a json file. The entity class also includes a datetime field:

class Ear
{
...
    /**
     * @ORM\Column(type="date")
     */
    private $date;
...
}

Then I create a new Ear:

$earLine = new Ear();
$earLine->setDate(new \DateTime());

Now I simply would think I can normalize and serialize this object:

$encoders = [new JsonEncoder()];
$normalizers = [new DateTimeNormalizer(), new ObjectNormalizer()];
$serializer = new Serializer($normalizers, $encoders);

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

which works fine. The json looks like this:

{
  "id": null,
  "date": "2021-05-31T00:00:00+02:00",
  ...
}

However upon trying to deserialize, it fails to denormalize the datetime.

$encoders = [new JsonEncoder()];
$normalizers = [new DateTimeNormalizer(), new ObjectNormalizer()];
$serializer = new Serializer($normalizers, $encoders);

dd($serializer->deserialize($json, Ear::class, 'json'));

with the error Failed to denormalize attribute "date" value for class "App\Entity\Ear": Expected argument of type "DateTimeInterface", "string" given at property path "date".

I read the docs up and down and search stackoverflow. But it fails for not apparent reason, at least not for me.

Using Symfony 5.3.

over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!