You are here

private function JsonResponse::jsonEncode in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/zendframework/zend-diactoros/src/Response/JsonResponse.php \Zend\Diactoros\Response\JsonResponse::jsonEncode()

Encode the provided data to JSON.

Parameters

mixed $data:

int $encodingOptions:

Return value

string

Throws

InvalidArgumentException if unable to encode the $data to JSON.

1 call to JsonResponse::jsonEncode()
JsonResponse::__construct in vendor/zendframework/zend-diactoros/src/Response/JsonResponse.php
Create a JSON response with the given data.

File

vendor/zendframework/zend-diactoros/src/Response/JsonResponse.php, line 62

Class

JsonResponse
JSON response.

Namespace

Zend\Diactoros\Response

Code

private function jsonEncode($data, $encodingOptions) {
  if (is_resource($data)) {
    throw new InvalidArgumentException('Cannot JSON encode resources');
  }

  // Clear json_last_error()
  json_encode(null);
  $json = json_encode($data, $encodingOptions);
  if (JSON_ERROR_NONE !== json_last_error()) {
    throw new InvalidArgumentException(sprintf('Unable to encode data to JSON in %s: %s', __CLASS__, json_last_error_msg()));
  }
  return $json;
}