# Class HttpSoft\Response\JsonResponse

Class, that implements Psr\Http\Message\ResponseInterface and HttpSoft\Response\ResponseStatusCodeInterface.

Implementation of Psr\Http\Message\ResponseInterface for easy creation an instance from data structure to convert to JSON.

If the data structure is an object, it is recommended that the object implement the JsonSerializable interface for correct serialization.

Source code on GitHub.

use HttpSoft\Response\JsonResponse;

$response = new JsonResponse(['key' => 'value']);
$response->getStatusCode(); // 200
$response->getReasonPhrase(); // 'OK'
$response->getBody()->getContents(); // '{"key":"value"}'
$response->getBody()->getMetadata('uri') // 'php://temp'
$response->getHeaders(); // ['Content-Type' => ['application/json; charset=UTF-8']]
$response->getProtocolVersion(); // '1.1'

$response = new JsonResponse(['key' => 'value'], 404, ['Content-Type' => 'text/plain; charset=UTF-8'], '2');
$response->getStatusCode(); // 404
$response->getReasonPhrase(); // 'Not Found'
$response->getBody()->getContents(); // '{"key":"value"}'
$response->getBody()->getMetadata('uri') // 'php://temp'
$response->getHeaders(); // ['Content-Type' => ['text/plain; charset=UTF-8']]
$response->getProtocolVersion(); // '2'

$newResponse = $response->withHeader('Content-Language', 'en');
$newResponse->getHeaderLine('content-language'); // 'en'
$response->getHeaders(); // ['Content-Type' => ['text/plain; charset=UTF-8'], 'Content-Language' => ['en']]

$newResponse = $response->withStatus(JsonResponse::STATUS_INTERNAL_SERVER_ERROR);
$newResponse->getStatusCode(); // 500
$newResponse->getReasonPhrase(); // 'Internal Server Error'

$newResponse = $response->withStatus(599, 'Custom Phrase');
$newResponse->getStatusCode(); // 599
$newResponse->getReasonPhrase(); // 'Custom Phrase'

Specifying the encoding options:

use HttpSoft\Response\JsonResponse;

// By default (JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE):
$response = new JsonResponse('<p>HTML</p>', 200, [], '1.1', '', JsonResponse::DEFAULT_OPTIONS);
$response->getBody()->getContents(); // '"<p>HTML</p>"'
// equivalently to:
$response = new JsonResponse('<p>HTML</p>');
$response->getBody()->getContents(); // '"<p>HTML</p>"'
// or:
$newResponse = $response->withJsonData('<p>HTML</p>', JsonResponse::DEFAULT_OPTIONS);
$newResponse->getBody()->getContents(); // '"<p>HTML</p>"'
// equivalently to:
$newResponse = $response->withJsonData('<p>HTML</p>');
$newResponse->getBody()->getContents(); // '"<p>HTML</p>"'

// For HTML (JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_TAG | JSON_UNESCAPED_UNICODE):
$response = new JsonResponse('<p>HTML</p>', 200, [], '1.1', '', JsonResponse::HTML_OPTIONS);
$response->getBody()->getContents(); // '"\u003Cp\u003EHTML\u003C\/p\u003E"'
// or:
$newResponse = $response->withJsonData('<p>HTML</p>', JsonResponse::HTML_OPTIONS);
$newResponse->getBody()->getContents(); // '"\u003Cp\u003EHTML\u003C\/p\u003E"'

// Custom:
$response = new JsonResponse('<p>HTML</p>', 200, [], '1.1', '', JSON_THROW_ON_ERROR);
$newResponse->getBody()->getContents(); // '"<p>HTML<\/p>"'
// or:
$newResponse = $response->withJsonData('<p>HTML</p>', JSON_THROW_ON_ERROR);
$newResponse->getBody()->getContents(); // '"<p>HTML<\/p>"'

# Public methods

See the original detailed description of the methods in the Psr\Http\Message\ResponseInterface.

/**
 * @param mixed $data
 * @param int $code
 * @param array $headers
 * @param string $protocol
 * @param string $reasonPhrase
 * @param int $encodingOptions
 * @throws InvalidArgumentException If it is impossible to encode the data in JSON.
 */
public function __construct(
    $data,
    int $code = self::STATUS_OK,
    array $headers = [],
    string $protocol = '1.1',
    string $reasonPhrase = '',
    int $encodingOptions = self::DEFAULT_OPTIONS
);

If the Content-Type header is not specified, it will be set value to 'application/json; charset=UTF-8' by default.

If invalid specified data structure, the \InvalidArgumentException exception will be thrown.

# withJsonData

Returns a new HttpSoft\Response\JsonResponse instance with the specified data structure converted to JSON.

/**
 * @param mixed $data
 * @param int $encodingOptions
 * @return self
 */
public function withJsonData($data, int $encodingOptions = self::DEFAULT_OPTIONS): self;

If invalid specified data structure, the \InvalidArgumentException exception will be thrown.

use HttpSoft\Response\JsonResponse;

$response = new JsonResponse(['key' => 'value']);
$response->getBody()->getContents(); // '{"key":"value"}'

$newResponse = $response->withJsonData('<p>HTML</p>', JsonResponse::DEFAULT_OPTIONS);
$newResponse->getBody()->getContents(); // '"<p>HTML</p>"'
// equivalently to:
$newResponse = $response->withJsonData('<p>HTML</p>');
$newResponse->getBody()->getContents(); // '"<p>HTML</p>"'

$newResponse = $response->withJsonData('<p>HTML</p>', JsonResponse::HTML_OPTIONS);
$newResponse->getBody()->getContents(); // '"\u003Cp\u003EHTML\u003C\/p\u003E"'

$newResponse = $response->withJsonData('<p>HTML</p>', JSON_THROW_ON_ERROR);
$newResponse->getBody()->getContents(); // '"<p>HTML<\/p>"'

// If the specified data is an invalid UTF-8 sequence:
$response->withJsonData("\xB1\x31");  // throws InvalidArgumentException
$response->withJsonData("\xB1\x31", JSON_THROW_ON_ERROR);  // throws InvalidArgumentException
// If the specified data is an resource:
$response->withJsonData(fopen('php://memory', 'r'));  // throws InvalidArgumentException
$response->withJsonData(fopen('php://memory', 'r'), JSON_THROW_ON_ERROR);  // throws InvalidArgumentException

All other methods with descriptions are in the used traits: