/** * JSON 形式の文字列を値に変換します。 * * @param string $json * @param bool $returnAssoc * @throws RuntimeException * @return mixed */ public static function decodeJson($json, $returnAssoc = false) { if (function_exists('json_decode') && function_exists('json_last_error')) { $native = json_decode($json, $returnAssoc); $error = json_last_error(); if ($error !== JSON_ERROR_NONE) { $message = function_exists('json_last_error_msg') ? json_last_error_msg() : 'error code: ' . $error; throw new RuntimeException($message, $error); } return $native; } // オートローダーを働かせて定数を定義させる。 class_exists('Services_JSON'); $options = $returnAssoc ? SERVICES_JSON_LOOSE_TYPE : 0; $decoder = new Services_JSON($options); $native = $decoder->decode($json); if (Services_JSON::isError($json)) { throw new RuntimeException($native->toString(), $native->getCode()); } return $native; }
/** * array-walking function for use in generating JSON-formatted name-value pairs * * @param string $name name of key to use * @param mixed $value reference to an array element to be encoded * * @return string JSON-formatted name-value pair, like '"name":value' * @access private */ function name_value($name, $value) { $encoded_value = $this->encode($value); if (Services_JSON::isError($encoded_value)) { return $encoded_value; } return $this->encode(strval($name)) . ':' . $encoded_value; }
function name_value($name, $value) { $encoded_value = $this->_encode($value); if (Services_JSON::isError($encoded_value)) { return $encoded_value; } $lv = strval($name); // do not escape keyvalues if they are just text, and not keywords... if (preg_match('/^[a-z_]+$/i', $lv)) { if (!in_array($lv, $GLOBALS['Pman_Builder_Generator_JSON']['keywords'])) { return $lv . ' : ' . $encoded_value; } } return $this->_encode(strval($name)) . ' : ' . $encoded_value; }
function test_to_JSON() { $this->assertTrue(Services_JSON::isError($this->json->encode($this->res)), "resource case: {$this->res_d}"); $this->assertTrue(Services_JSON::isError($this->json->encode($this->arr)), "array case: {$this->arr_d}"); $this->assertTrue(Services_JSON::isError($this->json->encode($this->obj)), "object case: {$this->obj_d}"); }
/** * array-walking function for use in generating JSON-formatted name-value pairs * * @param string $name name of key to use * @param mixed $value reference to an array element to be encoded * * @return string JSON-formatted name-value pair, like '"name":value' * @access private */ static function name_value($name, $value) { $encoded_value = self::encode($value); if (Services_JSON::isError($encoded_value)) { return $encoded_value; } return self::encode(strval($name)) . ':' . $encoded_value; }
/** * Create a JSON-RPC client * * @param string $method A valid Mail API JSON-RPC method * @param string $args Parameters for your method * @return string | false If successful, a string is returned containing the JSON * data decoded into a PHP array. If a curl or JSON encode/decode * error occurs, the error is stored in $this->web_services_error. Note that * access to the HTTP status code (for further error checking) is not provided * in this method. */ function __call($method, $args) { $body = new stdclass(); $body->method = $method; $body->params = $args; // Use the JSON PHP extension if it's available if (function_exists('json_encode')) { // Anything json_encode can't encode is encoded as nulls $data = json_encode($body); } else { $json = new Services_JSON(); $data = $json->encode($body); if ($json->isError($data)) { $this->web_services_error = "Error encoding JSON"; return false; } } $ch = curl_init(self::jsonrpc_endpoint . "?appid={$this->appid}&WSSID={$this->WSSID}"); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Cookie: {$this->cookie}")); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $jsondata = curl_exec($ch); if (curl_errno($ch)) { $this->web_services_error = "Curl error number: " . curl_errno($ch); return false; } curl_close($ch); // Use the JSON PHP extension if it's available if (function_exists('json_encode')) { $data = json_decode($jsondata); if ($data == null) { $this->web_services_error = "Error decoding JSON"; return false; } } else { $data = $json->decode($jsondata); if ($json->isError($data)) { $this->web_services_error = "Error decoding JSON"; return false; } } return $data; }