testvals = array( true, false, 0, 1, 1.0, // php native encoding strips the .0 1.1, '', null, '1', '20060101T12:00:00', utf8_encode('Günter, Elène'), 'Günter, Elène', // KO: we encode it somehow, PHP stops on the first string $v1, base64_encode('hello'), // string new jsonrpcmsg('dummy'), new xmlrpcval(), array(), array(utf8_encode('Günter, Elène')), array('a'), array(array(1)), array('2' => true, false), // array - when decoded array keys will be reset array('hello' => 'world'), // struct array('hello' => true, 'world'), // mixed array('hello' => true, 'hello', 'world'), // mixed - encode KO (2 memebers with null name) but decode will be fine!!! array('methodname' => 'hello', 'params' => array()), // struct array('faultCode' => 666, 'faultString' => 'hello world'), array('faultCode' => 666, 'faultString' => 'hello world', 'faultWhat?' => 'dunno'), array('faultCode' => 666, 'faultString' => array('hello world')), array('faultCode' => 666, 'faultString' => array('hello' => 'world')) ); if (class_exists('Services_JSON')) $this->pear_json = new Services_JSON(SERVICES_JSON_LOOSE_TYPE); } function valdiff($val, $ref) { //$out = var_export($val, true); ///@todo better testing of equality for objects if (($ref === null) || $val !== $ref) { echo '';var_dump($val);/*.htmlspecialchars($out).*/ echo ''; } else //echo htmlspecialchars($out); var_dump($val); } function testDecoding() { echo "

String decoding tests

\n\n"; foreach($this->teststrings as $i => $test) { echo "\n"; } echo "
 ValueDecodedJS dec.Native dec.Pear dec.Zend dec.
$i"; var_dump($test); echo ""; $ok = json_parse($test, true); if ($ok) { var_dump($GLOBALS['_xh']['value']); } else { $GLOBALS['_xh']['value'] = null; echo "NULL
'"; echo htmlspecialchars($GLOBALS['_xh']['isf_reason'])."'"; } echo "
"; $ok = $GLOBALS['_xh']['value']; ?> "; if (extension_loaded('json')) { $okk = json_decode($test); $this->valdiff($okk, $ok); } echo ""; if ($this->pear_json) { $okkk = $this->pear_json->decode($test); //if (Services_JSON::isError($okkk)) //{ // $okkk = NULL; //} $this->valdiff($okkk, $ok); } echo ""; if (class_exists('Zend_Json')) { try { $msg = ''; $okkkk = Zend_Json::decode($test); } catch (Exception $e) { $okkkk = NULL; $msg = "
\n'".$e->getMessage()."'"; } //if (Services_JSON::isError($okkk)) //{ // $okkk = NULL; //} $this->valdiff($okkkk, $ok); echo $msg; } echo "
\n"; } function testEncoding() { echo "

Values encoding tests

\n\n"; foreach($this->testvals as $i => $test) { echo "\n"; } echo "
 ValueEncodedNative enc.Pear enc.Zend enc.
$i"; var_dump($test); echo ""; $jsval = php_jsonrpc_encode($test, array()); $jsval = $jsval->serialize(); //if ($ok) //{ var_dump($jsval); //} //else //{ // $GLOBALS['_xh']['value'] = null; // echo "NULL
'"; echo htmlspecialchars($GLOBALS['_xh']['isf_reason'])."'"; //} //echo "
"; ?> "; if (extension_loaded('json')) { $okk = json_encode($test); $this->valdiff($okk, $jsval); } echo ""; if ($this->pear_json) { $okkk = $this->pear_json->encode($test); if (Services_JSON::isError($okkk)) { $okkk = NULL; } $this->valdiff($okkk, $jsval); } echo ""; if (class_exists('Zend_Json')) { try { $okkkk = Zend_Json::encode($test); } catch (Exception $e) { $okkkk = NULL; } //if (Services_JSON::isError($okkk)) //{ // $okkk = NULL; //} $this->valdiff($okkkk, $jsval); } echo "
\n"; } function testExtensionAPI() { echo "

Extension API emulation tests

\n\n\n"; foreach($this->testvals as $i => $val) { echo "\n"; } echo "
OriginalNative EncodedEncodedNative DecodedDecodedNative Dec. as objDec. as obj
$i"; var_dump($val); echo ""; if (extension_loaded('json')) { $j1 = json_encode($val); var_dump($j1); echo ""; $j2 = json_alt_encode($val); if ($j1 !== $j2) { echo ''; var_dump($j2); echo "\n"; } else var_dump($j2); } else { echo ""; $j2 = json_encode($val); var_dump($j2); } echo ""; if (extension_loaded('json')) { $v1 = json_decode($j1, true); var_dump($v1); echo ""; $v2 = json_alt_decode($j2, true); if ($v1 !== $v2) { echo ''; var_dump($v2); echo "\n"; } else var_dump($v2); } else { echo ""; $v2 = json_decode($j2, true); var_dump($v2); } echo ""; if (extension_loaded('json')) { $v3 = json_decode($j1, false); if ($v1 !== $v3) { echo ''; var_dump($v3); echo "\n"; } else var_dump($v3); echo ""; $v4 = json_alt_decode($j2, false); if ($v3 !== $v4) { //$out = var_export($v4, true); echo ''; var_dump($v4); echo "\n"; } else var_dump($v4); } else { echo ""; $v4 = json_alt_decode($j2, false); var_dump($v4); } /*$v1 = json_decode('['.$j1.']', true); $v2 = json_alt_decode('['.$j2.']', true); echo ""; echo 'Encoded as array: ';var_dump($v1); echo ""; if ($v1 !== $v2) { echo ''; var_dump($v2); echo "\n"; } $v1 = json_decode($val, true); $v2 = json_alt_decode($val, true); echo ""; echo 'Decoded val: ';var_dump($v1); echo ""; if ($v1 !== $v2) { echo ''; var_dump($v2); echo "\n"; }*/ echo "
\n"; } } $test = new apitests(); $test->testDecoding(); $test->testEncoding(); $test->testExtensionAPI(); ?>