Loader

Loads YAML documents from files or char[].

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

Constructors

this
this()
Undocumented in source.

Members

Functions

empty
bool empty()

Implements the empty range primitive.

front
Node front()

Implements the front range primitive.

load
Node load()

Load single YAML document.

name
void name(string name)

Set stream name. Used in debugging messages.

opCmp
int opCmp(Loader )
Undocumented in source.
opEquals
bool opEquals(Loader )
Undocumented in source.
parse
auto parse()
Undocumented in source. Be warned that the author may not have intended to support it.
popFront
void popFront()

Implements the popFront range primitive.

resolver
auto ref resolver()

Specify custom Resolver to use.

scanBench
void scanBench()
Undocumented in source. Be warned that the author may not have intended to support it.

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, string filename)
Loader fromString(string data, string filename)

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:

// Open a temporary file
auto file = File.tmpfile;
// Write valid YAML
file.write("Hello world!");
// Return to the beginning
file.seek(0);
// Load document
auto rootNode = Loader.fromFile(file).load();
assert(rootNode == "Hello world!");

Load all YAML documents from a file:

import std.array : array;
import std.file : write;
write("example.yaml",
    "---\n"~
    "Hello world!\n"~
    "...\n"~
    "---\n"~
    "Hello world 2!\n"~
    "...\n"
);
auto nodes = Loader.fromFile("example.yaml").array;
assert(nodes.length == 2);

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

import std.file : write;
write("example.yaml",
    "---\n"~
    "Hello world!\n"~
    "...\n"~
    "---\n"~
    "Hello world 2!\n"~
    "...\n"
);
auto loader = Loader.fromFile("example.yaml");

foreach(ref node; loader)
{
    //Do something
}

Load YAML from a string:

string yaml_input = ("red:   '#ff0000'\n" ~
                    "green: '#00ff00'\n" ~
                    "blue:  '#0000ff'");

auto colors = Loader.fromString(yaml_input).load();

foreach(string color, string value; colors)
{
    // Do something with the color and its value...
}

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

import std.file : read, write;
import std.stdio : writeln;
// Create a yaml document
write("example.yaml",
    "---\n"~
    "Hello world!\n"~
    "...\n"~
    "---\n"~
    "Hello world 2!\n"~
    "...\n"
);
try
{
    string buffer = readText("example.yaml");
    auto yamlNode = Loader.fromString(buffer);

    // Read data from yamlNode here...
}
catch(FileException e)
{
    writeln("Failed to read file 'example.yaml'");
}

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

import std.file : write;
// Create a yaml document
write("example.yaml",
    "---\n"~
    "Hello world!\n"~
    "...\n"
);

auto loader = Loader.fromFile("example.yaml");

// Add resolver expressions here...
// loader.resolver.addImplicitResolver(...);

auto rootNode = loader.load();

Meta