Skip to content

Commit 193968c

Browse files
committed
feature symfony#7204 Added docs for JsonResponse::fromJsonString (javiereguiluz)
This PR was squashed before being merged into the 3.2 branch (closes symfony#7204). Discussion ---------- Added docs for JsonResponse::fromJsonString This fixes symfony#7174. Commits ------- e9a3be0 Added docs for JsonResponse::fromJsonString
2 parents a9683eb + e9a3be0 commit 193968c

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

components/http_foundation.rst

+14-5
Original file line numberDiff line numberDiff line change
@@ -548,13 +548,22 @@ class, which can make this even easier::
548548

549549
use Symfony\Component\HttpFoundation\JsonResponse;
550550

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

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

559568
.. caution::
560569

0 commit comments

Comments
 (0)