Grav v1.14.0

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}}")))
}
Output
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: v and key: followed by - item at 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, !!bool and !!null tags (others are ignored); comments; and documents separated by --- (Yaml.parseAll; Yaml.parse gives the first).
  • YAML 1.2, not 1.1. yes, no, on and off are text, as are dates (keep them as strings or parse them with DateTime), and 0777 is the number 777 (write 0o777 for octal). Keys that are not text become their text.
  • Errors are ValueErrors with the line and column; Yaml.tryParse returns 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 (indent spaces 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) -> string
  • static 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>

Edit this page on GitHub