php-json 1.2.1 review

Download
by rbytes.net on

php-json is an extremely fast PHP C extension for JSON (JavaScript Object Notation) serialisation

License: LGPL (GNU Lesser General Public License)
File size: 149K
Developer: Omar Kilani
0 stars award from rbytes.net

php-json is an extremely fast PHP C extension for JSON (JavaScript Object Notation) serialisation. php-json project library uses a forked version of json-c.

It can be used in conjunction with XMLHTTPRequest to exchange JavaScript-encoded data with a browser.

Usage:

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. Note: you need to compile php-json with gcc 3.x and up.

Then, just use json_encode to encode your PHP values into JSON, and json_decode to decode JSON into a PHP value.

For example:

$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.2345 }

While:

$input = '{ "abc": 12, "foo": "bar", "bool0": false, "bool1": true, "arr": [ 1, 2, 3, null, 5 ], "float": 1.2345 }';
$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"];
alert("result is " + result);

This should display an alert box with the value of result, i.e., 14.814.

What's New in This Release:
A complete rewrite using JSON_checker as the base for the parser.
Implements the JSON specification.
Significant performance improvements on encoding.
Re-licensed under the PHP license.

php-json 1.2.1 keywords