JSON-PHP / january 2005

JSON-PHP is a PHP implementation of JSON, freely-available for download and use from pear.php.net. This project has a public Yahoo! group for bug reports, updates, rants, etc.

JSON (JavaScript Object Notation) is a lightweight data-interchange format. In experimenting with XMLHTTPRequest, I've found that using a less-bloaty data exchange format beats XML. It's also perfect for the growing body of Ajax code, examples of which can be found at Ajax Matters.

In Javascript, consuming input in JSON format is as easy as:

eval("var decoded_data = "+encoded_data);

With JSON-PHP, it can be almost as easy on the server-side, too:

// create a new instance of Services_JSON
require_once('JSON.php');
$json = new Services_JSON();

// convert a complex value to JSON notation
$value = array(1, 2, 'foo');
$output = $json->encode($value);
print($output);

// accept incoming POST data
$input = $GLOBALS['HTTP_RAW_POST_DATA'];
$value = $json->decode($input);

JSON is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999. This feature can also be found in Python. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, TCL, and many others. These properties make JSON an ideal data-interchange language.

JSON-PHP is available in fully-functional proposal form from the PEAR site, under the PHP license.

If you're comfortable with compiling PHP extensions from source, and have root privileges on the machines you use, there is a C implementation of JSON for PHP available, with big speed advantages over JSON-PHP.

more goodies, elsewhere:

elsewhere

I am also elsewhere on the web.

I bookmark at Pinboard, where I can be found at /u:migurski.

Other people's pictures I like go on migurski.tumblr.com. Before, they went on ffffound.com/migurski.

I'm medium-active on twitter/michalmigurski.