Metainformationen zur Seite

PHP Module loxberry_json.php

The module loxberry_json.php is a small helper class to work with json files (e.g. configuration files like general.json). The class manages opening and writing a json file, with change detection on write (only writes on change).


LoxBerry Compatibility

This module is available from LoxBerry V2.0.2. Set the minimum LoxBerry version in your plugin.cfg.

Inclusion

require_once "loxberry_json.php";

loxberry_json.php has no dependencies. It's not required to add a path to the PHP library's filename.

Abstract

require_once "loxberry_json.php";
 
// Open json file
$cfg = new LBJSON("/opt/loxberry/config/system/general.json");
 
// Query data from json object
echo "Send anonymous usage statistic: " . $cfg->Base->Sendstatistic . "\n";
 
// Change a value 
$cfg->Base->Sendstatistic = "off";
 
// Write all changes
$cfg->write();
 
// Query the current filename
echo "Filename is " . $cfg->filename();

The write method only writes if the data have been changed. Saving is skipped, if you have not changed any data.

Be careful, as the class is not concurrency resistant: If you open a json, and another process changes the json later, and you write back your file, changes of the other process will be overwritten. 

The saved json always is prettified, and slashes are not quoted.