Grav v1.14.0

TOML

import "std/data" (included in import "std") · namespace std.data

TOML 1.0 (the format of grav.toml, Cargo.toml and pyproject.toml) read into the same JsonValue tree as JSON, so the rest of the program uses it the same way: tables are objects, arrays are arrays, and dates and times are kept as their text. Toml.stringify writes a JsonValue object back: plain values first, then [tables] and [[arrays of tables]] (dates come out as quoted strings). Malformed input, or a key or table defined twice, is a ValueError naming the line. Its results match Python's tomllib.

grav
import "std"

fn main() {
    let cfg = Toml.parse("name = \"shop\"\n[server]\nport = 8080\nhosts = [\"a\", \"b\"]\n[[workers]]\nqueue = \"mail\"\n")
    print(cfg.getString("name", ""))
    print(cfg.get("server").get("port").asLong() + 1)
    print(cfg.get("workers").at(0).getString("queue", ""))
    print(Str.trimEnd(Toml.stringify(cfg)))
}
Output
shop
8081
mail
name = "shop"

[server]
port = 8080
hosts = ["a", "b"]

[[workers]]
queue = "mail"

class Toml

Static members

  • static fn parse(text: string) -> JsonValue — The document as a JsonValue object; ValueError (with the line) when it is not TOML.
  • static fn stringify(doc: JsonValue) -> string — A JsonValue object as TOML: plain values first, then tables ([name]) and arrays of tables ([[name]]); arrays of plain values and objects inside arrays are written inline. Null values are left out (TOML has none).
  • static fn key(k: string) -> string
  • static fn value(v: JsonValue) -> string

class TomlParser

A one-pass reader over the text's bytes.

Constructors

  • new TomlParser(text: string)

Methods

  • fn document() -> JsonValue

Edit this page on GitHub