merged branch Seldaek/jsonresp (PR #6635)

This PR was submitted for the 2.1 branch but it was merged into the master branch instead (closes #6635).

Commits
-------

26aafff [HttpFoundation] Remove special handling of empty arrays, fixes #5506

Discussion
----------

[HttpFoundation] Remove special handling of empty arrays, fixes #5506

Bug fix: yes
Feature addition: no
Backwards compatibility break: tiny/no
Symfony2 tests pass: yes
Fixes the following tickets: #5506
License of the code: MIT

See the linked issue for details, IMO it should be removed because it's not consistently applied and just a WTF for people.

---------------------------------------------------------------------------

by vicb at 2013-01-09T11:11:06Z

👍, could you please update the changelog with the BC break ?

---------------------------------------------------------------------------

by Seldaek at 2013-01-09T11:16:44Z

If we really consider this as a BC break and document it as such then I guess it should be 2.2 only and not 2.1. @fabpot?

---------------------------------------------------------------------------

by vicb at 2013-01-09T11:28:16Z

IMO there is no such things as "tiny BC" but BCs are yes/no.

---------------------------------------------------------------------------

by fabpot at 2013-01-09T15:09:18Z

Let's do it in master only and add a note in the CHANGELOG about the BC break.
This commit is contained in:
Fabien Potencier 2013-01-11 08:01:56 +01:00
commit a0c26e0335

View File

@ -28,17 +28,20 @@ class JsonResponse extends Response
* @param integer $status The response status code
* @param array $headers An array of response headers
*/
public function __construct($data = array(), $status = 200, $headers = array())
public function __construct($data = null, $status = 200, $headers = array())
{
parent::__construct('', $status, $headers);
if (null === $data) {
$data = new \ArrayObject();
}
$this->setData($data);
}
/**
* {@inheritDoc}
*/
public static function create($data = array(), $status = 200, $headers = array())
public static function create($data = null, $status = 200, $headers = array())
{
return new static($data, $status, $headers);
}
@ -79,11 +82,6 @@ class JsonResponse extends Response
*/
public function setData($data = array())
{
// root should be JSON object, not array
if (is_array($data) && 0 === count($data)) {
$data = new \ArrayObject();
}
// 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);