Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Merge branch 'hotfix/simplify-json-casting'
Browse files Browse the repository at this point in the history
Close #64
Fixes #63
  • Loading branch information
weierophinney committed Jun 25, 2015
2 parents 150d25f + ea4a197 commit eeedadd
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 33 deletions.
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

All notable changes to this project will be documented in this file, in reverse chronological order by release.

## 1.1.1 - TBD
## 1.1.1 - 2015-06-25

### Added

Expand All @@ -18,7 +18,9 @@ All notable changes to this project will be documented in this file, in reverse

### Fixed

- Nothing.
- [#64](https://github.com/zendframework/zend-diactoros/pull/64) fixes the
behavior of `JsonResponse` with regards to serialization of `null` and scalar
values; the new behavior is to serialize them verbatim, without any casting.

## 1.1.0 - 2015-06-24

Expand Down
5 changes: 2 additions & 3 deletions doc/book/custom-responses.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,8 @@ the `Content-Type` header to `application/json`:
$response = new JsonResponse($data);
```

If a null value is provide, an empty JSON object is used for the content. Scalar data is cast to an
array before serialization. If providing an object, we recommend implementing
[JsonSerializable](http://php.net/JsonSerializable) to ensure your object is correctly serialized.
If providing an object, we recommend implementing [JsonSerializable](http://php.net/JsonSerializable)
to ensure your object is correctly serialized.

Just like the `HtmlResponse`, the `JsonResponse` allows passing two additional arguments — a
status code, and an array of headers — to allow you to further seed the initial state of the
Expand Down
23 changes: 5 additions & 18 deletions src/Response/JsonResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,23 @@

namespace Zend\Diactoros\Response;

use ArrayObject;
use InvalidArgumentException;
use Zend\Diactoros\Response;
use Zend\Diactoros\Stream;

/**
* HTML response.
* JSON response.
*
* Allows creating a response by passing an HTML string to the constructor;
* by default, sets a status code of 200 and sets the Content-Type header to
* text/html.
* Allows creating a response by passing data to the constructor; by default,
* serializes the data to JSON, sets a status code of 200 and sets the
* Content-Type header to application/json.
*/
class JsonResponse extends Response
{
use InjectContentTypeTrait;

/**
* Create a JSON response with the given array of data.
*
* If the data provided is null, an empty ArrayObject is used; if the data
* is scalar, it is cast to an array prior to serialization.
* Create a JSON response with the given data.
*
* Default JSON encoding is performed with the following options, which
* produces RFC4627-compliant JSON, capable of embedding into HTML.
Expand Down Expand Up @@ -69,15 +65,6 @@ private function jsonEncode($data, $encodingOptions)
throw new InvalidArgumentException('Cannot JSON encode resources');
}

if ($data === null) {
// Use an ArrayObject to force an empty JSON object.
$data = new ArrayObject();
}

if (is_scalar($data)) {
$data = (array) $data;
}

// Clear json_last_error()
json_encode(null);

Expand Down
14 changes: 4 additions & 10 deletions test/Response/JsonResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,10 @@ public function testConstructorAcceptsDataAndCreatesJsonEncodedMessageBody()
$this->assertSame($json, (string) $response->getBody());
}

public function testNullValuePassedToConstructorRendersEmptyJsonObjectInBody()
{
$response = new JsonResponse(null);
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals('application/json', $response->getHeaderLine('content-type'));
$this->assertSame('{}', (string) $response->getBody());
}

public function scalarValuesForJSON()
{
return [
'null' => [null],
'false' => [false],
'true' => [true],
'zero' => [0],
Expand All @@ -56,12 +49,13 @@ public function scalarValuesForJSON()
/**
* @dataProvider scalarValuesForJSON
*/
public function testScalarValuePassedToConstructorRendersValueWithinJSONArray($value)
public function testScalarValuePassedToConstructorJsonEncodesDirectly($value)
{
$response = new JsonResponse($value);
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals('application/json', $response->getHeaderLine('content-type'));
$this->assertSame(json_encode([$value], JSON_UNESCAPED_SLASHES), (string) $response->getBody());
// 15 is the default mask used by JsonResponse
$this->assertSame(json_encode($value, 15), (string) $response->getBody());
}

public function testCanProvideStatusCodeToConstructor()
Expand Down

0 comments on commit eeedadd

Please sign in to comment.