![]() home | projects libical | misc | php-js | php-json | postfix_memcached |
|||||
php-jsonphp-json is an extremely fast PHP C extension for JSON (JavaScript Object Notation) serialisation. php-json uses a forked version of json-c. DownloadVersion 1.1.1 (SRPM, Win32) - Released 2006-01-25 - TSRM performance improvements, changed license to version 3.01 of the PHP license, avoid a potential memory leak on invalid JSON input. Version 1.1.0 (SRPM, Win32: PHP 4.x, PHP 5.0.x, PHP 5.1.x) - Released 2005-12-04 - Port to Win32. Version 1.0.8 (SRPM) - Released 2005-12-01 - Changed license to LGPL, modified build system to allow static compilation into PHP, added strndup check for json-c. Version 1.0.7 - Released 2005-09-07 - Fixed issues with negative array keys (Thanks to Marek Lewczuk for the report,) modified json-c to return an error on unquoted object key names instead of going into an infinite loop. Version 1.0.6 - Released 2005-08-05 - Fixed issues with exporting private and protected class members (Thanks to Marek Lewczuk for the report.) Version 1.0.5 - Released 2005-06-16 - Changed spacing in json-c encoding, added optional assoc (boolean) parameter to json_decode to decode as associative array instead of object (Thanks to James Jones for the patch and Oscar F. Durón for the discussion), fixed issues with escaping /. Version 1.0.3 - Released 2005-06-15 - Fixed json-c string corruption issues under Mac OS X (thanks to Brett Stimmerman for the report) and FreeBSD (thanks to Robert S Wojciechowski for the report.) Version 1.0.2 - Released 2005-06-11 - Fixed issues with object reference counts under PHP4. Thanks to James Jones for the report. Version 1.0.1 - Released 2005-06-10 - Fixed non-linear and mixed type array index issues, fixed issues with escaping \\, forked json-c and added Unicode support. Version 1.0.0 - Released 2005-04-01 - Initial release. Mailing List
DocumentationA simple ./configure; make; make install should do the trick. Make sure to add an extension=json.so line to your php.ini/php.d. Then, just use json_encode to encode your PHP values into JSON, and json_decode to decode JSON into a PHP value. For example: $val = array("abc" => 12, "foo" => "bar", "bool0" => false, "bool1" => true, "arr" => array(1, 2, 3, null, 5), "float" => 1.2345 ); $output = json_encode($val); echo $output."\n"; Would produce: { "abc": 12, "foo": "bar", "bool0": false, "bool1": true, "arr": [ 1, 2, 3, null, 5 ], "float": 1.234500 } While: $input = '{ "abc": 12, "foo": "bar", "bool0": false, "bool1": true, "arr": [ 1, 2, 3, null, 5 ], "float": 1.234500 }'; $val = json_decode($input); echo $val->abc."\n"; Would produce: 12 As of version 1.0.5, json_decode takes an optional parameter, assoc (boolean), that returns an associative array instead of an object. A PHP object correlates to a JavaScript object (associative array, i.e., key => value pairs), so the above would be referenced in JavaScript like so: var obj = ...; /* retrieve JSON and eval() it, returning an object */ var result = obj["abc"] * obj["float"]; /* obj.abc would be the same as obj["abc"] */ alert("result is " + result); This should display an alert box with the value of result, i.e., 14.814. PerformanceFollowing are some performance metrics for the php-json C extension in comparison to a native PHP implementation of JSON. The C extension is 86 times faster than the native PHP implementation in this test. More complex examples generally show the C extension in even better light, where a speed increase of 270 times is not uncommon. Test string is: { "abc": 12, "foo": "bar", "bool0": false, "bool1": true, "arr": [ 1, 2, 3, null, 5 ], "float": 1.2345 } Initial C output is: object(stdClass)#1 (6) refcount(1){ ["abc"]=> long(12) refcount(1) ["foo"]=> string(3) "bar" refcount(1) ["bool0"]=> bool(false) refcount(1) ["bool1"]=> bool(true) refcount(1) ["arr"]=> array(5) refcount(1){ [0]=> long(1) refcount(1) [1]=> long(2) refcount(1) [2]=> long(3) refcount(1) [3]=> NULL refcount(1) [4]=> long(5) refcount(1) } ["float"]=> double(1.2345) refcount(1) } Timing 1000 iterations with C 0.027885913848877 seconds elapsed Initial PHP output is: object(ObjectFromJSON)#3 (6) refcount(2){ ["abc"]=> long(12) refcount(1) ["foo"]=> string(3) "bar" refcount(1) ["bool0"]=> bool(false) refcount(1) ["bool1"]=> bool(true) refcount(1) ["arr"]=> array(5) refcount(1){ [0]=> long(1) refcount(1) [1]=> long(2) refcount(1) [2]=> long(3) refcount(1) [3]=> NULL refcount(1) [4]=> long(5) refcount(1) } ["float"]=> double(1.2345) refcount(1) } Timing 1000 iterations with PHP 2.3223311901093 seconds elapsed |