Versionen im Vergleich

Schlüssel

  • Diese Zeile wurde hinzugefügt.
  • Diese Zeile wurde entfernt.
  • Formatierung wurde geändert.

The class LoxBerry::JSON is intended to simplify working with json configuration files. It abstracts reading, writing and change recognition, and presents the content of the json file as a object variable. It supports simple key/value pairs, and also json objects and arrays. 

The advanced features allow to search for values in arrays and hashes.

Info
titleLoxBerry Compatibility

This object class is in development. We will announce the version number, as soon as this feature is available.

Inclusions

This library automatically includes:

  • use JSON;

Abstract

Codeblock
languageperl
linenumberstrue
use LoxBerry::JSON;
my $cfgfile = $lbpplugindir."/pluginconfig.json";

$jsonobj = LoxBerry::JSON->new();
$cfg = $jsonobj->open(filename => $cfgfile);

# Read values
print "Firstname: " . $cfg->{Firstname};
print "Sirname: " . $cfg->{Sirname};

# Change values
$cfg->{Firstname} = "Christian";
$cfg->{Sirname} = "Fenzl";

# Write 
$jsonobj->write();
# On every write, the class checks for changes and omits writing if nothing has changed

## Specials ##

# Enable debugging and dumping
$LoxBerry::JSON::DEBUG = 1; # Enables debug messages
$LoxBerry::JSON::DUMP = 1;  # Enables dumping of the current dataset

# Keep the file readonly in every case
$cfg = $jsonobj->open(filename => $cfgfile, readonly => 1);

# Write automatically on close
$cfg = $jsonobj->open(filename => $cfgfile, writeonclose => 1);

# Returns the filename
my $filename = $jsonobj->filename();

# Find
my @result = $jsonobj->find($cfg->{namesarray}, "$_->{Name} eq 'Christian'");
# See the detailed description for use-cases

# Dump
$jsonobj->dump{$cfg->{namesarray});

...