DateTime 响应字符串的 Symfony 序列化程序 [英] Symfony Serializer of DateTime Response String

查看:62
本文介绍了DateTime 响应字符串的 Symfony 序列化程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When attempting to perform a doctrine query and serialze it to json (not using JSM, using the symfony serializer) I get the following in the json:

""due":{"timezone":{"name":"Europe/Berlin","transitions":[{"ts":-2147483648,"time":"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","偏移":3600,"

""due":{"timezone":{"name":"Europe/Berlin","transitions":[{"ts":-2147483648,"time":"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,"

在存储截止日期时,会保存时区并存储一个额外的时区字段.有没有办法以特定格式提取日期或指定检索时使用的时区?

When storing the due date, the timezone is saved and there is an additional timezone field stored. Is there a way to pull the date in a specific format or specify the timezone to use when retrieving?

 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;
}

推荐答案

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

You have 2 ways to get RFC3339 Datetime format ...

选项 1:

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

Add DateTimeNormalizer as normalizer. An example is https://symfony.com/doc/current/components/serializer.html#recursive-denormalization-and-type-safety.

改变

$normalizer = array(new ObjectNormalizer());

$normalizer = array(new DateTimeNormalizer(), new ObjectNormalizer());

选项 2

更简单的是使用序列化器"容器服务......你的代码看起来像......

More simple is using "serializer" container service ... your code will look like ...

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;
}

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

.. or, if your prefer autowiring way (this code is unchecked)

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;
}

这篇关于DateTime 响应字符串的 Symfony 序列化程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆