JSON by Dannii Willis


This extension provides support for parsing, processing, and generating JSON.

Chapter - Creating JSON references

The basic data structure in this extension is the JSON reference. You can create JSON references with these phrases:

JSON null
JSON true
JSON false/boolean
JSON number (N - number)
JSON real number (N - real number)
JSON string/text from (T - text)
JSON array
JSON object

To determine which type a JSON reference has, use the phrase "type of (json reference)".

Chapter - Reading and setting JSON references

You can access the internal data of a JSON reference with these phrases. If you try to access a reference using the wrong type an error will be shown.

if (R - JSON reference) as a truth state:
(R - JSON reference) as a number
(R - JSON reference) as a real number
let (T - nonexisting text variable) be (R - JSON reference) as a text/string
let (L - nonexisting list of JSON references variable) be (R - JSON reference) as a/an list/array

To set a boolean, number, or real number, use these phrases:

set (R - JSON reference) to (V - truth state)
set (R - JSON reference) to (V - number)
set (R - JSON reference) to (V - real number)

Strings and arrays can be modified in place using the standard Inform phrases. You do not need to update the JSON reference with the new text or list.

Chapter - JSON objects

There is no Inform data structure that fits a JSON object, but you can use these phrases to access them:

if (R - JSON reference) has key (K - text):
set key (K - text) of (R - JSON reference) to (V - JSON reference)
get key (K - text) of (R - JSON reference)
delete key (K - text) of (R - JSON reference)

You can also repeat through the keys of a JSON object with this phrase:

repeat with (loopvar - nonexisting text variable) of/in (R - JSON reference)

Chapter - Parsing and stringifying JSON

These two phrases will parse a text into a JSON reference, and a JSON reference into a text:

parse (T - a text)
say (R - JSON reference)
say (R - JSON reference) escaping non-ASCII

You can use the standard Inform phrases "text of (external file)" and "write (text) to (external file)" to read and write JSON files, but note that they will have the. glkdata extension, as well as the normal Inform file header. These file phrases only support ASCII, so use the "escaping non-ASCII" phrase if you have non-ASCII characters.

Chapter - Cleaning up

JSON references exist outside the normal Inform model, so you must manually destroy them in order to not leak memory. Destroying an array or object will clean up all of its contents as well.

destroy (R - JSON reference)

Example: * Actions tracker

"Actions tracker"

Include JSON by Dannii Willis.

The Lab is a room.
In the lab is a coin.

The File of Tracked Actions is called "actions".

When play begins:
     if the File of Tracked Actions exists:
         let text data be "[text of File of Tracked Actions]";
         let json data be parse text data;
         if json data has key "restarted":
             say "You have restarted this demo.[line break]";
         otherwise:
             say "You have not yet restarted this demo.[line break]";
         if json data has key "jumped":
             let jump count be get key "jumped" of json data;
             say "You have jumped [jump count as a number] times.[line break]";
         if json data has key "picked up coin":
             let pick up coint count be get key "picked up coin" of json data;
             say "You have picked up the coin [pick up coint count as a number] times.[line break]";
         destroy json data;

First carry out restarting the game rule:
     let json data be a JSON reference;
     if the File of Tracked Actions exists:
         let text data be "[text of File of Tracked Actions]";
         let json data be parse text data;
     otherwise:
         let json data be a new JSON object;
     set key "restarted" of json data to JSON true;
     write "[json data]" to File of Tracked Actions;
     destroy json data;

To increment (T - a text) in tracked actions:
     let json data be a JSON reference;
     if the File of Tracked Actions exists:
         let text data be "[text of File of Tracked Actions]";
         let json data be parse text data;
     otherwise:
         let json data be a new JSON object;
     let count be 0;
     if json data has key T:
         now count is get key T of json data as a number;
     increment count;
     set key T of json data to JSON number count;
     write "[json data]" to File of Tracked Actions;
     destroy json data;

Carry out jumping:
     increment "jumped" in tracked actions;

After taking the coin:
     increment "picked up coin" in tracked actions;
     continue the action;