[英]How to deserialize DateTime in this case?
I can't for the life of me figure out how to handle a regular deserialization.我一辈子都想不出如何处理常规的反序列化。 I've read dozens of SO questions and also the official doc and it seems to be easy.
我已经阅读了数十个 SO 问题以及官方文档,这似乎很容易。 Seems.
似乎。
I've got a simple JSON, like:我有一个简单的 JSON,例如:
[{"id":"00112063002463454431","first_name":"John","last_name":"Doe","date_of_birth":"2006-09-28"}]
Now I'd like to map it to my class Person .现在我想 map 给我的 class Person 。 No matter what I've tried, it always complains about date_of_birth to be string.
无论我尝试过什么,它总是抱怨date_of_birth是字符串。 It is expected to be DateTimeInterface when the routine internally calls
setDateOfBirth(?DateTimeInterface $dateOfBirth)
inside the Person class. But in my understanding DateTimeNormalizer's denormalize() should've already converted it to a DateTime object before it hydrates the Person object, shouldn't it?当例程在 Person class 内部调用
setDateOfBirth(?DateTimeInterface $dateOfBirth)
时,预计将是 DateTimeInterface。但据我了解,DateTimeNormalizer 的denormalize()应该已经将其转换为 DateTime object,然后再为 Person object 补水,不应该是吗?
Inside my class the field is defined as follows:在我的 class 中,字段定义如下:
#[ORM\Column(type: Types::DATE_MUTABLE)]
#[Context([DateTimeNormalizer::FORMAT_KEY => 'Y-m-d'])]
private ?DateTimeInterface $dateOfBirth = null;
Deserializing process:反序列化过程:
$serializer = new Serializer(
[new DateTimeNormalizer(), new GetSetMethodNormalizer(), new ArrayDenormalizer()],
[new JsonEncoder()]
);
$personsFromJson = $serializer->deserialize($requestContent, 'App\Entity\Person[]', 'json');
Is there anything else to do?!还有什么可以做的吗?!
Edit编辑
One-class example:一类例子:
<?php
namespace App\Controller;
use DateTimeInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Serializer\Annotation\Context;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
use Symfony\Component\Serializer\Serializer;
class TestController extends AbstractController
{
#[Route(path: '/test', name: 'app_test')]
public function index(): Response
{
$json = '{"dateOfBirth":"2023-01-31"}';
$serializer = new Serializer(
[new DateTimeNormalizer(), new GetSetMethodNormalizer()],
[new JsonEncoder()]
);
$testPersonFromJson = $serializer->deserialize($json, TestPerson::class, 'json');
return $this->json($testPersonFromJson);
}
}
class TestPerson {
#[Context([DateTimeNormalizer::FORMAT_KEY => 'Y-m-d'])]
private ?DateTimeInterface $dateOfBirth = null;
public function getDateOfBirth(): ?DateTimeInterface {
return $this->dateOfBirth;
}
public function setDateOfBirth(?DateTimeInterface $dateOfBirth): self {
$this->dateOfBirth = $dateOfBirth;
return $this;
}
}
App\Controller\TestPerson::setDateOfBirth(): Argument #1 ($dateOfBirth) must be of type?DateTimeInterface, string given, called in [...]\vendor\symfony\serializer\Normalizer\GetSetMethodNormalizer.php on line 163
App\Controller\TestPerson::setDateOfBirth():参数 #1 ($dateOfBirth) 必须是类型?DateTimeInterface,给定的字符串,在第 163 行的 [...]\vendor\symfony\serializer\Normalizer\GetSetMethodNormalizer.php 中调用
The DateTimeNormalizer() gets instantiated, but indeed, denormalize() gets never called. DateTimeNormalizer() 被实例化,但实际上,denormalize() 从未被调用过。
I had to add a ReflectionExtractor to my ObjectNormalizer.我必须向我的 ObjectNormalizer 添加一个 ReflectionExtractor。 I didn't notice this anywhere.
我在任何地方都没有注意到这一点。
new ObjectNormalizer(null, null, null, new ReflectionExtractor())
https://symfony.com/doc/current/components/property_info.html#reflectionextractor https://symfony.com/doc/current/components/property_info.html#reflectionextractor
Found via the comments after my 18567th search at: Symfony Serialize Entity with Datetime / Deserialize fails在我第 18567 次搜索后通过评论找到: Symfony Serialize Entity with Datetime / Deserialize fails
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.