Navigation Menu

Skip to content

Commit

Permalink
[HttpFoundation] Add ability to change JSON encoding options
Browse files Browse the repository at this point in the history
  • Loading branch information
stloyd committed Jan 7, 2014
1 parent 8850456 commit 89f4784
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 4 deletions.
6 changes: 6 additions & 0 deletions 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
-----

Expand Down
41 changes: 37 additions & 4 deletions src/Symfony/Component/HttpFoundation/JsonResponse.php
Expand Up @@ -26,6 +26,7 @@ class JsonResponse extends Response
{
protected $data;
protected $callback;
protected $encodingOptions;

/**
* Constructor.
Expand All @@ -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);
}

Expand Down Expand Up @@ -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
*
Expand All @@ -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());
Expand All @@ -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
*/
Expand Down
19 changes: 19 additions & 0 deletions src/Symfony/Component/HttpFoundation/Tests/JsonResponseTest.php
Expand Up @@ -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
*/
Expand Down

0 comments on commit 89f4784

Please sign in to comment.