/**
  * Render the view into a string and return for output
  *
  * @param mixed $input
  * @return string
  * @throws \Exception
  */
 public function render($input = null)
 {
     $context = new SerializationContext();
     $context->setSerializeNull(true);
     $context->enableMaxDepthChecks();
     FrontController::getInstance()->getResponse()->headers->set('Content-Type', 'application/json');
     return $this->serializer->serialize($input, $this->format, $context);
 }
 /**
  * @Route("/monster/{id}", methods={"GET"}, requirements={"id"="^[0-9].*$"})
  * @param Monster $monster
  * @return Monster
  */
 public function getMonsterAction(Monster $monster)
 {
     $serializer = $this->get("jms_serializer");
     $context = new SerializationContext();
     $context->enableMaxDepthChecks();
     $response = new Response($serializer->serialize($monster, 'json', $context));
     $response->headers->set('Content-Type', 'application/json');
     return $response;
 }
 /**
  * @Route("/collection/category/{id}", methods={"GET"}, requirements={"id"="^[0-9].*$"})
  * @param CollectionCategory $collectionCategory
  * @return CollectionCategory
  */
 public function getCollectionCategoryAction(CollectionCategory $collectionCategory)
 {
     $serializer = $this->get("jms_serializer");
     $context = new SerializationContext();
     $context->enableMaxDepthChecks();
     $response = new Response($serializer->serialize($collectionCategory, 'json', $context));
     $response->headers->set('Content-Type', 'application/json');
     return $response;
 }
 /**
  * @param PaginationInterface $pagination
  * @param Request             $request
  * @param Router              $router
  * @param array               $serializationGroups
  *
  * @return mixed
  */
 public function buildMultiObjectResponse(PaginationInterface $pagination, Request $request, Router $router, array $serializationGroups = array())
 {
     if (empty($pagination->getItems())) {
         return $this->handle(View::create(null, Response::HTTP_NO_CONTENT));
     }
     $paginationData = $pagination->getPaginationData();
     $link = $this->buildPaginationLink($paginationData, $request, $router);
     $headers = [];
     if (!empty($link)) {
         $headers['Link'] = $link;
     }
     if (isset($paginationData['totalCount'])) {
         $headers['X-Total-Count'] = $paginationData['totalCount'];
     }
     $view = View::create($pagination->getItems(), Response::HTTP_OK, $headers);
     $context = new SerializationContext();
     $context->setSerializeNull(true);
     $context->enableMaxDepthChecks();
     if (!empty($serializationGroups)) {
         $context->setGroups($serializationGroups);
     }
     $view->setSerializationContext($context);
     return $this->handle($view);
 }
 /**
  * Create serialization context with settings taken from the controller's annotation
  *
  * @param MappingInterface $mapping
  * @return SerializationContext
  */
 protected function createSerializationContext(MappingInterface $mapping)
 {
     $context = new SerializationContext();
     if ($mapping->getAnnotation()->getSerializerEnableMaxDepthChecks()) {
         $context->enableMaxDepthChecks();
     }
     if ($mapping->getAnnotation()->getSerializerGroups()) {
         $context->setGroups($mapping->getAnnotation()->getSerializerGroups());
     }
     if ($mapping->getAnnotation()->getSerializerSerializeNull()) {
         $context->setSerializeNull(true);
     }
     return $context;
 }
 /**
  * Serializes an object.
  *
  * Implements FormatterInterface::formatItem($item)
  *
  * @param mixed $item
  *
  * @return array
  */
 public function formatItem($item, $format = 'json')
 {
     $formatted = null;
     switch ($format) {
         case 'json':
             // serialize properties with null values
             $context = new SerializationContext();
             $context->setSerializeNull(true);
             $context->enableMaxDepthChecks(true);
             $formatted = $this->getSerializer()->serialize($item, 'json', $context);
             break;
         case 'jsonp':
             $callback = $this->getRequest()->query->get('jsonp.callback', 'JSONP.callback');
             // validate against XSS
             $validator = new \JsonpCallbackValidator();
             if (!$validator->validate($callback)) {
                 throw new BadRequestHttpException('Invalid JSONP callback value');
             }
             $context = new SerializationContext();
             $context->setSerializeNull(true);
             $json = $this->getSerializer()->serialize($item, 'json');
             $formatted = sprintf('/**/%s(%s)', $callback, $json);
             break;
         default:
             // any other format is not supported
             throw new \InvalidArgumentException(sprintf('Format not supported: %s', $format));
     }
     return $formatted;
 }