Loader

Loads YAML documents from files or char[].

User specified Constructor and/or Resolver can be used to support new tags / data types.

Disabled Default Constructor

A disabled default is present on this object. To use it, use one of the other constructors or a factory function.

Members

Functions

constructor
void constructor(Constructor constructor)

Specify custom Constructor to use.

load
Node load()

Load single YAML document.

loadAll
Node[] loadAll()

Load all YAML documents.

name
void name(string name)

Set stream name. Used in debugging messages.

opApply
int opApply(int delegate(ref Node) @safe dg)
int opApply(int delegate(ref Node) @system dg)

Foreach over YAML documents.

resolver
void resolver(Resolver resolver)

Specify custom Resolver to use.

Static functions

fromBuffer
Loader fromBuffer(ubyte[] yamlData)
Loader fromBuffer(void[] yamlData)

Construct a Loader to load YAML from a buffer.

fromFile
Loader fromFile(string filename)
Loader fromFile(File file)

Construct a Loader to load YAML from a file.

fromString
Loader fromString(char[] data)
Loader fromString(string data)

Construct a Loader to load YAML from a string.

Examples

Load single YAML document from a file:

write("example.yaml", "Hello world!");
auto rootNode = Loader.fromFile("example.yaml").load();
assert(rootNode == "Hello world!");

Load single YAML document from an already-opened file:

1 // Open a temporary file
2 auto file = File.tmpfile;
3 // Write valid YAML
4 file.write("Hello world!");
5 // Return to the beginning
6 file.seek(0);
7 // Load document
8 auto rootNode = Loader.fromFile(file).load();
9 assert(rootNode == "Hello world!");

Load all YAML documents from a file:

1 import std.file : write;
2 write("example.yaml",
3     "---\n"~
4     "Hello world!\n"~
5     "...\n"~
6     "---\n"~
7     "Hello world 2!\n"~
8     "...\n"
9 );
10 auto nodes = Loader.fromFile("example.yaml").loadAll();
11 assert(nodes.length == 2);

Iterate over YAML documents in a file, lazily loading them:

1 import std.file : write;
2 write("example.yaml",
3     "---\n"~
4     "Hello world!\n"~
5     "...\n"~
6     "---\n"~
7     "Hello world 2!\n"~
8     "...\n"
9 );
10 auto loader = Loader.fromFile("example.yaml");
11 
12 foreach(ref node; loader)
13 {
14     //Do something
15 }

Load YAML from a string:

1 string yaml_input = ("red:   '#ff0000'\n" ~
2                     "green: '#00ff00'\n" ~
3                     "blue:  '#0000ff'");
4 
5 auto colors = Loader.fromString(yaml_input).load();
6 
7 foreach(string color, string value; colors)
8 {
9     // Do something with the color and its value...
10 }

Load a file into a buffer in memory and then load YAML from that buffer:

1 import std.file : read, write;
2 import std.stdio : writeln;
3 // Create a yaml document
4 write("example.yaml",
5     "---\n"~
6     "Hello world!\n"~
7     "...\n"~
8     "---\n"~
9     "Hello world 2!\n"~
10     "...\n"
11 );
12 try
13 {
14     string buffer = readText("example.yaml");
15     auto yamlNode = Loader.fromString(buffer);
16 
17     // Read data from yamlNode here...
18 }
19 catch(FileException e)
20 {
21     writeln("Failed to read file 'example.yaml'");
22 }

Use a custom constructor/resolver to support custom data types and/or implicit tags:

1 import std.file : write;
2 // Create a yaml document
3 write("example.yaml",
4     "---\n"~
5     "Hello world!\n"~
6     "...\n"
7 );
8 auto constructor = new Constructor();
9 auto resolver = new Resolver();
10 
11 // Add constructor functions / resolver expressions here...
12 
13 auto loader = Loader.fromFile("example.yaml");
14 loader.constructor = constructor;
15 loader.resolver = resolver;
16 auto rootNode = loader.load();

Meta