Documentation

Json helper

Table of Contents 

Presentation

This helper is used to read JSON files containing comments (which is not supported by the JSON standard).

It offers two static methods:

  • decode(), which takes the same parameters as the json_decode() function.
  • decodeFile(), which takes as parameter a path to a JSON file. The file is read and the JSON objects are converted into associative arrays.

Usage

use \Temma\Utils\Json as TµJson;

$json = <<< EOT
/*
 * List of users
 */
[
    // admnistrator
    {
        "name": "Alice",
        "role": "admin"
    },
    // manager
    {
        "name": "Bob",
        "role": "manager"
    }
]
EOT;
$data = TµJson::decode($json, true);
/*
[
    [
        'name' => 'Alice',
        'role' => 'admin',
    ],
    [
        'name' => 'Bob',
        'role' => 'manager',
    ],
]
*/
use \Temma\Utils\Json as TµJson;

$data = TµJson::decodeFile('/path/to/file.json');
Previous: IniExport helper
Next: Lock helper

Table of Contents