@@ -590,39 +590,59 @@ Streaming a JSON Response
590590
591591..versionadded ::6.2
592592
593- The:class: `Symfony\\ Component\\ HttpFoundation\\ StreamedJsonResponse ` classallows
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 ` classwas
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.
596596
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::
601600
602601 use Symfony\Component\HttpFoundation\StreamedJsonResponse;
603602
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+
604609 $response = new StreamedJsonResponse(
605- // json structure withreplace identifiers
610+ // json structure withgenerators in which will be streamed as a list
606611 [
607612 '_embedded' => [
608- 'articles' =>'__articles__',
613+ 'articles' =>loadArticles(), // any \Generator can be used which will be streamed as list of data
609614 ],
610615 ],
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- ]
619616 );
620617
621618..tip ::
622619
623620 If loading data via doctrine the ``toIterable `` method of ``Doctrine `` can be
624621 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+
626646
627647Serving Files
628648~~~~~~~~~~~~~