Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[HttpFoundation] Fix BC break introduced by #17642 #18006

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

Closed
wants to merge 2 commits into from
Closed
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
73 changes: 41 additions & 32 deletions src/Symfony/Component/HttpFoundation/JsonResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,20 @@ class JsonResponse extends Response
protected $encodingOptions = self::DEFAULT_ENCODING_OPTIONS;

/**
* Constructor.
*
* @param mixed $data The response data
* @param int $status The response status code
* @param array $headers An array of response headers
* @param bool $preEncoded If the data is already a JSON string
* @param mixed $data The response data
* @param int $status The response status code
* @param array $headers An array of response headers
* @param bool $json If the data is already a JSON string
*/
public function __construct($data = null, $status = 200, $headers = array(), $preEncoded = false)
public function __construct($data = null, $status = 200, $headers = array(), $json = false)
{
parent::__construct('', $status, $headers);

if (null === $data) {
$data = new \ArrayObject();
}

$this->setData($data, $preEncoded);
$json ? $this->setJson($data) : $this->setData($data);
}

/**
Expand Down Expand Up @@ -87,46 +85,57 @@ public function setCallback($callback = null)
return $this->update();
}

/**
* Sets a raw string containing a JSON document to be sent.
*
* @param string $data
*
* @return JsonResponse
*
* @throws \InvalidArgumentException
*/
public function setJson($json)
{
$this->data = $json;

return $this->update();
}

/**
* Sets the data to be sent as JSON.
*
* @param mixed $data
* @param bool $preEncoded If the data is already a JSON string
*
* @return JsonResponse
*
* @throws \InvalidArgumentException
*/
public function setData($data = array(), $preEncoded = false)
public function setData($data = array())
{
if (!$preEncoded) {
if (defined('HHVM_VERSION')) {
// HHVM does not trigger any warnings and let exceptions
// thrown from a JsonSerializable object pass through.
// If only PHP did the same...
if (defined('HHVM_VERSION')) {
// HHVM does not trigger any warnings and let exceptions
// thrown from a JsonSerializable object pass through.
// If only PHP did the same...
$data = json_encode($data, $this->encodingOptions);
} else {
try {
// PHP 5.4 and up wrap exceptions thrown by JsonSerializable
// objects in a new exception that needs to be removed.
// Fortunately, PHP 5.5 and up do not trigger any warning anymore.
$data = json_encode($data, $this->encodingOptions);
} else {
try {
// PHP 5.4 and up wrap exceptions thrown by JsonSerializable
// objects in a new exception that needs to be removed.
// Fortunately, PHP 5.5 and up do not trigger any warning anymore.
$data = json_encode($data, $this->encodingOptions);
} catch (\Exception $e) {
if ('Exception' === get_class($e) && 0 === strpos($e->getMessage(), 'Failed calling ')) {
throw $e->getPrevious() ?: $e;
}
throw $e;
} catch (\Exception $e) {
if ('Exception' === get_class($e) && 0 === strpos($e->getMessage(), 'Failed calling ')) {
throw $e->getPrevious() ?: $e;
}
}

if (JSON_ERROR_NONE !== json_last_error()) {
throw new \InvalidArgumentException(json_last_error_msg());
throw $e;
}
}

$this->data = $data;
if (JSON_ERROR_NONE !== json_last_error()) {
throw new \InvalidArgumentException(json_last_error_msg());
}

return $this->update();
return $this->setJson($data);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,16 @@ public function testConstructorWithCustomContentType()
$this->assertSame('application/vnd.acme.blog-v1+json', $response->headers->get('Content-Type'));
}

public function testConstructorWithPreEncoded()
public function testSetJson()
{
$response = new JsonResponse('1', 200, array(), true);
$this->assertEquals('1', $response->getContent());

$response = new JsonResponse('[1]', 200, array(), true);
$this->assertEquals('[1]', $response->getContent());

$response = new JsonResponse('true', 200, array(), true);
$response = new JsonResponse(null, 200, array());
$response->setJson('true');
$this->assertEquals('true', $response->getContent());
}

Expand Down