NimYAML Home Testing Ground Docs: Overview Serialization Modules NimYAML 2.x Source on GitHub

Module yaml/dom

This is the DOM API, which enables you to load YAML into a tree-like structure. It can also dump the structure back to YAML. Formally, it represents the Representation Graph as defined in the YAML specification.

You use this API by importing this module alongside loading and/or dumping, and then call load() / dump() on YamlNode values. A special function loadFlattened allows you to load aliases in the YAML input as if they were copies of the referenced subtree.

The YamlNode objects in the DOM are structured similarly to the JsonNode objects of Nim's json module.

Imports

data, stream, taglib, internal, parser, loading

Types

YamlNode = ref YamlNodeObj
Represents a node in a YAML document
YamlNodeKind = enum
  yScalar, yMapping, ySequence
YamlNodeObj = object
  tag*: Tag
  startPos*, endPos*: Mark
  case kind*: YamlNodeKind
  of yScalar:
      content*: string
      scalarStyle*: ScalarStyle

  of ySequence:
      elems*: seq[YamlNode]
      seqStyle*: CollectionStyle

  of yMapping:
      fields*: TableRef[YamlNode, YamlNode]
      mapStyle*: CollectionStyle

  

Procs

proc `$`(n: YamlNode): string {....raises: [], tags: [], forbids: [].}
proc `==`(x, y: YamlNode): bool {....raises: [], tags: [], forbids: [].}
proc `[]`(node: YamlNode; i: int): YamlNode {....raises: [], tags: [], forbids: [].}
Get the node at index i from a sequence. node must be a ySequence.
proc `[]`(node: YamlNode; key: string): YamlNode {....raises: [KeyError], tags: [],
    forbids: [].}
Get the value for a string key in a mapping. node must be a yMapping. This searches for a scalar key with content key and either no explicit tag or the explicit tag !!str.
proc `[]`(node: YamlNode; key: YamlNode): YamlNode {....raises: [KeyError],
    tags: [], forbids: [].}
proc `[]=`(node: var YamlNode; i: int; val: YamlNode) {....raises: [], tags: [],
    forbids: [].}
Set the node at index i of a sequence. node must be a ySequence.
proc `[]=`(node: YamlNode; key: YamlNode; value: YamlNode) {....raises: [],
    tags: [], forbids: [].}
Set the value for a key in a mapping. node must be a yMapping.
proc constructChild(ctx: var ConstructionContext; result: var YamlNode) {.
    ...raises: [YamlStreamError, YamlConstructionError], tags: [RootEffect],
    forbids: [].}
proc hash(o: YamlNode): Hash {....raises: [], tags: [], forbids: [].}
proc len(node: YamlNode): int {....raises: [], tags: [], forbids: [].}
If node is a yMapping, return the number of key-value pairs. If node is a ySequence, return the number of elements. Else, return 0
proc loadFlattened[K](input: Stream | string; target: var K) {....raises: [
    YamlConstructionError, YamlSerializationError, IOError, OSError,
    YamlParserError].}
Replaces all aliases with the referenced nodes in the input, then loads the resulting YAML into K. Can be used when anchors & aliases are used like variables in the input, to avoid having to define ref types for the anchored data.
proc newYamlNode(content: string; tag: Tag = yTagQuestionMark;
                 style: ScalarStyle = ssAny; startPos: Mark = Mark();
                 endPos: Mark = Mark()): YamlNode {....raises: [], tags: [],
    forbids: [].}
proc newYamlNode(elems: openArray[YamlNode]; tag: Tag = yTagQuestionMark;
                 style: CollectionStyle = csAny; startPos: Mark = Mark();
                 endPos: Mark = Mark()): YamlNode {....raises: [], tags: [],
    forbids: [].}
proc newYamlNode(fields: openArray[(YamlNode, YamlNode)];
                 tag: Tag = yTagQuestionMark; style: CollectionStyle = csAny;
                 startPos: Mark = Mark(); endPos: Mark = Mark()): YamlNode {.
    ...raises: [], tags: [], forbids: [].}
proc representChild(ctx: var SerializationContext; value: YamlNodeObj) {.
    ...raises: [YamlSerializationError], tags: [RootEffect], forbids: [].}

Iterators

iterator items(node: YamlNode): YamlNode {....raises: [], tags: [], forbids: [].}
Iterates over all items of a sequence. node must be a ySequence.
iterator mitems(node: var YamlNode): YamlNode {....raises: [], tags: [],
    forbids: [].}
Iterates over all items of a sequence. node must be a ySequence. Values can be modified.
iterator mpairs(node: var YamlNode): tuple[key: YamlNode, value: var YamlNode] {.
    ...raises: [], tags: [], forbids: [].}
Iterates over all key-value pairs of a mapping. node must be a yMapping. Values can be modified.
iterator pairs(node: YamlNode): tuple[key, value: YamlNode] {....raises: [],
    tags: [], forbids: [].}
Iterates over all key-value pairs of a mapping. node must be a yMapping.