Skip to content

JsonStream

github-actions edited this page Mar 29, 2026 · 1 revision

Class JsonStream.

Provides a JSON-specific stream implementation, extending Nyholm's PSR-7 Stream. This class SHALL encapsulate a JSON-encoded payload within an in-memory PHP stream, while retaining the original decoded payload for convenient retrieval.

Implementations of this class MUST properly handle JSON encoding errors and SHALL explicitly prohibit the inclusion of resource types within the JSON payload.


Constants

Constant Visibility Type Value
ENCODING_OPTIONS public int \JSON_THROW_ON_ERROR | \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE

Methods

__construct

Constructs a new JsonStream instance with the provided payload.

public __construct(mixed $payload = [], int $encodingOptions = \self::ENCODING_OPTIONS): mixed

The payload SHALL be JSON-encoded and written to an in-memory stream. The original payload is retained in its decoded form for later access via getPayload().

Parameters:

Parameter Type Description
$payload mixed The data to encode as JSON. MUST be JSON-encodable. Resources are explicitly prohibited.
$encodingOptions int Optional JSON encoding flags. If omitted, ENCODING_OPTIONS will be applied.

getPayload

Retrieves the decoded payload associated with the stream.

public getPayload(): mixed

This method SHALL return the original JSON-encodable payload provided during construction or via withPayload().

Return Value:

the decoded payload


withPayload

Returns a new instance of the stream with the specified payload.

public withPayload(mixed $payload): self

This method MUST return a new JsonStream instance with the body replaced by a stream containing the JSON-encoded form of the new payload. The current instance SHALL remain unchanged.

Parameters:

Parameter Type Description
$payload mixed the new JSON-encodable payload

Return Value:

a new JsonStream instance containing the updated payload


Clone this wiki locally