function decode($str) { //$lib='native'; // $lib='services_JSON'; // $lib='jsonrpc'; // dirty trick to correct bad json for photos //$str = preg_replace('/\t} {/','}, {', $str); // remove trailing , //$str = preg_replace('/,[ \r\n\t]+}/',' }', $str); // echo "Using $lib<BR>"; // echo $str; if (function_exists('json_decode') && JSON_LIB_DECODE == 'native') { $arr = json_decode($str, true); } else { if (JSON_LIB_DECODE == 'services_JSON') { require_once dirname(__FILE__) . '/json.php'; $json = new Services_JSON(SERVICES_JSON_LOOSE_TYPE); $arr = $json->decode($str); } else { if (JSON_LIB_DECODE == 'jsonrpc') { require_once dirname(__FILE__) . '/jsonrpc/xmlrpc.php'; require_once dirname(__FILE__) . '/jsonrpc/jsonrpc.php'; $GLOBALS['xmlrpc_internalencoding'] = 'UTF-8'; //require_once dirname(__FILE__).'/jsonrpc/json_extension_api.php'; //$arr=json_decode($str, true); $value = php_jsonrpc_decode_json($str); if ($value) { $arr = php_jsonrpc_decode($value, array()); } } } } // print_r($arr); return $arr; }
/** * 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 || $jsval->mytype != 3 && $jsval->mytype != 2) { return NULL; } else { $options = $assoc ? array() : array('decode_php_objs'); $val = php_jsonrpc_decode($jsval, $options); return $val; } }
/** * 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; } }
/** * <MethodDescription> * * @param type <description> * * @return type <description> */ function dispatch($msg = NULL) { # ensure correct invocation if (is_null($msg) || !is_a($msg, 'jsonrpcmsg')) { return $this->throw_exception('functions_parameters_type must not be ' . 'phpvals.'); } # get decoded parameters $len = $msg->getNumParams(); $argument_array = array(); for ($i = 0; $i < $len; ++$i) { $argument_array[] = php_jsonrpc_decode($msg->getParam($i)); } # return result return new jsonrpcresp(php_jsonrpc_encode($this->invoke($msg->method(), $argument_array))); }
/** * Convert the json representation of a jsonrpc method call, jsonrpc method response * or single json value into the appropriate object (a.k.a. deserialize). * Please note that there is no way to distinguish the serialized representation * of a single json val of type object which has the 3 appropriate members from * the serialization of a method call or method response. * In such a case, the function will return a jsonrpcresp or jsonrpcmsg * @param string $json_val * @param array $options * @return mixed false on error, or an instance of jsonrpcval, jsonrpcresp or jsonrpcmsg * @access public * @todo add options controlling character set encodings */ function php_jsonrpc_decode_json($json_val, $options = array()) { $src_encoding = array_key_exists('src_encoding', $options) ? $options['src_encoding'] : $GLOBALS['xmlrpc_defencoding']; $dest_encoding = array_key_exists('dest_encoding', $options) ? $options['dest_encoding'] : $GLOBALS['xmlrpc_internalencoding']; // echo "#$json_val*"; //$GLOBALS['_xh'] = array(); $GLOBALS['_xh']['isf'] = 0; if (!json_parse($json_val, false, $src_encoding, $dest_encoding)) { error_log($GLOBALS['_xh']['isf_reason']); return false; } else { $val = $GLOBALS['_xh']['value']; // shortcut if ($GLOBALS['_xh']['value']->kindOf() == 'struct') { if ($GLOBALS['_xh']['value']->structSize() == 3) { if ($GLOBALS['_xh']['value']->structMemExists('method') && $GLOBALS['_xh']['value']->structMemExists('params') && $GLOBALS['_xh']['value']->structMemExists('id')) { /// @todo we do not check for correct type of 'method', 'params' struct members... $method = $GLOBALS['_xh']['value']->structMem('method'); $msg =& new jsonrpcmsg($method->scalarval(), null, php_jsonrpc_decode($GLOBALS['_xh']['value']->structMem('id'))); $params = $GLOBALS['_xh']['value']->structMem('params'); for ($i = 0; $i < $params->arraySize(); ++$i) { $msg->addparam($params->arrayMem($i)); } return $msg; } else { if ($GLOBALS['_xh']['value']->structMemExists('result') && $GLOBALS['_xh']['value']->structMemExists('error') && $GLOBALS['_xh']['value']->structMemExists('id')) { $id = php_jsonrpc_decode($GLOBALS['_xh']['value']->structMem('id')); $err = php_jsonrpc_decode($GLOBALS['_xh']['value']->structMem('error')); if ($err == null) { $resp = new jsonrpcresp($GLOBALS['_xh']['value']->structMem('result')); } else { if (is_array($err) && array_key_exists('faultCode', $err) && array_key_exists('faultString', $err)) { if ($err['faultCode'] == 0) { // FAULT returned, errno needs to reflect that $err['faultCode'] = -1; } } else { $err = array('faultCode' => -1, 'faultString' => serialize_jsonrpcval($GLOBALS['_xh']['value']->structMem('error'))); } $resp =& new jsonrpcresp(0, $err['faultCode'], $err['faultString']); } $resp->id = $id; return $resp; } } } } // not a request msg nor a response: a plain jsonrpcval obj return $GLOBALS['_xh']['value']; } }
/** * Takes a json value in PHP jsonrpcval object format * and translates it into native PHP types. * * @param jsonrpcval $jsonrpc_val * @param array $options if 'decode_php_objs' is set in the options array, jsonrpc objects can be decoded into php objects * @return mixed * @access public */ function php_jsonrpc_decode($jsonrpc_val, $options = array()) { $kind = $jsonrpc_val->kindOf(); if ($kind == 'scalar') { return $jsonrpc_val->scalarval(); } elseif ($kind == 'array') { $size = $jsonrpc_val->arraysize(); $arr = array(); for ($i = 0; $i < $size; $i++) { $arr[] = php_jsonrpc_decode($jsonrpc_val->arraymem($i), $options); } return $arr; } elseif ($kind == 'struct') { $jsonrpc_val->structreset(); // If user said so, try to rebuild php objects for specific struct vals. /// @todo should we raise a warning for class not found? // shall we check for proper subclass of xmlrpcval instead of // presence of _php_class to detect what we can do? if (in_array('decode_php_objs', $options)) { if ($jsonrpc_val->_php_class != '' && class_exists($jsonrpc_val->_php_class)) { $obj = @new $jsonrpc_val->_php_class(); } else { $obj = new stdClass(); } while (list($key, $value) = $jsonrpc_val->structeach()) { $obj->{$key} = php_jsonrpc_decode($value, $options); } return $obj; } else { $arr = array(); while (list($key, $value) = $jsonrpc_val->structeach()) { $arr[$key] = php_jsonrpc_decode($value, $options); } return $arr; } } }