/** * 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(json_last_error_message()); } return $encodedJson; }
/** * 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(json_last_error_message()); } return $decodedData; }
/** * Add data to the current request * * @param mixed $obj * @throws Exception * @return InsightlyRequest */ public function body($obj) { $data = json_encode($obj); $errno = json_last_error(); if ($errno != JSON_ERROR_NONE) { throw new Exception("Error encountered encoding JSON: " . json_last_error_message()); } curl_setopt($this->curl, CURLOPT_POSTFIELDS, $data); $this->headers[] = "Content-Type: application/json"; return $this; }