如何在symfony 4.2中使用JMSSerializer

问题描述 投票:0回答:3

我正在使用symfony 4.2构建一个Api,并希望使用jms-serializer以Json格式序列化我的数据,安装后使用

composer需要jms / serializer-bundle

当我尝试这样使用它时:

``` demands = $demandRepo->findAll();
    return $this->container->get('serializer')->serialize($demands,'json');```

它给了我这个错误:

Service "serializer" not found, the container inside "App\Controller\DemandController" is a smaller service locator that only knows about the "doctrine", "http_kernel", "parameter_bag", "request_stack", "router" and "session" services. Try using dependency injection instead.

symfony jmsserializerbundle jsonresponse symfony-4.2
3个回答
0
投票

正如我在评论中所说,您可以使用Symfony的默认序列化程序并使用它由构造函数注入它。

//...

use Symfony\Component\Serializer\SerializerInterface;

//...

class whatever 
{
    private $serializer;

    public function __constructor(SerializerInterface $serialzer)
    {
        $this->serializer = $serializer;
    }

    public function exampleFunction()
    {
        //...
        $data = $this->serializer->serialize($demands, "json");
        //...
    }
}

0
投票

假设您有一个名为Foo.php的实体,它有idnamedescription

并且你想只返回idname消费特定的API,如foo/summary/在另一种情况下需要返回description以及foo/details

这里的序列化器非常有用。

use JMS\Serializer\Annotation as Serializer;

/*
* @Serializer\ExclusionPolicy("all")
*/
class Foo {
    /**
    * @Serializer\Groups({"summary", "details"})
    * @Serializer\Expose()
    */
    private $id;

    /**
    * @Serializer\Groups({"summary"})
    * @Serializer\Expose()
    */
    private $title;

    /**
    * @Serializer\Groups({"details"})
    * @Serializer\Expose()
    */
    private $description;

}

让我们使用序列化器来获取数据取决于组

class FooController {
    public function summary(Foo $foo, SerializerInterface $serialzer)
    {
        $context = SerializationContext::create()->setGroups('summary');
        $data = $serialzer->serialize($foo, json, $context);

        return new JsonResponse($data);
    }

    public function details(Foo $foo, SerializerInterface $serialzer)
    {
        $context = SerializationContext::create()->setGroups('details');
        $data = $serialzer->serialize($foo, json, $context);

        return new JsonResponse($data);
    }
}


0
投票

最后我发现使用Symfony序列化器的答案非常简单:

  • 第一步:使用以下命令安装symfony序列化程序:

作曲家需要symfony / serializer

  • 第二:使用serializerInterface:

.....//

use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;

// .....

.... //

 /**
     * @Route("/demand", name="demand")
     */
    public function index(SerializerInterface $serializer)
    {
        $demands = $this->getDoctrine()
            ->getRepository(Demand::class)
            ->findAll();

        if($demands){
            return new JsonResponse(
                $serializer->serialize($demands, 'json'),
                200,
                [],
                true
            );
        }else{
            return '["message":"ooooops"]';
        }

    }
    
    //......
    

并与它,我没有发现依赖关系或DateTime或其他问题的任何问题;)

© www.soinside.com 2019 - 2024. All rights reserved.