Commit f89a45f
committed
minor #14903 [HttpFoundation] JsonResponse content updated (MehGokalp)
This PR was submitted for the 5.2 branch but it was merged into the 4.4 branch instead.
Discussion
----------
[HttpFoundation] JsonResponse content updated
When using the `JsonResponse` with customized encoding options, the `JsonResponse` class can modify the given data.
Example:
```php
$response = new JsonResponse(['data' => (float) 3]);
echo $response->getContent(); // {"data": 3}
$response->setEncodingOptions(JsonResponse::DEFAULT_ENCODING_OPTIONS | \JSON_PRESERVE_ZERO_FRACTION);
echo $response->getContent(); // {"data": 3}
```
as you can see we gave float value and it returns integer. Because when we instantiate the `JsonResponse` it serializes the data with default encoding options. The proper way to do this;
```php
$response = new JsonResponse();
$response->setEncodingOptions(JsonResponse::DEFAULT_ENCODING_OPTIONS | \JSON_PRESERVE_ZERO_FRACTION);
$response->setData(['data' => (float) 3]);
echo $response->getContent(); // {"data": 3.0}
```
for details of the \JSON_PRESERVE_ZERO_FRACTION flag see [https://www.php.net/manual/en/function.json-encode.php](https://www.php.net/manual/en/function.json-encode.php)
Commits
-------
367efdb JsonResponse content updated1 file changed
+3
-1
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
668 | 668 | | |
669 | 669 | | |
670 | 670 | | |
671 | | - | |
| 671 | + | |
672 | 672 | | |
673 | 673 | | |
| 674 | + | |
| 675 | + | |
674 | 676 | | |
675 | 677 | | |
676 | 678 | | |
| |||
0 commit comments