Skip to content

[HttpFoundation] JsonResponse content updated #14903

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

Merged
merged 1 commit into from
Apr 7, 2021
Merged

[HttpFoundation] JsonResponse content updated #14903

merged 1 commit into from
Apr 7, 2021

Conversation

MehGokalp
Copy link
Contributor

When using the JsonResponse with customized encoding options, the JsonResponse class can modify the given data.
Example:

$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;

$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

@carsonbot carsonbot changed the title JsonResponse content updated [HttpFoundation] JsonResponse content updated Feb 1, 2021
@OskarStark OskarStark added this to the 4.4 milestone Feb 1, 2021
@wouterj wouterj changed the base branch from 5.2 to 4.4 April 7, 2021 15:44
@wouterj wouterj requested a review from xabbuh as a code owner April 7, 2021 15:44
@wouterj
Copy link
Member

wouterj commented Apr 7, 2021

Thank you @MehGokalp! I've slightly modified the comments (to make them a bit shorter) and merged your contribution in 4.4. We'll take care of bringing it to the newer versions from there.

@wouterj wouterj merged commit f89a45f into symfony:4.4 Apr 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants