omar kilani

home | projects

libical | misc | php-js | php-json | postfix_memcached


           

php-json

php-json is an extremely fast PHP C extension for JSON (JavaScript Object Notation) serialisation. It conforms to the JSON specification.

It is now part of PHP 5.2.0.

Download

Version 1.2.1 (SRPM, Win32) - Released 2006-04-01 - Rework comma insertion during encoding.

Mailing List

Google Groups
Subscribe to php-json
Email:
Visit this group

Documentation

A 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.

Performance

Following are some performance metrics for the php-json C extension (version 1.2.1) in comparison to a native PHP implementation of JSON (JSON.php,v 1.31 2006/06/28 05:54:17).

The C extension is 153 times faster than the native PHP implementation in this test on decoding.

More complex examples generally show the C extension in even better light, where a speed increase of 270 times is not uncommon.

Initial C decode:
object(stdClass)#2 (7) 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)
  ["unicode"]=>
  string(18) "プレスキット" refcount(1)
}

Timing 1000 decode iterations...
0.012725114822388 seconds elapsed

Initial C encode:
string(134) "{"abc":12,"foo":"bar","bool0":false,"bool1":true,"arr":[1,2,3,null,5],"float":1.2345,"unicode":"\u30d7\u30ec\u30b9\u30ad\u30c3\u30c8"}" refcount(1)

Timing 1000 encode iterations...
0.0062010288238525 seconds elapsed

Initial PHP decode:
object(stdClass)#2 (7) 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)
  ["unicode"]=>
  string(18) "プレスキット" refcount(1)
}

Timing 1000 decode iterations...
1.9429512023926 seconds elapsed

Initial PHP encode:
string(134) "{"abc":12,"foo":"bar","bool0":false,"bool1":true,"arr":[1,2,3,null,5],"float":1.2345,"unicode":"\u30d7\u30ec\u30b9\u30ad\u30c3\u30c8"}" refcount(1)

Timing 1000 encode iterations...
0.43110299110413 seconds elapsed

Decode: C is 152.68633953497 times faster
Encode: C is 69.521204198547 times faster