Skip to content

Internals Response Senders JsonSender

Stefan Crkvenjakov edited this page Oct 7, 2026 · 1 revision

response\senders\JsonSender

Framework Internals · Subsystem overview · Developer guide

Source: awt_src/classes/response/senders/JsonSender.php · Snapshot 8411f4287706.

Response builds status, headers and data/file payloads, chooses a MIME sender, and emits HTTP output. JSON arrays are wrapped in a status/message/data envelope.

JsonSender

Declaration and architectural role

class JsonSender extends AbstractSender

Constants, enum cases, traits and properties

No own constants, enum cases, traits or property declarations. Inherited members are documented on the parent reference page.

Method index

Method Signature
supportedMimes public function supportedMimes(): array
send public function send(mixed $payload, int $status, array $headers): void

supportedMimes

Source line 7

public function supportedMimes(): array

Return expressions (including local callbacks): ['application/json'].

Implementation, validation guards and interaction order
public function supportedMimes(): array
    {
        return ['application/json'];
    }

send

Source line 17

public function send(mixed $payload, int $status, array $headers): void

Parameters

Parameter Type, modifiers and default Purpose
$payload mixed $payload Operation input; the exact use, validation and propagation are shown in the implementation trace.
$status int $status Operation input; the exact use, validation and propagation are shown in the implementation trace.
$headers array $headers Operation input; the exact use, validation and propagation are shown in the implementation trace.

$payload may be:

  • string → treated as a file path to a .json file
  • array → encoded directly to JSON

Explicit exceptions: \RuntimeException. The guards in the implementation below show when each is raised; exceptions from collaborators also propagate unless caught.

Implementation, validation guards and interaction order
/**
     * $payload may be:
     *  - string  → treated as a file path to a .json file
     *  - array   → encoded directly to JSON
     */
    public function send(mixed $payload, int $status, array $headers): void
    {
        $this->emitStatus($status);
        $this->emitContentType('application/json');
        $this->emitHeaders($headers);

        if (is_string($payload)) {
            $raw = file_get_contents($payload);
            json_decode($raw);
            if (json_last_error() !== JSON_ERROR_NONE) {
                throw new \RuntimeException("File '$payload' is not valid JSON.");
            }
            echo $raw;
        } else {
            echo json_encode($payload, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
        }
    }

Related types

Clone this wiki locally