comphp/config-json provides CommonPHP\Drivers\Config\JSON\JsonConfigurationDriver for JSON configuration files.
use CommonPHP\Drivers\Config\JSON\JsonConfigurationDriver;
$driver = new JsonConfigurationDriver();
$config = [
'name' => 'demo',
'database' => [
'host' => 'localhost',
],
];
$data = $driver->encode($config);
$decoded = $driver->decode($data);$driver->write(__DIR__ . '/config.json', $config);
$config = $driver->read(__DIR__ . '/config.json');Decoded JSON must produce a PHP array. Scalar-only JSON is not accepted as a configuration array, including during validation.
Failures throw CommonPHP config exceptions instead of returning false.