Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. JsonResponse.php

function JsonResponse::setData

Sets the data to be sent as JSON.

Return value

$this

Throws

\InvalidArgumentException

2 calls to JsonResponse::setData()
JsonResponse::setEncodingOptions in vendor/symfony/http-foundation/JsonResponse.php
Sets options used while encoding data to JSON.
JsonResponse::__construct in vendor/symfony/http-foundation/JsonResponse.php

File

vendor/symfony/http-foundation/JsonResponse.php, line 123

Class

JsonResponse
Response represents an HTTP response in JSON format.

Namespace

Symfony\Component\HttpFoundation

Code

public function setData(mixed $data = []) : static {
    try {
        $data = json_encode($data, $this->encodingOptions);
    } catch (\Exception $e) {
        if ('Exception' === $e::class && str_starts_with($e->getMessage(), 'Failed calling ')) {
            throw $e->getPrevious() ?: $e;
        }
        throw $e;
    }
    if (\JSON_THROW_ON_ERROR & $this->encodingOptions) {
        return $this->setJson($data);
    }
    if (\JSON_ERROR_NONE !== json_last_error()) {
        throw new \InvalidArgumentException(json_last_error_msg());
    }
    return $this->setJson($data);
}
RSS feed
Powered by Drupal