DateTime响应字符串的Symfony序列化器

Lee*_*Lee 2 serialization symfony

当尝试执行一个学说查询并将其序列化为json时(不使用JSM,而是使用symfony序列化器),我在json中得到了以下内容:

“”到期“:{”时区“:{”名称“:”欧洲/柏林“,”转场“:[{” ts“:-2147483648,”时间“:” 1901-12-13T20:45:52 + 0000 “,” offset“:3208,” isdst“:false,” abbr“:” LMT“},{” ts“:-2147483648,” time“:” 1901-12-13T20:45:52 + 0000“,” offset“:3600,” isdst“:false,” abbr“:” CET“},{” ts“:-1693706400,” time“:” 1916-04-30T22:00:00 + 0000“,” offset“: 7200,“ isdst”:true,“ abbr”:“ CEST”},{“ ts”:-1680483600,“ time”:“ 1916-09-30T23:00:00 + 0000”,“ offset”:3600,“ isdst“:false,” abbr“:” CET“},{” ts“:-1663455600,” time“:” 1917-04-16T01:00:00 + 0000“,”offset“:7200,” isdst“:true,” abbr“:” CEST“},{” ts“:-1650150000,” time“:” 1917-09-17T01:00:00 + 0000“,” offset“: 3600,“ isdst”:false,“ abbr”:“ CET”},{“ ts”:-1632006000,“ time”:“ 1918-04-15T01:00:00 + 0000”,“ offset”:7200,“ isdst“:true,” abbr“:” CEST“},{” ts“:-1618700400,” time“:” 1918-09-16T01:00:00 + 0000“,” offset“:3600,” isdst“: false,“ abbr”:“ CET”},{“ ts”:-938905200,“ time”:“ 1940-04-01T01:00:00 + 0000”,“ offset”:7200,“ isdst”:true,“ abbr“:” CEST“},{” ts“:-857257200,” time“:” 1942-11-02T01:00:00 + 0000“,” offset“:3600,” isdst“:false,“ abbr”:“ CET”},{“ ts”:-844556400,“ time”:“ 1943-03-29T01:00:00 + 0000”,“ offset”:7200,“ isdst”:true,“ abbr” :“ CEST”},{“ ts”:-828226800,“ time”:“ 1943-10-04T01:00:00 + 0000”,“ offset”:3600,“ isdst”:false,“ abbr”:“ CET “},{” ts“:-812502000,” time“:” 1944-04-03T01:00:00 + 0000“,” offset“:7200,” isdst“:true,” abbr“:” CEST“}, {“ ts”:-796777200,“ time”:“ 1944-10-02T01:00:00 + 0000”,“ offset”:3600,““ time”:“ 1943-10-04T01:00:00 + 0000”,“ offset”:3600,“ isdst”:false,“ abbr”:“ CET”},{“ ts”:-812502000,“ time” :“ 1944-04-03T01:00:00 + 0000”,“ offset”:7200,“ isdst”:true,“ abbr”:“ CEST”},{“ ts”:-796777200,“ time”:“ 1944 -10-02T01:00:00 + 0000“,” offset“:3600,”“ time”:“ 1943-10-04T01:00:00 + 0000”,“ offset”:3600,“ isdst”:false,“ abbr”:“ CET”},{“ ts”:-812502000,“ time” :“ 1944-04-03T01:00:00 + 0000”,“ offset”:7200,“ isdst”:true,“ abbr”:“ CEST”},{“ ts”:-796777200,“ time”:“ 1944 -10-02T01:00:00 + 0000“,” offset“:3600,”

在存储到期日期时,将保存时区,并存储一个附加的时区字段。有没有办法以特定格式提取日期或指定检索时使用的时区?

 public function blahAction(Request $request)
{
    $currentUser = $this->getUser();
    $em = $this->getDoctrine()->getManager();
    $blahs = $em->getRepository('AppBundle:blah')->findAllByStatus($currentUser,'TODO');
    $encoders = array(new XmlEncoder(), new JsonEncoder());
    $normalizer = array(new ObjectNormalizer());
    $serializer = new Serializer($normalizer, $encoders);
    $response = new Response($serializer->serialize($blahs, 'json'));
    $response->headers->set('Content-Type', 'application/json');
    return $response;
}
Run Code Online (Sandbox Code Playgroud)

Sil*_*ioQ 6

您有2种方法来获取RFC3339日期时间格式...

选项1:

添加DateTimeNormalizer作为规范化器。一个示例是https://symfony.com/doc/current/components/serializer.html#recursive-denormalization-and-type-safety

更改

$normalizer = array(new ObjectNormalizer());
Run Code Online (Sandbox Code Playgroud)

通过

$normalizer = array(new DateTimeNormalizer(), new ObjectNormalizer());
Run Code Online (Sandbox Code Playgroud)

选项2

更简单的是使用“ serializer”容器服务...您的代码将如下所示...

public function blahAction(Request $request)
{
    $currentUser = $this->getUser();
    $em = $this->getDoctrine()->getManager();
    $blahs = $em->getRepository('AppBundle:blah')->findAllByStatus($currentUser,'TODO');
    $response = new Response($this->get('serializer')->serialize($blahs, 'json'));
    $response->headers->set('Content-Type', 'application/json');
    return $response;
}
Run Code Online (Sandbox Code Playgroud)

..或者,如果您更喜欢自动装配方式(未选中此代码)

public function blahAction(Request $request, \Symfony\Component\Serializer\SerializerInterface $serializer)
{
    $currentUser = $this->getUser();
    $em = $this->getDoctrine()->getManager();
    $blahs = $em->getRepository('AppBundle:blah')->findAllByStatus($currentUser,'TODO');
    $response = new Response($serializer->serialize($blahs, 'json'));
    $response->headers->set('Content-Type', 'application/json');
    return $response;
}
Run Code Online (Sandbox Code Playgroud)

  • 请注意..在定义 $normalizer 数组时,DateTimeNormalizer() 应该是第一个。顺序很重要。 (6认同)