JSON & YAML
The json namespace provides fast built-in JSON parsing and serialization.
json.parse(string)
Section titled “json.parse(string)”Converts a JSON string into Praia values:
| JSON | Praia |
|---|---|
{} | map |
[] | array |
"string" | string |
123 | number |
true/false | bool |
null | nil |
let data = json.parse("{\"name\": \"Ada\", \"age\": 36}")print(data.name) // Adaprint(data.age) // 36
let list = json.parse("[1, 2, 3]")print(list) // [1, 2, 3]json.stringify(value, indent?)
Section titled “json.stringify(value, indent?)”Converts a Praia value to a JSON string. Optional indent for pretty-printing.
let obj = {name: "Ada", scores: [100, 95]}
json.stringify(obj) // {"name":"Ada","scores":[100,95]}json.stringify(obj, 2) // pretty-printed with 2-space indentRound-tripping
Section titled “Round-tripping”let original = {users: [{name: "Alice"}, {name: "Bob"}]}let str = json.stringify(original)let restored = json.parse(str)print(restored.users[0].name) // AliceThe yaml namespace provides built-in YAML parsing and serialization. Supports mappings, sequences, nested structures, comments, flow sequences, and quoted strings.
yaml.parse(string)
Section titled “yaml.parse(string)”let config = yaml.parse("host: localhost\nport: 8080\ndebug: true")print(config.host) // localhostprint(config.port) // 8080print(config.debug) // trueNested:
let yaml_str = "database:\n host: localhost\n port: 5432"let conf = yaml.parse(yaml_str)print(conf.database.host) // localhostSequences:
let list = yaml.parse("- apple\n- banana\n- cherry")print(list) // ["apple", "banana", "cherry"]Flow sequences:
let data = yaml.parse("tags: [web, api, fast]")print(data.tags) // ["web", "api", "fast"]Comments are stripped:
let data = yaml.parse("name: Ada # the inventor")print(data.name) // Adayaml.stringify(value)
Section titled “yaml.stringify(value)”let obj = {name: "Praia", features: ["fast", "simple"]}print(yaml.stringify(obj))// name: Praia// features:// - fast// - simpleReading config files
Section titled “Reading config files”let config = yaml.parse(sys.read("config.yaml"))print("Listening on port %{config.server.port}")