简体   繁体   English

Symfony序列化Response与规范化JsonResponse

[英]Symfony serialized Response vs normalized JsonResponse

I'm building an API which needs to ouput some entities as JSON. 我正在构建一个需要将某些实体作为JSON输出的API。 I'm trying to figure out if it's better to normalize the entity and pass it to JsonResponse or if I should serialize it and pass it to Response . 我试图找出是否最好标准化该实体并将其传递给JsonResponse或者是否应该序列化并将其传递给Response What is the difference between the two? 两者有什么区别?

/**
 * Returning a Response
 */
public function getEntityAction($id)
{
    $entity = $this->getDoctrine()->getRepository(MyEntity::class)->find($id);

    $json = $this->get('serializer')->serialize($entity);
    $response = new Response($json);
    $response->headers->set('Content-Type', 'application/json');

    return $response
}

/**
 * Returning a JsonResponse.
 */
public function getEntityAction($id)
{
    $entity = $this->getDoctrine()->getRepository(MyEntity::class)->find($id);

    $array = $this->get('serializer')->normalize($entity);
    return new JsonResponse($array);
}

Is there any actual difference between the two, besides the fact I don't have to manually set the Content-Type header for a JsonResponse ? 两者之间是否有实际区别,除了我不必手动为JsonResponse设置Content-Type标头之外?

You can compare the encoder used by the Serializer: JsonEncode with what JsonResponse does. 您可以将Serializer: JsonEncode使用的编码器与JsonResponse进行的比较 Essentially it's the same. 本质上是一样的。 Under the hood both use json_encode to generate a string. 两者都使用json_encode生成字符串。

I guess whatever feels right for your project is a good choice. 我认为适合您的项目的任何选择都是不错的选择。 JsonResponse is mainly for convenience and as you already noted will just automatically set the correct Content Type-header and do the encoding as json for you. JsonResponse主要是为了方便起见,正如您已经指出的那样,JsonResponse会自动设置正确的Content Type-header并为您编码为json。

From what I understand about Symfony serialization, normalization is a part of serialization process in which the object is mapped to an associative array, this array is then encoded to a plain JSON object, accomplishing the serialization. 根据我对Symfony序列化的了解,规范化是序列化过程的一部分,在序列化过程中,对象被映射到关联数组,然后将该数组编码为普通的JSON对象,从而完成了序列化。

Your code using normalize function in fact can be modified to use Response class instead of JsonResponse: 实际上,可以将使用normalize函数的代码修改为使用Response类而不是JsonResponse:

/**
 * Returning a JsonResponse.
 */
public function getEntityAction($id)
{
    $entity = $this->getDoctrine()->getRepository(MyEntity::class)->find($id);

    $array = $this->get('serializer')->normalize($entity);
    $response = new Response(json_encode($array));
    $response->headers->set('Content-Type', 'application/json');
    return $response;
}

I haven't checked the Symfony code for the serialize function, but believe part of it will be the normalize function. 我还没有检查Symfony代码的序列化功能,但相信其中一部分将是规范化功能。 You can find the explain in symfony doc: http://symfony.com/doc/current/components/serializer.html 您可以在symfony文档中找到说明: http//symfony.com/doc/current/components/serializer.html 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM