You are here

public function JsonDecode::decode in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/symfony/serializer/Encoder/JsonDecode.php \Symfony\Component\Serializer\Encoder\JsonDecode::decode()

Decodes data.

Parameters

string $data The encoded JSON string to decode:

string $format Must be set to JsonEncoder::FORMAT:

array $context An optional set of options for the JSON decoder; see below:

The $context array is a simple key=>value array, with the following supported keys:

json_decode_associative: boolean If true, returns the object as associative array. If false, returns the object as nested stdClass If not specified, this method will use the default set in JsonDecode::__construct

json_decode_recursion_depth: integer Specifies the maximum recursion depth If not specified, this method will use the default set in JsonDecode::__construct

json_decode_options: integer Specifies additional options as per documentation for json_decode. Only supported with PHP 5.4.0 and higher

Return value

mixed

Throws

UnexpectedValueException

Overrides DecoderInterface::decode

See also

http://php.net/json_decode json_decode

File

vendor/symfony/serializer/Encoder/JsonDecode.php, line 96

Class

JsonDecode
Decodes JSON data.

Namespace

Symfony\Component\Serializer\Encoder

Code

public function decode($data, $format, array $context = array()) {
  $context = $this
    ->resolveContext($context);
  $associative = $context['json_decode_associative'];
  $recursionDepth = $context['json_decode_recursion_depth'];
  $options = $context['json_decode_options'];
  if (PHP_VERSION_ID >= 50400) {
    $decodedData = json_decode($data, $associative, $recursionDepth, $options);
  }
  else {
    $decodedData = json_decode($data, $associative, $recursionDepth);
  }
  if (JSON_ERROR_NONE !== ($this->lastError = json_last_error())) {
    throw new UnexpectedValueException(JsonEncoder::getLastErrorMessage());
  }
  return $decodedData;
}