Fixed a bug which prevented dumping to file. Updated tutorials
and example with new information.
This commit is contained in:
parent
23290239a7
commit
210091a75f
20 changed files with 493 additions and 68 deletions
|
@ -3,10 +3,16 @@ import yaml;
|
|||
|
||||
void main()
|
||||
{
|
||||
yaml.Node root = Loader("input.yaml").load();
|
||||
//Read the input.
|
||||
Node root = Loader("input.yaml").load();
|
||||
|
||||
//Display the data read.
|
||||
foreach(string word; root["Hello World"])
|
||||
{
|
||||
writeln(word);
|
||||
}
|
||||
writeln("The answer is ", root["Answer"].get!int);
|
||||
|
||||
//Dump the loaded document to output.yaml.
|
||||
Dumper("output.yaml").dump(root);
|
||||
}
|
||||
|
|
2
examples/representer/Makefile
Normal file
2
examples/representer/Makefile
Normal file
|
@ -0,0 +1,2 @@
|
|||
main:
|
||||
dmd -w -I../../ -L-L../../ -L-ldyaml main.d
|
55
examples/representer/main.d
Normal file
55
examples/representer/main.d
Normal file
|
@ -0,0 +1,55 @@
|
|||
import std.stdio;
|
||||
import yaml;
|
||||
|
||||
struct Color
|
||||
{
|
||||
ubyte red;
|
||||
ubyte green;
|
||||
ubyte blue;
|
||||
}
|
||||
|
||||
Node representColor(ref Node node, Representer representer)
|
||||
{
|
||||
//The node is guaranteed to be Color as we add representer for Color.
|
||||
Color color = node.get!Color;
|
||||
|
||||
static immutable hex = "0123456789ABCDEF";
|
||||
|
||||
//Using the color format from the Constructor example.
|
||||
string scalar;
|
||||
foreach(channel; [color.red, color.green, color.blue])
|
||||
{
|
||||
scalar ~= hex[channel / 16];
|
||||
scalar ~= hex[channel % 16];
|
||||
}
|
||||
|
||||
//Representing as a scalar, with custom tag to specify this data type.
|
||||
return representer.representScalar("!color", scalar);
|
||||
}
|
||||
|
||||
void main()
|
||||
{
|
||||
try
|
||||
{
|
||||
auto representer = new Representer;
|
||||
representer.addRepresenter!Color(&representColor);
|
||||
|
||||
auto resolver = new Resolver;
|
||||
resolver.addImplicitResolver("!color", std.regex.regex("[0-9a-fA-F]{6}"),
|
||||
"0123456789abcdefABCDEF");
|
||||
|
||||
auto dumper = Dumper("output.yaml");
|
||||
dumper.representer = representer;
|
||||
dumper.resolver = resolver;
|
||||
|
||||
auto document = Node([Color(255, 0, 0),
|
||||
Color(0, 255, 0),
|
||||
Color(0, 0, 255)]);
|
||||
|
||||
dumper.dump(document);
|
||||
}
|
||||
catch(YAMLException e)
|
||||
{
|
||||
writeln(e.msg);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue