Пример #1
0
 /**
  * Encodes PHP data to a JSON string.
  *
  * {@inheritdoc}
  */
 public function encode($data, $format, array $context = array())
 {
     $context = $this->resolveContext($context);
     $encodedJson = json_encode($data, $context['json_encode_options']);
     if (JSON_ERROR_NONE !== ($this->lastError = json_last_error())) {
         throw new UnexpectedValueException(JsonEncoder::getLastErrorMessage());
     }
     return $encodedJson;
 }
Пример #2
0
 /**
  * Decodes data.
  *
  * @param string $data    The encoded JSON string to decode
  * @param string $format  Must be set to JsonEncoder::FORMAT
  * @param 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 mixed
  *
  * @throws UnexpectedValueException
  *
  * @see http://php.net/json_decode json_decode
  */
 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'];
     $decodedData = json_decode($data, $associative, $recursionDepth, $options);
     if (JSON_ERROR_NONE !== ($this->lastError = json_last_error())) {
         throw new UnexpectedValueException(JsonEncoder::getLastErrorMessage());
     }
     return $decodedData;
 }