YAML
import "std/data" (included in import "std") · namespace std.data
YAML 1.2 read into, and written from, the same JsonValue tree as JSON: mappings are objects, sequences are arrays, and plain scalars are typed by the core schema (null, true/false, integers, floats, else text).
grav
import "std"
fn main() {
let cfg = Yaml.parse("app:\n name: shop\n port: 8080\n debug: false\n" +
"defaults: &db\n host: localhost\n pool: 5\n" +
"databases:\n main:\n <<: *db\n name: shop\n" +
"hosts: [a.test, b.test]\n" +
"motd: |\n Welcome!\n Be nice.\n")
print(cfg.path("app.port").asLong())
print(cfg.path("databases.main.host").asString())
print(cfg.get("hosts").at(1).asString())
print(cfg.getString("motd", ""))
print(Yaml.stringify(Json.parse("{\"name\":\"shop\",\"tags\":[\"a\",\"b\"],\"on\":\"yes\",\"limits\":{\"rps\":10}}")))
}8080
localhost
b.test
Welcome!
Be nice.
name: shop
tags:
- a
- b
"on": "yes"
limits:
rps: 10
- What is read. Block and flow collections (and the compact forms,
- key: vandkey:followed by- itemat the same indentation); plain, single- and double-quoted scalars, over several lines too; literal (|) and folded (>) block scalars with their chomping (-,+) and indentation indicators; anchors and aliases; merge keys (<<); the!!str,!!int,!!float,!!booland!!nulltags (others are ignored); comments; and documents separated by---(Yaml.parseAll;Yaml.parsegives the first). - YAML 1.2, not 1.1.
yes,no,onandoffare text, as are dates (keep them as strings or parse them with DateTime), and0777is the number 777 (write0o777for octal). Keys that are not text become their text. - Errors are
ValueErrors with the line and column;Yaml.tryParsereturns null instead. - Writing.
Yaml.stringify(value, indent)writes block style, quoting text only when it would otherwise read back as something else ("yes"stays text in YAML 1.1 readers too), and writing multi-line text as a literal block. - Speed. A 185 KB lock file parses in about 17 ms, about three times faster than PyYAML with libyaml.
API
class Yaml
Static members
static fn parse(text: string) -> JsonValue— The first document (null as JsonValue when there is none); ValueError on bad input.static fn parseAll(text: string) -> Array<JsonValue>— Every document of a stream (separated by ---).static fn tryParse(text: string) -> JsonValue— Parse, or null when the text is not valid YAML.static fn stringify(v: JsonValue, indent: int = 2) -> string— A value as YAML in block style (indentspaces per level), ending in a newline.
class YamlWriter
Static members
static fn pad(out: StringBuilder, n: int)static fn mapping(out: StringBuilder, v: JsonValue, depth: int, step: int)static fn sequence(out: StringBuilder, v: JsonValue, depth: int, step: int)static fn scalar(v: JsonValue, depth: int, step: int) -> stringstatic fn quoteIfNeeded(s: string) -> string— Text as a plain scalar when it reads back as the same string, else double-quoted.
class YamlParser
Constructors
new YamlParser(text: string)
Static members
static fn hexValue(h: string) -> long— Hex digits as a number, or -1.static fn resolve(s: string) -> JsonValue— A plain scalar's value: null, a boolean, an integer, a float, or text.
Methods
fn documents(firstOnly: bool) -> Array<JsonValue>