Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[HttpFoundation] Add ability to change JSON encoding options

  • Loading branch information...
commit 89f47841fe00cc0d59bb1ca932d9d5edaaa6fc92 1 parent 8850456
@stloyd stloyd authored
View
6 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
-----
View
41 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
*/
View
19 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
*/
Please sign in to comment.
Something went wrong with that request. Please try again.