public function onKernelView(GetResponseForControllerResultEvent $event)
    {
        $request = $event->getRequest();
        $result = $event->getControllerResult();

        if (!$request->attributes->get('__hydra_serialize')) {
            return;
        }

        if (is_array($result) || ($result instanceof \ArrayAccess) || ($result instanceof \Traversable)) {
            $result = new Collection($request->getUri(), $result);
        } elseif (null === $result) {
            $event->setResponse(new JsonLdResponse('', 200));
            return;
        } elseif (!is_object($result)) {
            throw new \Exception("A Hydra controller must return either an array or an object, got a(n) " . gettype($result));
        }

        $serialized = $this->serializer->serialize($result, 'jsonld');

        $event->setResponse(new JsonLdResponse($serialized));
    }