You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 updated
0 commit comments