8000 [json-response] Add a JsonResponse class for convenient JSON encoding · symfony/symfony@5fa1c70 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5fa1c70

Browse files
committed
[json-response] Add a JsonResponse class for convenient JSON encoding
Usage example: $data = array(user => $user->toArray()); return new JsonResponse($data);
1 parent 7b8acbc commit 5fa1c70

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpFoundation;
13+
14+
/**
15+
* Response represents an HTTP response in JSON format.
16+
*
17+
* @author Igor Wiedler <igor@wiedler.ch>
18+
*/
19+
class JsonResponse extends Response
20+
{
21+
/**
22+
* Constructor.
23+
*
24+
* @param mixed $data The response data
25+
* @param integer $status The response status code
26+
* @param array $headers An array of response headers
27+
*/
28+
public function __construct($data = array(), $status = 200, $headers = array())
29+
{
30+
// root should be JSON object, not array
31+
if (is_array($data) && 0 === count($data)) {
32+
$data = new \ArrayObject();
33+
}
34+
35+
parent::__construct(
36+
json_encode($data),
37+
$status,
38+
array_merge(array('Content-Type' => 'application/json'), $headers)
39+
);
40+
}
41+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Tests\Component\HttpFoundation;
13+
14+
use Symfony\Component\HttpFoundation\Request;
15+
use Symfony\Component\HttpFoundation\Response;
16+
use Symfony\Component\HttpFoundation\JsonResponse;
17+
18+
/**
19+
* @covers Symfony\Component\HttpFoundation\JsonResponse::__construct
20+
*/
21+
class JsonResponseTest extends \PHPUnit_Framework_TestCase
22+
{
23+
public function testConstructorEmptyCreatesJsonObject()
24+
{
25+
$response = new JsonResponse();
26+
$this->assertSame('{}', $response->getContent());
27+
}
28+
29+
public function testConstructorWithArrayCreatesJsonArray()
30+
{
31+
$response = new JsonResponse(array(0, 1, 2, 3));
32+
$this->assertSame('[0,1,2,3]', $response->getContent());
33+
}
34+
35+
public function testConstructorWithAssocArrayCreatesJsonObject()
36+
{
37+
$response = new JsonResponse(array('foo' => 'bar'));
38+
$this->assertSame('{"foo":"bar"}', $response->getContent());
39+
}
40+
41+
public function testConstructorWithSimpleTypes()
42+
{
43+
$response = new JsonResponse('foo');
44+
$this->assertSame('"foo"', $response->getContent());
45+
46+
$response = new JsonResponse(0);
47+
$this->assertSame('0', $response->getContent());
48+
49+
$response = new JsonResponse(0.1);
50+
$this->assertSame('0.1', $response->getContent());
51+
52+
$response = new JsonResponse(true);
53+
$this->assertSame('true', $response->getContent());
54+
}
55+
56+
public function testConstructorWithCustomStatus()
57+
{
58+
$response = new JsonResponse(array(), 202);
59+
$this->assertSame(202, $response->getStatusCode());
60+
}
61+
62+
public function testConstructorAddsContentTypeHeader()
63+
{
64+
$response = new JsonResponse();
65+
$this->assertSame('application/json', $response->headers->get('Content-Type'));
66+
}
67+
68+
public function testConstructorWithCustomHeaders()
69+
{
70+
$response = new JsonResponse(array(), 200, array('ETag' => 'foo'));
71+
$this->assertSame('application/json', $response->headers->get('Content-Type'));
72+
$this->assertSame('foo', $response->headers->get('ETag'));
73+
}
74+
75+
public function testConstructorWithCustomContentType()
76+
{
77+
$headers = array('Content-Type' => 'application/vnd.acme.blog-v1+json');
78+
79+
$response = new JsonResponse(array(), 200, $headers);
80+
$this->assertSame('application/vnd.acme.blog-v1+json', $response->headers->get('Content-Type'));
81+
}
82+
}

0 commit comments

Comments
 (0)
0