Skip to content

Commit e9a3be0

Browse files
javiereguiluzxabbuh
authored andcommitted
Added docs for JsonResponse::fromJsonString
1 parent 7a8e74a commit e9a3be0

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

components/http_foundation.rst

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -547,13 +547,22 @@ class, which can make this even easier::
547547

548548
use Symfony\Component\HttpFoundation\JsonResponse;
549549

550+
// if you know the data to send when creating the response
551+
$response = new JsonResponse(array('data' => 123));
552+
553+
// if you don't know the data to send when creating the response
550554
$response = new JsonResponse();
551-
$response->setData(array(
552-
'data' => 123
553-
));
555+
// ...
556+
$response->setData(array('data' => 123));
557+
558+
// if the data to send is already encoded in JSON
559+
$response = JsonResponse::fromJsonString('{ "data": 123 }');
560+
561+
.. versionadded:: 3.2
562+
The ``JsonResponse::fromJsonString()`` method was added in Symfony 3.2.
554563

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

558567
.. caution::
559568

0 commit comments

Comments
 (0)