Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: write

...

Code Block
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 "Surname: " . $cfg->{Surname};

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

# Write 
$jsonobj->write();
# On every write, the class checks for changes and omits writing if nothing has changed
$saved = $jsonobj->write();
# From LB2.0.2: write() returns 1 if content actually was changed/written, or undef if unchanged.

## Specials ##

# Parse an already existing json string (available from LB 1.4.1)
my $jsonstring = ' { "Firstname": "christian", "Surname": "fenzl" } ';
$cfg = $jsonobj->parse($jsonstring);

# Return a flat key=value hash from a hierarchical data structure (available from LB 2.0)
my $flat_hashref = $jsonobj->flatten();
my $flat_hashref = $jsonobj->flatten($prefix);

# Use $jsonobj in HTML::Template associate (available from LB 2.0)
my $template = HTML::Template->new(
    filename  => 'json_param.tmpl',
    associate => $jsonobj,
);

# Encode current data hash to json string, optional pretty printing (available from LB 2.0)
my $jsonstr = $jsonobj->encode();
my $jsonstr = $jsonobj->encode( pretty => 1 );

# Generate ready and escaped JavaScript code from current data (available from LB 2.0)
print $jsonobj->jsblock('jsvariablename');
# Returns: cfg = JSON.parse('<your data as json correctly escaped>');

# 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();
# Changes the filename (available from LB 1.4.1)
$jsonobj->filename('/tmp/newfilename.json');

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

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

...

Code Block
languagexml
linenumberstrue
{
   "Colors" : [
      "red",
      "blue",
      "green"
   ],
   "Info" : "Write data to JSON",
   "MINISERVERS" : {
      "1" : {
         "Name" : "MSOG"
      },
      "2" : {
         "Name" : "MSUG"
      }
   },
   "Server" : {
      "ip" : "192.168.0.1",
      "port" : "8000",
      "protocol" : "tcp"
   },
   "Version" : 1
}

write()

The write method will check if the content has changed, and only really writes the new content on changes.

write never writes a file:

  • if it has not changed,
  • if readonly => 1 is set

 From LB 2.0.2+: write returns 1, if the file was written, and undef if nothing has changed. Before LB2.0.2,write always returns undef.

Search in JSON

The module implements a find function to search for elements in hashes and arrays.

...