Skip to content

Commit 29371cd

Browse files
authored
docs(performance): document JSON streaming via JsonStreamer (#2298)
1 parent 0a842cb commit 29371cd

1 file changed

Lines changed: 79 additions & 0 deletions

File tree

core/performance.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,85 @@ api_platform:
538538

539539
More details are available on the [pagination documentation](pagination.md#partial-pagination).
540540

541+
## JSON Streaming
542+
543+
Available since API Platform 4.2.
544+
545+
By default, API Platform builds the whole serialized representation of a response in memory (using
546+
the Symfony Serializer) before sending it. For large payloads this is costly both in memory and in
547+
time to first byte.
548+
549+
API Platform can instead use the
550+
[Symfony JsonStreamer](https://symfony.com/doc/current/components/json_streamer.html) component to
551+
stream the response. JsonStreamer generates optimized, ahead-of-time-compiled encoders from the
552+
typed properties of your resources and writes the JSON directly to a `StreamedResponse`, without
553+
holding the full output in memory.
554+
555+
This is an opt-in optimization. JsonStreamer is an experimental Symfony component and requires
556+
**Symfony 7.4 or higher**.
557+
558+
### Installing the Component
559+
560+
```console
561+
composer require symfony/json-streamer
562+
```
563+
564+
Once installed, the streaming integration is enabled automatically (the `enable_json_streamer`
565+
configuration option defaults to `true` when the component is available). You can toggle it
566+
explicitly:
567+
568+
```yaml
569+
# api/config/packages/api_platform.yaml
570+
api_platform:
571+
enable_json_streamer: true
572+
```
573+
574+
### Enabling Streaming on a Resource or Operation
575+
576+
Streaming is opt-in per resource or per operation through the `jsonStream` attribute. Set it on the
577+
`#[ApiResource]` attribute to enable it for every operation:
578+
579+
```php
580+
<?php
581+
// api/src/ApiResource/Book.php
582+
namespace App\ApiResource;
583+
584+
use ApiPlatform\Metadata\ApiResource;
585+
586+
#[ApiResource(jsonStream: true)]
587+
class Book
588+
{
589+
public string $title;
590+
public string $author;
591+
592+
// ...
593+
}
594+
```
595+
596+
Or enable it for a single operation only:
597+
598+
```php
599+
<?php
600+
// api/src/ApiResource/Book.php
601+
namespace App\ApiResource;
602+
603+
use ApiPlatform\Metadata\ApiResource;
604+
use ApiPlatform\Metadata\GetCollection;
605+
606+
#[ApiResource]
607+
#[GetCollection(jsonStream: true)]
608+
class Book
609+
{
610+
// ...
611+
}
612+
```
613+
614+
Streaming applies to the `json` and `jsonld` formats. When a request asks for another format, or
615+
when `jsonStream` is not enabled, API Platform falls back to the regular Serializer-based response.
616+
617+
> **Note:** JsonStreamer relies on the typed properties of your resources to build its encoders.
618+
> Make sure every serialized property has a type declaration.
619+
541620
## Profiling with Blackfire.io
542621

543622
Blackfire.io allows you to monitor the performance of your applications. For more information, visit

0 commit comments

Comments
 (0)