Comparison of PHP json libraries

Today I start a project of building an Android Application that allows lenders to search for a certified and trained appraisers within their preferred area. To build a simple app, you can use REST to return data. You will need Json extension on the PHP server to have the task done. If you would like to know how to install JSON on your PHP server (only PHP version below 5.2), go to http://www.php.net/manual/en/json.installation.php. Most of the hosting servers nowadays have PHP version 5.2 that comes with json enabled by default, so you don’t have to install anything.

Feature Matrix

Feature PHP json PEAR json ZEND json XMLRPC json
Version 1.2.1 2006/03/31 1.31 2006/06/28 20061105-1470 CVS Id 1.25 2006/11/05
PHP version compatibility 4 (for windows the dll for php 4.3 and above is available on pecl4win), 5 4.0.6, 5.0 5.0 4.2, 5
PHP files used 0 1 6 2
Encodes to 7-bit ASCII clean JSON strings, using unicode escape sequences Yes Yes No Yes
Supports encoding php strings from different charsets Assumes UTF-8 Assumes UTF-8 Does not convert string data: it is up to the user to take care about conversion to the desired charset UTF-8, ISO-8859-1, ASCII
Supports decoding JSON data that has not an an array or object as top level element No Yes Yes Yes
Supports decoding JSON objects as either php arrays or objects Yes Yes Yes Yes
Supports distinguishing a failed decoding from decoding a single NULL value Yes (single NULL value cannot be decoded) No Yes (exception thrown) Yes
Supports decoding JSON to different charsets Only UTF-8 UTF-8, ISO-8859-1, ASCII
Supports features of Javascript outside the scope of JSON None: only data in accord with the Json spec is accepted Parses almost everything as valid (within an array) Some Tries to mimic the behaviour of common web browsers js engines;
keeps type information for float and integer php values (a float value of 1.0 will be encoded ad later decoded by the lib as float, even though in js only ‘numbers’ exists)
Extra features Provides a compatibility layer that implement the same API as the PHP native json extension;
adds support for the json-rpc webservice dialect

Sample codes

Goal PHP json PEAR json ZEND json XMLRPC json
Encode a php value to JSON string $out = json_encode($data); $value = new Services_JSON();

$out = $value->encode($data);

$out = Zend_Json::encode($data); $value = php_jsonrpc_encode($data);

$out = $value->serialize();

Decode a JSON string to PHP value (Json objects as php objects) $out = json_decode($data); $value = new Services_JSON();

$out = $value->decode($data);

try {
$out = Zend_Json::decode($data, Zend_Json::TYPE_OBJECT);
} catch (Exception $e) { }
$value = php_jsonrpc_decode_json($data);

if ($value) $out = php_jsonrpc_decode($value, array(‘decode_php_objs’));

Decode a JSON string to PHP value (Json objects as php arrays) $out = json_decode($data, true); $value = new Services_JSON (SERVICES_JSON_LOOSE_TYPE);

$out = $value->decode($data);

try { $out = Zend_Json::decode(); }

catch (Exception $e) { }

$value = php_jsonrpc_decode_json($data);

if ($value) $out = php_jsonrpc_decode($value);