Skip to content

Added docs for JsonResponse::fromJsonString #7204

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

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions components/http_foundation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -547,13 +547,22 @@ class, which can make this even easier::

use Symfony\Component\HttpFoundation\JsonResponse;

// if you know the data to send when creating the response
$response = new JsonResponse(array('data' => 123));

// if you don't know the data to send when creating the response
$response = new JsonResponse();
$response->setData(array(
'data' => 123
));
// ...
$response->setData(array('data' => 123));

// if the data to send is already encoded in JSON
$response = JsonResponse::fromJsonString('{ "data": 123 }');

.. versionadded:: 3.2
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wrong version

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well, if it is in 3.2, the wrong number is on the next line saying 3.3 (I haven't checked the code to know which version included it)

Copy link
Member

@xabbuh xabbuh Dec 10, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ``JsonResponse::fromJsonString()`` method was added in Symfony 3.2.

This encodes your array of data to JSON and sets the ``Content-Type`` header
to ``application/json``.
The ``JsonResponse`` class sets the ``Content-Type`` header to
``application/json`` and encodes your data to JSON when needed.

.. caution::

Expand Down