diff --git a/src/Symfony/Component/HttpFoundation/JsonResponse.php b/src/Symfony/Component/HttpFoundation/JsonResponse.php index 31747ce81416b..e49c6b276216f 100644 --- a/src/Symfony/Component/HttpFoundation/JsonResponse.php +++ b/src/Symfony/Component/HttpFoundation/JsonResponse.php @@ -34,14 +34,12 @@ class JsonResponse extends Response protected $encodingOptions = self::DEFAULT_ENCODING_OPTIONS; /** - * Constructor. - * - * @param mixed $data The response data - * @param int $status The response status code - * @param array $headers An array of response headers - * @param bool $preEncoded If the data is already a JSON string + * @param mixed $data The response data + * @param int $status The response status code + * @param array $headers An array of response headers + * @param bool $json If the data is already a JSON string */ - public function __construct($data = null, $status = 200, $headers = array(), $preEncoded = false) + public function __construct($data = null, $status = 200, $headers = array(), $json = false) { parent::__construct('', $status, $headers); @@ -49,7 +47,7 @@ public function __construct($data = null, $status = 200, $headers = array(), $pr $data = new \ArrayObject(); } - $this->setData($data, $preEncoded); + $json ? $this->setJson($data) : $this->setData($data); } /** @@ -87,46 +85,57 @@ public function setCallback($callback = null) return $this->update(); } + /** + * Sets a raw string containing a JSON document to be sent. + * + * @param string $data + * + * @return JsonResponse + * + * @throws \InvalidArgumentException + */ + public function setJson($json) + { + $this->data = $json; + + return $this->update(); + } + /** * Sets the data to be sent as JSON. * * @param mixed $data - * @param bool $preEncoded If the data is already a JSON string * * @return JsonResponse * * @throws \InvalidArgumentException */ - public function setData($data = array(), $preEncoded = false) + public function setData($data = array()) { - if (!$preEncoded) { - if (defined('HHVM_VERSION')) { - // HHVM does not trigger any warnings and let exceptions - // thrown from a JsonSerializable object pass through. - // If only PHP did the same... + if (defined('HHVM_VERSION')) { + // HHVM does not trigger any warnings and let exceptions + // thrown from a JsonSerializable object pass through. + // If only PHP did the same... + $data = json_encode($data, $this->encodingOptions); + } else { + try { + // PHP 5.4 and up wrap exceptions thrown by JsonSerializable + // objects in a new exception that needs to be removed. + // Fortunately, PHP 5.5 and up do not trigger any warning anymore. $data = json_encode($data, $this->encodingOptions); - } else { - try { - // PHP 5.4 and up wrap exceptions thrown by JsonSerializable - // objects in a new exception that needs to be removed. - // Fortunately, PHP 5.5 and up do not trigger any warning anymore. - $data = json_encode($data, $this->encodingOptions); - } catch (\Exception $e) { - if ('Exception' === get_class($e) && 0 === strpos($e->getMessage(), 'Failed calling ')) { - throw $e->getPrevious() ?: $e; - } - throw $e; + } catch (\Exception $e) { + if ('Exception' === get_class($e) && 0 === strpos($e->getMessage(), 'Failed calling ')) { + throw $e->getPrevious() ?: $e; } - } - - if (JSON_ERROR_NONE !== json_last_error()) { - throw new \InvalidArgumentException(json_last_error_msg()); + throw $e; } } - $this->data = $data; + if (JSON_ERROR_NONE !== json_last_error()) { + throw new \InvalidArgumentException(json_last_error_msg()); + } - return $this->update(); + return $this->setJson($data); } /** diff --git a/src/Symfony/Component/HttpFoundation/Tests/JsonResponseTest.php b/src/Symfony/Component/HttpFoundation/Tests/JsonResponseTest.php index eac2f60fe7f8b..1dd2e60c062dd 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/JsonResponseTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/JsonResponseTest.php @@ -75,7 +75,7 @@ public function testConstructorWithCustomContentType() $this->assertSame('application/vnd.acme.blog-v1+json', $response->headers->get('Content-Type')); } - public function testConstructorWithPreEncoded() + public function testSetJson() { $response = new JsonResponse('1', 200, array(), true); $this->assertEquals('1', $response->getContent()); @@ -83,7 +83,8 @@ public function testConstructorWithPreEncoded() $response = new JsonResponse('[1]', 200, array(), true); $this->assertEquals('[1]', $response->getContent()); - $response = new JsonResponse('true', 200, array(), true); + $response = new JsonResponse(null, 200, array()); + $response->setJson('true'); $this->assertEquals('true', $response->getContent()); }