Skip to content

[HttpFoundation] Add ability to change JSON encoding options #9915

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 7, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Symfony/Component/HttpFoundation/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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