diff --git a/src/Symfony/Component/HttpFoundation/CHANGELOG.md b/src/Symfony/Component/HttpFoundation/CHANGELOG.md index 061d47e7eec94..0da734bce2f1c 100644 --- a/src/Symfony/Component/HttpFoundation/CHANGELOG.md +++ b/src/Symfony/Component/HttpFoundation/CHANGELOG.md @@ -1,6 +1,12 @@ CHANGELOG ========= +2.5.0 +----- + + * added `JsonResponse::setEncodingOptions()` & `JsonResponse::getEncodingOptions()` for easier manipulation + of the options used while encoding data to JSON format. + 2.4.0 ----- diff --git a/src/Symfony/Component/HttpFoundation/JsonResponse.php b/src/Symfony/Component/HttpFoundation/JsonResponse.php index 6dfa75cd2ddc4..fb91536693a58 100644 --- a/src/Symfony/Component/HttpFoundation/JsonResponse.php +++ b/src/Symfony/Component/HttpFoundation/JsonResponse.php @@ -26,6 +26,7 @@ class JsonResponse extends Response { protected $data; protected $callback; + protected $encodingOptions; /** * Constructor. @@ -41,6 +42,10 @@ public function __construct($data = null, $status = 200, $headers = array()) if (null === $data) { $data = new \ArrayObject(); } + + // Encode <, >, ', &, and " for RFC4627-compliant JSON, which may also be embedded into HTML. + $this->encodingOptions = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT; + $this->setData($data); } @@ -80,7 +85,7 @@ public function setCallback($callback = null) } /** - * Sets the data to be sent as json. + * Sets the data to be sent as JSON. * * @param mixed $data * @@ -90,8 +95,7 @@ public function setCallback($callback = null) */ public function setData($data = array()) { - // Encode <, >, ', &, and " for RFC4627-compliant JSON, which may also be embedded into HTML. - $this->data = json_encode($data, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT); + $this->data = json_encode($data, $this->encodingOptions); if (JSON_ERROR_NONE !== json_last_error()) { throw new \InvalidArgumentException($this->transformJsonError()); @@ -101,7 +105,36 @@ public function setData($data = array()) } /** - * Updates the content and headers according to the json data and callback. + * Returns options used while encoding data to JSON. + * + * @return integer + */ + public function getEncodingOptions() + { + return $this->encodingOptions; + } + + /** + * Sets options used while encoding data to JSON. + * + * @param array $encodingOptions + * + * @return JsonResponse + */ + public function setEncodingOptions(array $encodingOptions) + { + $this->encodingOptions = 0; + foreach ($encodingOptions as $encodingOption) { + if (($this->encodingOptions & $encodingOption) != $encodingOption) { + $this->encodingOptions |= $encodingOption; + } + } + + return $this->setData(json_decode($this->data)); + } + + /** + * Updates the content and headers according to the JSON data and callback. * * @return JsonResponse */ diff --git a/src/Symfony/Component/HttpFoundation/Tests/JsonResponseTest.php b/src/Symfony/Component/HttpFoundation/Tests/JsonResponseTest.php index ef392ca59dbe6..c027f346417df 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/JsonResponseTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/JsonResponseTest.php @@ -166,6 +166,25 @@ public function testJsonEncodeFlags() $this->assertEquals('"\u003C\u003E\u0027\u0026\u0022"', $response->getContent()); } + public function testGetEncodingOptions() + { + $response = new JsonResponse(); + + $this->assertEquals(JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT, $response->getEncodingOptions()); + } + + public function testSetEncodingOptions() + { + $response = new JsonResponse(); + $response->setData(array(array(1, 2, 3))); + + $this->assertEquals('[[1,2,3]]', $response->getContent()); + + $response->setEncodingOptions(array(JSON_FORCE_OBJECT)); + + $this->assertEquals('{"0":{"0":1,"1":2,"2":3}}', $response->getContent()); + } + /** * @expectedException \InvalidArgumentException */
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: