diff --git a/doc/doctrees/environment.pickle b/doc/doctrees/environment.pickle index 9894b7d..5abd626 100644 Binary files a/doc/doctrees/environment.pickle and b/doc/doctrees/environment.pickle differ diff --git a/doc/doctrees/tutorials/custom_types.doctree b/doc/doctrees/tutorials/custom_types.doctree index 3fe2c1c..7518da8 100644 Binary files a/doc/doctrees/tutorials/custom_types.doctree and b/doc/doctrees/tutorials/custom_types.doctree differ diff --git a/doc/html/_sources/tutorials/custom_types.txt b/doc/html/_sources/tutorials/custom_types.txt index 9b3eaf5..65853ee 100644 --- a/doc/html/_sources/tutorials/custom_types.txt +++ b/doc/html/_sources/tutorials/custom_types.txt @@ -3,13 +3,13 @@ Custom YAML data types ====================== Sometimes you need to serialize complex data types such as classes. To do this -you could use plain nodes such as mappings with class data members. However, -YAML supports custom types with identifiers called *tags*. That is the topic of -this tutorial. +you could use plain nodes such as mappings with class data members. YAML also +supports custom types with identifiers called *tags*. That is the topic of this +tutorial. 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* -during parsing, so you don't need to specify tag for each float, integer, etc. +during parsing - you don't need to specify tag for each float, integer, etc. D:YAML can also implicitly resolve custom tags, as we will show later. @@ -24,7 +24,7 @@ the *addConstructorXXX()* methods, where *XXX* is *Scalar*, *Sequence* or *Mapping*. *Constructor* is then passed to *Loader*, which parses YAML input. Struct types have no specific requirements for YAML support. Class types should -define the *opEquals()* operator, as this is used in equality comparisons of +define the *opEquals()* operator - this is used in equality comparisons of nodes. Default class *opEquals()* compares references, which means two identical objects might be considered unequal. (Default struct *opEquals()* compares byte-by-byte, sometimes you might want to override that as well.) @@ -41,24 +41,26 @@ following struct: ubyte blue; } -First, we need a function to construct our data type. It must take two *Mark* -structs, which store position of the node in the file, and 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. 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: +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: .. code-block:: d - Color constructColorScalar(Mark start, Mark end, ref Node node) + Color constructColorScalar(ref Node node) { string value = node.as!string; if(value.length != 6) { - throw new ConstructorException("Invalid color: " ~ value, start, end); + throw new Exception("Invalid color: " ~ value); } //We don't need to check for uppercase chars this way. value = value.toLower(); @@ -68,7 +70,7 @@ of these functions: { if(!std.ascii.isHexDigit(c)) { - throw new ConstructorException("Invalid color: " ~ value, start, end); + throw new Exception("Invalid color: " ~ value); } if(std.ascii.isDigit(c)) @@ -86,21 +88,16 @@ of these functions: return result; } - Color constructColorMapping(Mark start, Mark end, ref Node node) + Color constructColorMapping(ref Node node) { ubyte r,g,b; //Might throw if a value is missing is not an integer, or is out of range. - try - { - r = node["r"].as!ubyte; - g = node["g"].as!ubyte; - b = node["b"].as!ubyte; - } - catch(NodeException e) - { - throw new ConstructorException("Invalid color: " ~ e.msg, start, end); - } + //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; return Color(cast(ubyte)r, cast(ubyte)g, cast(ubyte)b); } diff --git a/doc/html/api/dyaml.constructor.html b/doc/html/api/dyaml.constructor.html index b81d239..f05fd59 100644 --- a/doc/html/api/dyaml.constructor.html +++ b/doc/html/api/dyaml.constructor.html @@ -95,14 +95,20 @@ -
Add a constructor function from scalar.
-The function must take two Marks (start and end positions of - the node in file) and a reference to Node to construct from. +
The function must take a reference to Node to construct from.
The node contains a string for scalars, Node[] for sequences and
Node.Pair[] for mappings.
+
+
+ Any exception thrown by this function will be caught by D:YAML and
+ its message will be added to a YAMLException that will also tell the
+ user which type failed to construct, and position in the file.
+
+
The value returned by this function will be stored in the resulting node.
@@ -124,20 +130,13 @@
int x, y, z;
}
- MyStruct constructMyStructScalar(Mark start, Mark end, ref Node node)
+ MyStruct constructMyStructScalar(ref Node node)
{
//Guaranteed to be string as we construct from scalar.
//!mystruct x:y:z
auto parts = node.as!string().split(":");
- try
- {
- return MyStruct(to!int(parts[0]), to!int(parts[1]), to!int(parts[2]));
- }
- catch(Exception e)
- {
- throw new ConstructorException("Could not construct MyStruct: " ~ e.msg,
- start, end);
- }
+ //If this throws, the D:YAML will handle it and throw a YAMLException.
+ return MyStruct(to!int(parts[0]), to!int(parts[1]), to!int(parts[2]));
}
void main()
@@ -152,7 +151,7 @@
Add a constructor function from sequence.
@@ -169,19 +168,11 @@ int x, y, z; } - MyStruct constructMyStructSequence(Mark start, Mark end, ref Node node) + MyStruct constructMyStructSequence(ref Node node) { //node is guaranteed to be sequence. //!mystruct [x, y, z] - try - { - return MyStruct(node[0].as!int, node[1].as!int, node[2].as!int); - } - catch(NodeException e) - { - throw new ConstructorException("Could not construct MyStruct: " ~ e.msg, - start, end); - } + return MyStruct(node[0].as!int, node[1].as!int, node[2].as!int); } void main() @@ -196,7 +187,7 @@Add a constructor function from a mapping.
@@ -213,19 +204,11 @@ int x, y, z; } - MyStruct constructMyStructMapping(Mark start, Mark end, ref Node node) + MyStruct constructMyStructMapping(ref Node node) { //node is guaranteed to be mapping. //!mystruct {"x": x, "y": y, "z": z} - try - { - return MyStruct(node["x"].as!int, node["y"].as!int, node["z"].as!int); - } - catch(NodeException e) - { - throw new ConstructorException("Could not construct MyStruct: " ~ e.msg, - start, end); - } + return MyStruct(node["x"].as!int, node["y"].as!int, node["z"].as!int); } void main() @@ -242,72 +225,72 @@Construct a null node.
Construct a merge node - a node that merges another node into a mapping.
Construct a boolean node.
Construct an integer (long) node.
Construct a floating point (real) node.
Construct a binary (base64) node.
Construct a timestamp (SysTime) node.
Construct a string node.
Convert a sequence of single-element mappings into a sequence of pairs.
Construct an ordered map (ordered sequence of key:value pairs without duplicates) node.
Construct a pairs (ordered sequence of key: value pairs allowing duplicates) node.
Construct a set node.
Construct a sequence (array) node.
Construct an unordered map (unordered set of key: value pairs without duplicates) node.
diff --git a/doc/html/articles/spec_differences.html b/doc/html/articles/spec_differences.html index c859c39..eaff95f 100644 --- a/doc/html/articles/spec_differences.html +++ b/doc/html/articles/spec_differences.html @@ -138,7 +138,7 @@ struct appears in Phobos.