JSON Handling

JSON is the standard format for data exchange. The system.json module makes it easy to work with JSON in Quartz.

Parsing JSON

Convert a JSON string into Quartz dictionaries and arrays:

import system.io as io;
import system.json as json;

let raw = '{"name": "Quartz", "version": 0.5, "tags": ["brave", "small"]}';
let data = json.parse(raw);

io.out.println("Language:", data["name"]);
io.out.println("First tag:", data["tags"][0]);

Generating JSON

Convert Quartz collections back into a JSON string:

import system.io as io;
import system.json as json;

let config = {
"debug": true,
"port": 8080,
"allowed_hosts": ["localhost", "127.0.0.1"]
};

let serialized = json.stringify(config);
io.out.println(serialized);

Reading JSON from a File

Combine system.fs and system.json:

import system.io as io;
import system.fs as fs;
import system.json as json;

fn loadConfig(path) {
if (!fs.exists(path)) {
return {};
}
let raw = fs.read_file(path);
return json.parse(raw);
}

let myConfig = loadConfig("settings.json");

Related Reference