dyaml.loader
Class used to load YAML documents.
- struct Loader;
Loads YAML documents from files or streams.
User specified Constructor and/or Resolver can be used to support new tags / data types.
Examples:Load single YAML document from a file:auto rootNode = Loader("file.yaml").load(); ...
Load all YAML documents from a file:auto nodes = Loader("file.yaml").loadAll(); ...
Iterate over YAML documents in a file, lazily loading them:auto loader = Loader("file.yaml"); foreach(ref node; loader) { ... }
Load YAML from memory:import std.stream; import std.stdio; string yaml_input = "red: '#ff0000'\n" "green: '#00ff00'\n" "blue: '#0000ff'"; auto colors = Loader(new MemoryStream(cast(char[])yaml_input)).load(); foreach(string color, string value; colors) { writeln(color, " is ", value, " in HTML/CSS"); }
Use a custom constructor/resolver to support custom data types and/or implicit tags:auto constructor = new Constructor(); auto resolver = new Resolver(); //Add constructor functions / resolver expressions here... auto loader = Loader("file.yaml"); loader.constructor = constructor; loader.resolver = resolver; auto rootNode = loader.load(node);
- this(string filename);
Construct a Loader to load YAML from a file.
Parameters:Throws:string filename Name of the file to load from. YAMLException if the file could not be opened or read.- this(Stream stream);
Construct a Loader to load YAML from a stream.
Parameters:Throws:Stream stream Stream to read from. Must be readable and seekable. YAMLException if stream could not be read.- @property void name(string name);
Set stream name. Used in debugging messages.
- @property void resolver(Resolver resolver);
Specify custom Resolver to use.
- @property void constructor(Constructor constructor);
Specify custom Constructor to use.
- Node load();
Load single YAML document.
If none or more than one YAML document is found, this throws a YAMLException.
Returns:
This can only be called once; this is enforced by contract.Root node of the document.Throws:YAMLException if there wasn't exactly one document or on a YAML parsing error.- Node[] loadAll();
Load all YAML documents.
This is just a shortcut that iterates over all documents and returns them all at once. Calling loadAll after iterating over the node or vice versa will not return any documents, as they have all been parsed already.
Returns:
This can only be called once; this is enforced by contract.Array of root nodes of all documents in the file/stream.Throws:YAMLException on a parsing error.- int opApply(int delegate(ref Node) dg);
Foreach over YAML documents.
Parses documents lazily, when they are needed.
Throws:
Foreach over a Loader can only be used once; this is enforced by contract.YAMLException on a parsing error.