@@ -590,39 +590,59 @@ Streaming a JSON Response
590
590
591
591
.. versionadded :: 6.2
592
592
593
- The :class: `Symfony\\ Component\\ HttpFoundation\\ StreamedJsonResponse ` class allows
594
- an API to return a lot of data as JSON and keep the used resources low by make usage
595
- of Generators.
593
+ The :class: `Symfony\\ Component\\ HttpFoundation\\ StreamedJsonResponse ` class was
594
+ introduced in Symfony 6.2. It allows an API to return a lot of data as JSON and keep
595
+ the used resources low by make usage of Generators.
596
596
597
- It expects a JSON structure with one or multiple replacers identifiers, as example
598
- the `'__articles__' `. As a second argument it requires one or multiple Generators
599
- which items can be converted to a JSON via ``json_encode `` method. The key of the
600
- Generators requires to be the used replacer identifiers::
597
+ It expects an array which represents the JSON structure and the list which should be
598
+ streamed are represented in the array as ``\Generator ``. The response will stream this
599
+ JSON with generators in to most efficient way and keep resources as low as possible::
601
600
602
601
use Symfony\Component\HttpFoundation\StreamedJsonResponse;
603
602
603
+ function loadArticles(): \Generator { // any method or function returning a Generator
604
+ yield ['title' => 'Article 1'];
605
+ yield ['title' => 'Article 2'];
606
+ yield ['title' => 'Article 3'];
607
+ });
608
+
604
609
$response = new StreamedJsonResponse(
605
- // json structure with replace identifiers
610
+ // json structure with generators in which will be streamed as a list
606
611
[
607
612
'_embedded' => [
608
- 'articles' => '__articles__',
613
+ 'articles' => loadArticles(), // any \Generator can be used which will be streamed as list of data
609
614
],
610
615
],
611
- // array of generators with replace identifier used as key
612
- [
613
- '__articles__' => (function (): \Generator { // any method or function returning a Generator
614
- yield ['title' => 'Article 1'];
615
- yield ['title' => 'Article 2'];
616
- yield ['title' => 'Article 3'];
617
- })(),
618
- ]
619
616
);
620
617
621
618
.. tip ::
622
619
623
620
If loading data via doctrine the ``toIterable `` method of ``Doctrine `` can be
624
621
used to keep also the resources low and fetch only one row one by one.
625
- See the `Doctrine Batch processing `_ documentation for more.
622
+ See the `Doctrine Batch processing `_ documentation for more::
623
+
624
+ public function __invoke(): Response
625
+ {
626
+ return new StreamedJsonResponse(
627
+ [
628
+ '_embedded' => [
629
+ 'articles' => $this->loadArticles(),
630
+ ],
631
+ ],
632
+ );
633
+ }
634
+
635
+ public function loadArticles(): \Generator
636
+ {
637
+ $queryBuilder = $entityManager->createQueryBuilder();
638
+ $queryBuilder->from(Article::class, 'article');
639
+ $queryBuilder->select('article.id')
640
+ ->addSelect('article.title')
641
+ ->addSelect('article.description');
642
+
643
+ return $queryBuilder->getQuery()->toIterable();
644
+ }
645
+
626
646
627
647
Serving Files
628
648
~~~~~~~~~~~~~
0 commit comments