2011-08-16 12:53:13 +00:00
|
|
|
======================
|
|
|
|
Custom YAML data types
|
|
|
|
======================
|
|
|
|
|
2011-10-29 18:43:30 +00:00
|
|
|
Sometimes you need to serialize complex data types such as classes. To do this
|
2012-01-23 14:57:26 +00:00
|
|
|
you could use plain nodes such as mappings with classes' fields. YAML also
|
2011-11-17 22:53:24 +00:00
|
|
|
supports custom types with identifiers called *tags*. That is the topic of this
|
|
|
|
tutorial.
|
2011-08-16 12:53:13 +00:00
|
|
|
|
|
|
|
Each YAML node has a tag specifying its type. For instance: strings use the tag
|
|
|
|
``tag:yaml.org,2002:str``. Tags of most default types are *implicitly resolved*
|
2011-11-17 22:53:24 +00:00
|
|
|
during parsing - you don't need to specify tag for each float, integer, etc.
|
2011-10-29 18:43:30 +00:00
|
|
|
D:YAML can also implicitly resolve custom tags, as we will show later.
|
2011-08-16 12:53:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
-----------
|
|
|
|
Constructor
|
|
|
|
-----------
|
|
|
|
|
2011-10-15 14:31:23 +00:00
|
|
|
D:YAML uses the `Constructor <../api/dyaml.constructor.html>`_ class to process
|
2011-10-29 18:43:30 +00:00
|
|
|
each node to hold data type corresponding to its tag. *Constructor* stores
|
|
|
|
functions to process each supported tag. These are supplied by the user using
|
|
|
|
the *addConstructorXXX()* methods, where *XXX* is *Scalar*, *Sequence* or
|
|
|
|
*Mapping*. *Constructor* is then passed to *Loader*, which parses YAML input.
|
2011-10-15 14:31:23 +00:00
|
|
|
|
2012-01-23 14:57:26 +00:00
|
|
|
Structs and classes must implement the *opCmp()* operator for YAML support. This
|
|
|
|
is used for duplicate detection in mappings, sorting and equality comparisons of
|
|
|
|
nodes. The signature of the operator that must be implemented is
|
|
|
|
``const int opCmp(ref const MyStruct s)`` for structs where *MyStruct* is the
|
|
|
|
struct type, and ``int opCmp(Object o)`` for classes. Note that the class
|
|
|
|
*opCmp()* should not alter the compared values - it is not const for compatibility
|
|
|
|
reasons.
|
2011-08-16 12:53:13 +00:00
|
|
|
|
|
|
|
We will implement support for an RGB color type. It is implemented as the
|
|
|
|
following struct:
|
|
|
|
|
|
|
|
.. code-block:: d
|
|
|
|
|
|
|
|
struct Color
|
|
|
|
{
|
|
|
|
ubyte red;
|
|
|
|
ubyte green;
|
|
|
|
ubyte blue;
|
2012-01-23 14:57:26 +00:00
|
|
|
|
|
|
|
const int opCmp(ref const Color c)
|
|
|
|
{
|
|
|
|
if(red != c.red) {return red - c.red;}
|
|
|
|
if(green != c.green){return green - c.green;}
|
|
|
|
if(blue != c.blue) {return blue - c.blue;}
|
|
|
|
return 0;
|
|
|
|
}
|
2011-08-16 12:53:13 +00:00
|
|
|
}
|
|
|
|
|
2011-11-17 22:53:24 +00:00
|
|
|
First, we need a function to construct our data type. The function will take a
|
|
|
|
reference to *Node* to construct from. The node is guaranteed to contain either
|
|
|
|
a *string*, an array of *Node* or of *Node.Pair*, depending on whether we're
|
|
|
|
constructing our value from a scalar, sequence, or mapping, respectively.
|
|
|
|
If this function throws any exception, D:YAML handles it and adds its message
|
|
|
|
to a *YAMLException* that will be thrown when loading the file.
|
|
|
|
|
|
|
|
In this tutorial, we have functions to construct a color from a scalar, using
|
|
|
|
CSS-like format, RRGGBB, or from a mapping, where we use the following format:
|
|
|
|
{r:RRR, g:GGG, b:BBB} . Code of these functions:
|
2011-08-16 12:53:13 +00:00
|
|
|
|
|
|
|
.. code-block:: d
|
|
|
|
|
2011-11-17 22:53:24 +00:00
|
|
|
Color constructColorScalar(ref Node node)
|
2011-08-16 12:53:13 +00:00
|
|
|
{
|
2011-10-22 15:06:32 +00:00
|
|
|
string value = node.as!string;
|
2011-10-17 10:53:38 +00:00
|
|
|
|
2011-08-16 12:53:13 +00:00
|
|
|
if(value.length != 6)
|
|
|
|
{
|
2011-11-17 22:53:24 +00:00
|
|
|
throw new Exception("Invalid color: " ~ value);
|
2011-08-16 12:53:13 +00:00
|
|
|
}
|
|
|
|
//We don't need to check for uppercase chars this way.
|
|
|
|
value = value.toLower();
|
|
|
|
|
|
|
|
//Get value of a hex digit.
|
|
|
|
uint hex(char c)
|
|
|
|
{
|
2014-08-01 01:00:40 +00:00
|
|
|
import std.ascii;
|
2011-08-16 12:53:13 +00:00
|
|
|
if(!std.ascii.isHexDigit(c))
|
|
|
|
{
|
2011-11-17 22:53:24 +00:00
|
|
|
throw new Exception("Invalid color: " ~ value);
|
2011-08-16 12:53:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(std.ascii.isDigit(c))
|
|
|
|
{
|
|
|
|
return c - '0';
|
|
|
|
}
|
|
|
|
return c - 'a' + 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
Color result;
|
|
|
|
result.red = cast(ubyte)(16 * hex(value[0]) + hex(value[1]));
|
|
|
|
result.green = cast(ubyte)(16 * hex(value[2]) + hex(value[3]));
|
|
|
|
result.blue = cast(ubyte)(16 * hex(value[4]) + hex(value[5]));
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2011-11-17 22:53:24 +00:00
|
|
|
Color constructColorMapping(ref Node node)
|
2011-08-16 12:53:13 +00:00
|
|
|
{
|
2011-10-17 11:13:02 +00:00
|
|
|
ubyte r,g,b;
|
|
|
|
|
|
|
|
//Might throw if a value is missing is not an integer, or is out of range.
|
2011-11-17 22:53:24 +00:00
|
|
|
//If this happens, D:YAML will handle the exception and use its message
|
|
|
|
//in a YAMLException thrown when loading.
|
|
|
|
r = node["r"].as!ubyte;
|
|
|
|
g = node["g"].as!ubyte;
|
|
|
|
b = node["b"].as!ubyte;
|
2011-10-17 11:13:02 +00:00
|
|
|
|
2011-08-16 12:53:13 +00:00
|
|
|
return Color(cast(ubyte)r, cast(ubyte)g, cast(ubyte)b);
|
|
|
|
}
|
2011-10-17 11:13:02 +00:00
|
|
|
|
2011-10-15 14:31:23 +00:00
|
|
|
Next, we need some YAML data using our new tag. Create a file called
|
|
|
|
``input.yaml`` with the following contents:
|
2011-08-16 12:53:13 +00:00
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
scalar-red: !color FF0000
|
|
|
|
scalar-orange: !color FFFF00
|
|
|
|
mapping-red: !color-mapping {r: 255, g: 0, b: 0}
|
|
|
|
mapping-orange:
|
|
|
|
!color-mapping
|
|
|
|
r: 255
|
|
|
|
g: 255
|
|
|
|
b: 0
|
|
|
|
|
|
|
|
You can see that we're using tag ``!color`` for scalar colors, and
|
|
|
|
``!color-mapping`` for colors expressed as mappings.
|
|
|
|
|
|
|
|
Finally, the code to put it all together:
|
|
|
|
|
|
|
|
.. code-block:: d
|
|
|
|
|
|
|
|
void main()
|
|
|
|
{
|
|
|
|
auto red = Color(255, 0, 0);
|
|
|
|
auto orange = Color(255, 255, 0);
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto constructor = new Constructor;
|
|
|
|
//both functions handle the same tag, but one handles scalar, one mapping.
|
2011-10-17 10:53:38 +00:00
|
|
|
constructor.addConstructorScalar("!color", &constructColorScalar);
|
|
|
|
constructor.addConstructorMapping("!color-mapping", &constructColorMapping);
|
2011-08-16 12:53:13 +00:00
|
|
|
|
2011-10-14 08:34:53 +00:00
|
|
|
auto loader = Loader("input.yaml");
|
|
|
|
loader.constructor = constructor;
|
2011-08-16 12:53:13 +00:00
|
|
|
|
2011-10-14 08:34:53 +00:00
|
|
|
auto root = loader.load();
|
2011-08-16 12:53:13 +00:00
|
|
|
|
2011-10-22 15:06:32 +00:00
|
|
|
if(root["scalar-red"].as!Color == red &&
|
|
|
|
root["mapping-red"].as!Color == red &&
|
|
|
|
root["scalar-orange"].as!Color == orange &&
|
|
|
|
root["mapping-orange"].as!Color == orange)
|
2011-08-16 12:53:13 +00:00
|
|
|
{
|
|
|
|
writeln("SUCCESS");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(YAMLException e)
|
|
|
|
{
|
|
|
|
writeln(e.msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
writeln("FAILURE");
|
|
|
|
}
|
|
|
|
|
|
|
|
First, we create a *Constructor* and pass functions to handle the ``!color``
|
2011-10-15 14:31:23 +00:00
|
|
|
and ``!color-mapping`` tag. We construct a *Loader* and pass the *Constructor*
|
2011-10-22 15:06:32 +00:00
|
|
|
to it. We then load the YAML document, and finally, read the colors to test if
|
|
|
|
they were loaded as expected.
|
2011-08-16 12:53:13 +00:00
|
|
|
|
|
|
|
You can find the source code for what we've done so far in the
|
|
|
|
``examples/constructor`` directory in the D:YAML package.
|
|
|
|
|
|
|
|
|
|
|
|
--------
|
|
|
|
Resolver
|
|
|
|
--------
|
|
|
|
|
2011-10-29 18:43:30 +00:00
|
|
|
Specifying tag for every color can be tedious. D:YAML can implicitly resolve
|
|
|
|
scalar tags using regular expressions. This is how default types are resolved.
|
|
|
|
We will use the `Resolver <../api/dyaml.resolver.html>`_ class to add implicit
|
|
|
|
tag resolution for the Color data type (in its scalar form).
|
2011-08-16 12:53:13 +00:00
|
|
|
|
2011-10-15 14:31:23 +00:00
|
|
|
We use the *addImplicitResolver()* method of *Resolver*, passing the tag,
|
|
|
|
regular expression the scalar must match to resolve to this tag, and a string of
|
|
|
|
possible starting characters of the scalar. Then we pass the *Resolver* to
|
|
|
|
*Loader*.
|
2011-08-16 12:53:13 +00:00
|
|
|
|
|
|
|
Note that resolvers added first override ones added later. If no resolver
|
|
|
|
matches a scalar, YAML string tag is used. Therefore our custom values must not
|
|
|
|
be resolvable as any non-string YAML data type.
|
|
|
|
|
|
|
|
Add this to your code to add implicit resolution of ``!color``.
|
|
|
|
|
|
|
|
.. code-block:: d
|
|
|
|
|
|
|
|
//code from the previous example...
|
|
|
|
|
|
|
|
auto resolver = new Resolver;
|
2011-10-15 14:31:23 +00:00
|
|
|
resolver.addImplicitResolver("!color", std.regex.regex("[0-9a-fA-F]{6}"),
|
|
|
|
"0123456789abcdefABCDEF");
|
2011-08-16 12:53:13 +00:00
|
|
|
|
2011-10-14 08:34:53 +00:00
|
|
|
auto loader = Loader("input.yaml");
|
|
|
|
|
|
|
|
loader.constructor = constructor;
|
|
|
|
loader.resolver = resolver;
|
2011-08-16 12:53:13 +00:00
|
|
|
|
|
|
|
//code from the previous example...
|
|
|
|
|
2011-10-15 14:31:23 +00:00
|
|
|
Now, change contents of ``input.yaml`` to this:
|
2011-08-16 12:53:13 +00:00
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
scalar-red: FF0000
|
|
|
|
scalar-orange: FFFF00
|
|
|
|
mapping-red: !color-mapping {r: 255, g: 0, b: 0}
|
|
|
|
mapping-orange:
|
|
|
|
!color-mapping
|
|
|
|
r: 255
|
|
|
|
g: 255
|
|
|
|
b: 0
|
|
|
|
|
|
|
|
We no longer need to specify the tag for scalar color values. Compile and test
|
|
|
|
the example. If everything went as expected, it should report success.
|
|
|
|
|
|
|
|
You can find the complete code in the ``examples/resolver`` directory in the
|
|
|
|
D:YAML package.
|
2011-10-15 14:31:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
-----------
|
|
|
|
Representer
|
|
|
|
-----------
|
|
|
|
|
2011-10-29 18:43:30 +00:00
|
|
|
Now that you can load custom data types, it might be good to know how to dump
|
|
|
|
them. D:YAML uses the `Representer <../api/dyaml.representer.html>`_ class for
|
|
|
|
this purpose.
|
2011-10-15 14:31:23 +00:00
|
|
|
|
|
|
|
*Representer* processes YAML nodes into plain mapping, sequence or scalar nodes
|
|
|
|
ready for output. Just like with *Constructor*, this is done by user specified
|
|
|
|
functions. These functions take references to a node to process and to the
|
|
|
|
*Representer*, and return the processed node.
|
|
|
|
|
|
|
|
Representer functions can be added with the *addRepresenter()* method. The
|
|
|
|
*Representer* is then passed to *Dumper*, which dumps YAML documents. Only one
|
2011-10-29 18:43:30 +00:00
|
|
|
function per type can be specified. This is asserted in *addRepresenter()*
|
|
|
|
preconditions. Default YAML types already have representer functions specified,
|
|
|
|
but you can disable them by constructing *Representer* with the
|
2011-10-15 14:31:23 +00:00
|
|
|
*useDefaultRepresenters* parameter set to false.
|
|
|
|
|
2011-10-29 18:43:30 +00:00
|
|
|
By default, tags are explicitly output for all non-default types. To make dumped
|
|
|
|
tags implicit, you can pass a *Resolver* that will resolve them implicitly. Of
|
|
|
|
course, you will need to use an identical *Resolver* when loading the output.
|
2011-10-15 14:31:23 +00:00
|
|
|
|
|
|
|
With the following code, we will add support for dumping the our Color type.
|
|
|
|
|
|
|
|
.. code-block:: d
|
|
|
|
|
|
|
|
Node representColor(ref Node node, Representer representer)
|
|
|
|
{
|
|
|
|
//The node is guaranteed to be Color as we add representer for Color.
|
2011-10-22 15:06:32 +00:00
|
|
|
Color color = node.as!Color;
|
2011-10-15 14:31:23 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
First we get the *Color* from the node. Then we convert it to a string with the
|
2011-10-17 10:53:38 +00:00
|
|
|
CSS-like format we've used before. Finally, we use the *representScalar()*
|
2011-10-29 18:43:30 +00:00
|
|
|
method of *Representer* to get a scalar node ready for output. There are
|
|
|
|
corresponding *representMapping()* and *representSequence()* methods
|
2011-10-15 14:31:23 +00:00
|
|
|
as well, with examples in the
|
2011-10-29 18:43:30 +00:00
|
|
|
`Resolver API documentation <../api/dyaml.resolver.html>`_.
|
2011-10-15 14:31:23 +00:00
|
|
|
|
|
|
|
Since a type can only have one representer function, we don't dump *Color* both
|
|
|
|
in the scalar and mapping formats we've used before. However, you can decide to
|
|
|
|
dump the node with different formats/tags in the representer function itself.
|
|
|
|
E.g. you could dump the Color as a mapping based on some arbitrary condition,
|
|
|
|
such as the color being white.
|
|
|
|
|
|
|
|
.. code-block:: d
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
We construct a new *Representer*, and specify a representer function for the
|
|
|
|
*Color* (the template argument) type. We also construct a *Resolver*, same as in
|
|
|
|
the previous section, so the ``!color`` tag will be implicit. Of course,
|
|
|
|
identical *Resolver* would then have to be used when loading the file.
|
|
|
|
You don't need to do this if you want the tag to be explicit.
|
|
|
|
|
|
|
|
We construct a *Dumper* to file ``output.yaml`` and pass the *Representer* and
|
|
|
|
*Resolver* to it. Then, we create a simple node containing a sequence of colors
|
|
|
|
and finally, we dump it.
|
|
|
|
|
|
|
|
Source code for this section can be found in the ``examples/representer``
|
|
|
|
directory of the D:YAML package.
|