Loading and Saving Variables

Prev Next

Introduction

The following functions provide convenient methods to save variables (including all the structure and array members and sub-members) into files or $ text strings (strings) and vice versa in JavaScriopt Object Notation (JSON). Focus is given on full data preservation in round trip cases, i.e. data is saved as JSON and covered again, and vice versa.

Following rules will be applied when saving variables into JSON text. These rules are insofar important because the B4P can consist of both base variables, array and structure members and sub-members, and the simple JSON representation focuses on leaf data, meaning the final members and sub-members without the intermediate one. In addition, special rules are applied to save B4P specific formats such as dates (and times) and sets. Following special rules apply

General The base variable as well as all members and sub-members, regardless if array or structures, will be saved.
Simple Variables If the variable is a simple variable, then it will be saved as a JSON plain array containing 1 element. Using 'JSON B4P' option in the loading process will recover the variable properly.
Arrays B4P arrays are stored as JSON arrays
Structures B4P structures are stored as JSON objects
strings string string contents are stored as text in double quotation marks
Numerals Numerals are written as numbers without quotation marks. When saving, scientific notation is not used. However, JSON files containing scientific notation will be loaded correctly.
Booleans Will be written as true and false. B4P will recognize boolean values and convert them to Boolean type
Void values Are written as null in JSON output.
Dates Dates (and times) are written as text in following formats: "YYYY-MM-DD", "hh:mm:ss", "YYYY-MM-DD hh:mm:ss", and "0000-00-00". The last value refers to a blank date and will be recognized as such. When reading JSON data, dates and times must adhere strictly with the given formats, otherwise they will be loaded as text (e.g. "01 April 2020").
Sets Sets will be converted to string format (similarly implemented as in the str() function) and stored as text. strings are stored in single qutoation marks, aiming to avoid confusion wtih double quotation marks for JSON text contents. Example: "{1,true,'Abc'}". Nested sets are supported.
Loading strings All loaded strings are declared as quoted strings
Special characters Specific characters will be converted to JSON-compliant escape sequences, e.g. new line, quotation marks inside strings, etc.

Intermediate Nodes

The JSON model describes a tree containing data at the end nodes only (leaves) whereas B4P variable model also supports data in intermediate nodes as well. In these case, intermediate arrays containing 2 elements are introduced to describe the intermediate node followed by the remaining data up the data tree. See the program example below:

JSON code without and with intermediate nodes
       b[Red] = Rot;
       b[Green,light] = Hellgrün;
       b[Green,dark] = Dunkelgrün;
       b[Blue,light] = Hellblau;
       b[Blue,dark] = Dunkelblau;

       see(b[]);

       j[] = variable to json( b[], JSON);

       echo("JSON code:", new line, j[], new line, "_______" );

       json to variable( c[], j[], JSON B4P );
       see( c[] );

       echo(new line, "Add data to intermediate nodes");

       b[] = Colors;
       b[Green] = Shades of green;

       j[] = variable to json( b[], JSON);

       echo("JSON code:", new line, j[], new line, "_______" );

       json to variable( c[], j[], JSON B4P );
       see( c[] ); // Same as before.

Output 02

b[]                     [Void]                     (void,full access)
Blue                    [Void]                     (void,full access)
  dark                  Dunkelblau                 (softquoted string,full access)
  light                 Hellblau                   (softquoted string,full access)
Green                   [Void]                     (void,full access)
  dark                  Dunkelgrün                (softquoted string,full access)
  light                 Hellgrün                  (softquoted string,full access)
Red                     Rot                        (softquoted string,full access)

JSON code:
{
  "Blue" : {
    "dark" : "Dunkelblau",
    "light" : "Hellblau"
  },
  "Green" : {
    "dark" : "Dunkelgrün",
    "light" : "Hellgrün"
  },
  "Red" : "Rot"
}
_______
c[]                     [Void]                     (void,full access)
Blue                    [Void]                     (void,full access)
  dark                  Dunkelblau                 (quoted string,full access)
  light                 Hellblau                   (quoted string,full access)
Green                   [Void]                     (void,full access)
  dark                  Dunkelgrün                (quoted string,full access)
  light                 Hellgrün                  (quoted string,full access)
Red                     Rot                        (quoted string,full access)


Add data to intermediate nodes
JSON code:

[ "Colors",{
    "Blue" : {
      "dark" : "Dunkelblau",
      "light" : "Hellblau"
    },
    "Green" :
    [ "Shades of green",{
        "dark" : "Dunkelgrün",
        "light" : "Hellgrün"
      }
      ],
    "Red" : "Rot"
  }
  ]
_______
c[]                     Colors                     (quoted string,full access)
Blue                    [Void]                     (void,full access)
  dark                  Dunkelblau                 (quoted string,full access)
  light                 Hellblau                   (quoted string,full access)
Green                   Shades of green            (quoted string,full access)
  dark                  Dunkelgrün                (quoted string,full access)
  light                 Hellgrün                  (quoted string,full access)
Red                     Rot                        (quoted string,full access)

Try it yourself: Open LIB_Features_Loading_and_saving_variables.b4p in B4P_Examples.zip. Decompress before use.




Procedures and Functions Provided:


Loading and saving variables using JSON:
    variable load
    json to variable
    variable save
    variable to json