Json PECL extension provides, since 2006, JSON format support. This extension have been integrated in PHP 5.2.0.

This is absolutely no project to drop this support from PHP.

Documentation : Javascript Object Notation

Use example of  json_encode :

php -r 'var_dump(json_encode(array("foo","bar")));'
string(13) "["foo","bar"]"

Use example of json_decode :

php -r 'var_dump(json_decode("[\"foo\",\"bar\"]"));'
array(2) {
  [0] =>
  string(3) "foo"
  [1] =>
  string(3) "bar"

During a code License review, Debian project have discovered an issue with this extension and have reported it (Bug #63520). Effectively, code from json.org is not free, as its License includes a restriction to freedom 0 (run the program for any purpose) : "The Software shall be used for Good, not Evil". (No need to argue on this fact, this have been stated by FSF, and this is a reference for most of Linux distribution).

I have been able to quickly free the encoder in PHP 5.4.10 by dropping the non-free and unneeded code (Bug #63588).

For the parser, I've start to write a new PECL extension, jsonc, which use the free json-c library (MIT License). The code of the encoder still the same than the PHP 5.5 one. This new development also imply some contribution to the library:

  • PR51 : parser dynamic depth (in 0.11)
  • PR52 : float number parse must be local independent (in 0.11)
  • PR90 : integer must not start with zero  (merged)
  • PR94 : string must be single-quoted (pending)
  • PR94 : comments are not allowed
  • PR94 : trailing char are not allowed
  • etc

The jsonc extension is a drop-in alternative to the non-free json extension.

As the json-c library provides a incremental parser, I also add this new feature to the extension.

Use example of JsonIncrementalParser class:

$parser = new JsonIncrementalParser();
$fic = fopen("somefile.json", "r");
do {
    $buf = fgets($fic);
    $ret = $parser->parse($buf);
} while ($buf && ($ret==JsonIncrementalParser::JSON_PARSER_CONTINUE));
$result = $parser->get();

You can also parse a file, without having to load it into memory:

$ret = $parser->parseFile("somefile.json");
$result = $parser->get();

This parser have 2 strictness mode, strict mode, used by default, close to original php one, and a standard mode, less strict which allow comments or some bad constructed data / list. Using the new  JSON_PARSER_NOTSTRICT option allow, for example, to parse the following configuration file (which can be useful) :

Configuration file
    "temp": "/tmp", // directory
    "debug": true,  // boolean

Currently, with version 1.3.1 (beta), there's still some differences detected by the test suite. Those are fixed in the upcoming version 1.3.2 (stable) which is nearly ready (will require to use the bundled copy of json-c library, or a new / patched version).

The only remaining difference is the partial implementation of big integers parsing (and of JSON_BIGINT_AS_STRING option). This will only work in a 32bits version for value fitting in a 64bits integer (not managed by PHP, so returned as string or float). As no encoder will generate such data, I don't think this is a critical issue.

Performances are a bit behind (twice slower), but less than 1/10 of a second to parse a 130000 entries data seems is still acceptable (see bench results).

Linux Distributions

Debian have dropped the non free extension in PHP 5.5 / Jessie and provides the php5-json package (installed by default) build from the jsonc extension sources.

Fedora have dropped the non free extension in PHP 5.5 / Fedora 19 and provides the php-pecl-jsonc package (installed by default).

Mageia have replaced, in PHP 5.5, the non-free extension by the jsonc one (commit 442375).

Ubuntu have dropped the non free extension in PHP 5.5 / Saucy and provides the php5-json package.

If you have information about another distribution, thanks to tell me, so I will be able to edit this list.


Whatever the PHP installation method, JSON support will be available.