= 0 || (version_compare(phpversion(), '4.4.5') >= 0 && version_compare(phpversion(), '5.0') < 0)) { $GLOBALS['json_extension_api_120_behaviour'] = false; } else { $GLOBALS['json_extension_api_120_behaviour'] = true; } if (!extension_loaded('json')) { /** * Takes a php value (array or object) and returns its json representation * @param mixed $value * @return string * * @bug if value is an ISO-8859-1 string with chars outside of ASCII, the php extension returns NULL, and we do not... */ function json_encode($value) { $jsval = php_jsonrpc_encode($value, array()); // make sure we emulate the std php behaviour: strings to be encoded are UTF-8!!! return $jsval->serialize(); } /** * Takes a json-formetted string and decodes it into php values * @param string $json * @param bool $assoc * @return mixed * * @todo add support for assoc=false */ function json_decode($json, $assoc=false) { $jsval = php_jsonrpc_decode_json($json); if (!$jsval) { return NULL; } else { // up to php version 5.2.1, json_decode only accepted structs and arrays as top-level elements if ($GLOBALS['json_extension_api_120_behaviour'] && ($jsval->mytype != 3 && $jsval->mytype != 2)) return NULL; $options = $assoc ? array() : array('decode_php_objs'); $val = php_jsonrpc_decode($jsval, $options); return $val; } } } else { // in case json extension is already loaded, register all funcions w. // different names, eg. json_alt_encode $content = file_get_contents(__FILE__); $content = str_replace(array("!extension_loaded('json')", 'function json_', ''), array('true', 'function json_alt_', '', ''), $content); eval($content); } ?>