Added a shortcut alias called "as" for Node.get(), and replaced
get() with as() all over the code, tutorials, examples and docs. Fixed a bug in YAML benchmark makefile. Fixed a bug in autoddoc configuration.
This commit is contained in:
parent
fb67e775e4
commit
13ea5f0c24
33 changed files with 236 additions and 215 deletions
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -54,7 +54,7 @@ of these functions:
|
|||
|
||||
Color constructColorScalar(Mark start, Mark end, ref Node node)
|
||||
{
|
||||
string value = node.get!string;
|
||||
string value = node.as!string;
|
||||
|
||||
if(value.length != 6)
|
||||
{
|
||||
|
@ -93,9 +93,9 @@ of these functions:
|
|||
//Might throw if a value is missing is not an integer, or is out of range.
|
||||
try
|
||||
{
|
||||
r = node["r"].get!ubyte;
|
||||
g = node["g"].get!ubyte;
|
||||
b = node["b"].get!ubyte;
|
||||
r = node["r"].as!ubyte;
|
||||
g = node["g"].as!ubyte;
|
||||
b = node["b"].as!ubyte;
|
||||
}
|
||||
catch(NodeException e)
|
||||
{
|
||||
|
@ -143,10 +143,10 @@ Finally, the code to put it all together:
|
|||
|
||||
auto root = loader.load();
|
||||
|
||||
if(root["scalar-red"].get!Color == red &&
|
||||
root["mapping-red"].get!Color == red &&
|
||||
root["scalar-orange"].get!Color == orange &&
|
||||
root["mapping-orange"].get!Color == orange)
|
||||
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)
|
||||
{
|
||||
writeln("SUCCESS");
|
||||
return;
|
||||
|
@ -162,8 +162,8 @@ Finally, the code to put it all together:
|
|||
|
||||
First, we create a *Constructor* and pass functions to handle the ``!color``
|
||||
and ``!color-mapping`` tag. We construct a *Loader* and pass the *Constructor*
|
||||
to it. We then load the YAML document, and finally, read the colors using
|
||||
*get()* method to test if they were loaded as expected.
|
||||
to it. We then load the YAML document, and finally, read the colors to test if
|
||||
they were loaded as expected.
|
||||
|
||||
You can find the source code for what we've done so far in the
|
||||
``examples/constructor`` directory in the D:YAML package.
|
||||
|
@ -256,7 +256,7 @@ With the following code, we will add support for dumping the our Color type.
|
|||
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;
|
||||
Color color = node.as!Color;
|
||||
|
||||
static immutable hex = "0123456789ABCDEF";
|
||||
|
||||
|
|
|
@ -90,7 +90,7 @@ into the file:
|
|||
{
|
||||
writeln(word);
|
||||
}
|
||||
writeln("The answer is ", root["Answer"].get!int);
|
||||
writeln("The answer is ", root["Answer"].as!int);
|
||||
|
||||
//Dump the loaded document to output.yaml.
|
||||
Dumper("output.yaml").dump(root);
|
||||
|
@ -115,7 +115,7 @@ possible.
|
|||
mapping (associative array) or a scalar (value). Here the root node is a
|
||||
mapping, and we use the index operator to get subnodes with keys "Hello World"
|
||||
and "Answer". We iterate over the first, as it is a sequence, and use the
|
||||
*Node.get()* method on the second to get its value as an integer.
|
||||
*Node.as()* method on the second to read its value as an integer.
|
||||
|
||||
You can iterate over a mapping or sequence as if it was an associative or normal
|
||||
array. If you try to iterate over a scalar, it will throw a *YAMLException*.
|
||||
|
@ -128,9 +128,9 @@ not possible to convert to iterated type, a *YAMLException* is thrown. For
|
|||
instance, if we specified *int* here, we would get an error, as "Hello"
|
||||
cannot be converted to an integer.
|
||||
|
||||
The *Node.get()* method is used to get value of a scalar node, allowing to
|
||||
specify type. D:YAML will try to return the scalar as this type, converting if
|
||||
needed, throwing *YAMLException* if not possible.
|
||||
The *Node.as()* method is used to read value of a scalar node as specified type.
|
||||
D:YAML will try to return the scalar as this type, converting if needed,
|
||||
throwing *YAMLException* if not possible.
|
||||
|
||||
Finally we dump the document we just read to ``output.yaml`` with the
|
||||
*Dumper.dump()* method. *Dumper* is a struct used to dump YAML documents.
|
||||
|
|
|
@ -127,7 +127,7 @@
|
|||
{
|
||||
<span class="d_comment">//Guaranteed to be string as we construct from scalar.
|
||||
</span> <span class="d_comment">//!mystruct x:y:z
|
||||
</span> <span class="d_keyword">auto</span> parts = node.get!string().split(<span class="d_string">":"</span>);
|
||||
</span> <span class="d_keyword">auto</span> parts = node.as!string().split(<span class="d_string">":"</span>);
|
||||
<span class="d_keyword">try</span>
|
||||
{
|
||||
<span class="d_keyword">return</span> MyStruct(to!<span class="d_keyword">int</span>(parts[0]), to!<span class="d_keyword">int</span>(parts[1]), to!<span class="d_keyword">int</span>(parts[2]));
|
||||
|
@ -174,7 +174,7 @@
|
|||
</span> <span class="d_comment">//!mystruct [x, y, z]
|
||||
</span> <span class="d_keyword">try</span>
|
||||
{
|
||||
<span class="d_keyword">return</span> MyStruct(node[0].get!<span class="d_keyword">int</span>, node[1].get!<span class="d_keyword">int</span>, node[2].get!<span class="d_keyword">int</span>);
|
||||
<span class="d_keyword">return</span> MyStruct(node[0].as!<span class="d_keyword">int</span>, node[1].as!<span class="d_keyword">int</span>, node[2].as!<span class="d_keyword">int</span>);
|
||||
}
|
||||
<span class="d_keyword">catch</span>(NodeException e)
|
||||
{
|
||||
|
@ -218,7 +218,7 @@
|
|||
</span> <span class="d_comment">//!mystruct {"x": x, "y": y, "z": z}
|
||||
</span> <span class="d_keyword">try</span>
|
||||
{
|
||||
<span class="d_keyword">return</span> MyStruct(node[<span class="d_string">"x"</span>].get!<span class="d_keyword">int</span>, node[<span class="d_string">"y"</span>].get!<span class="d_keyword">int</span>, node[<span class="d_string">"z"</span>].get!<span class="d_keyword">int</span>);
|
||||
<span class="d_keyword">return</span> MyStruct(node[<span class="d_string">"x"</span>].as!<span class="d_keyword">int</span>, node[<span class="d_string">"y"</span>].as!<span class="d_keyword">int</span>, node[<span class="d_string">"z"</span>].as!<span class="d_keyword">int</span>);
|
||||
}
|
||||
<span class="d_keyword">catch</span>(NodeException e)
|
||||
{
|
||||
|
|
|
@ -189,9 +189,11 @@
|
|||
</table></div>
|
||||
<p><b>Example:</b><br>
|
||||
<pre class="d_code"> Dumper dumper = Dumper(<span class="d_string">"file.yaml"</span>);
|
||||
string[string] directives;
|
||||
directives[<span class="d_string">"!short!"</span>] = <span class="d_string">"tag:long.org,2011:"</span>;
|
||||
<span class="d_comment">//This will emit tags starting with "tag:long.org,2011"
|
||||
</span> <span class="d_comment">//with a "!short!" prefix instead.
|
||||
</span> dumper.<span class="d_param">tags</span>(<span class="d_string">"short"</span>, <span class="d_string">"tag:long.org,2011:"</span>);
|
||||
</span> dumper.<span class="d_psymbol">tagDirectives</span>(directives);
|
||||
dumper.dump(Node(<span class="d_string">"foo"</span>));
|
||||
</pre>
|
||||
</p>
|
||||
|
|
|
@ -149,6 +149,12 @@
|
|||
<dt class="d_decl">Node[] <a name="loadAll"></a><span class="ddoc_psymbol">loadAll</span>();
|
||||
</dt>
|
||||
<dd><p>Load all YAML documents.
|
||||
</p>
|
||||
<p>This is just a shortcut that iterates over all documents and returns
|
||||
them all at once. Calling <a name="loadAll"></a><span class="ddoc_psymbol">loadAll</span> after iterating over the node or
|
||||
vice versa will not return any documents, as they have all been parsed
|
||||
already.
|
||||
|
||||
</p>
|
||||
<b>Returns:</b><div class="pbr">Array of root nodes of all documents in the file/stream.
|
||||
|
||||
|
|
|
@ -246,6 +246,11 @@
|
|||
</dt>
|
||||
<dd><p>Is this node a user defined type?</p>
|
||||
|
||||
</dd>
|
||||
<dt class="d_decl">const @property string <a name="tag"></a><span class="ddoc_psymbol">tag</span>();
|
||||
</dt>
|
||||
<dd><p>Return <a name="tag"></a><span class="ddoc_psymbol">tag</span> of the node.</p>
|
||||
|
||||
</dd>
|
||||
<dt class="d_decl">bool <a name="opEquals"></a><span class="ddoc_psymbol">opEquals</span>(T)(ref T <b>rhs</b>);
|
||||
</dt>
|
||||
|
@ -271,6 +276,11 @@
|
|||
</table></div>
|
||||
<b>Returns:</b><div class="pbr"><b>true</b> if equal, <b>false</b> otherwise.</div>
|
||||
|
||||
</dd>
|
||||
<dt class="d_decl">alias <a name="as"></a><span class="ddoc_psymbol">as</span>;
|
||||
</dt>
|
||||
<dd><p>Shortcut for get().</p>
|
||||
|
||||
</dd>
|
||||
<dt class="d_decl">T <a name="get"></a><span class="ddoc_psymbol">get</span>(T)();
|
||||
</dt>
|
||||
|
@ -303,9 +313,9 @@
|
|||
<b>Examples:</b><div class="pbr">Automatic type conversion:
|
||||
<pre class="d_code"> <span class="d_keyword">auto</span> node = Node(42);
|
||||
|
||||
<span class="d_keyword">assert</span>(node.<span class="d_psymbol">get</span>!<span class="d_keyword">int</span> == 42);
|
||||
<span class="d_keyword">assert</span>(node.<span class="d_psymbol">get</span>!string == <span class="d_string">"42"</span>);
|
||||
<span class="d_keyword">assert</span>(node.<span class="d_psymbol">get</span>!<span class="d_keyword">double</span> == 42.0);
|
||||
<span class="d_keyword">assert</span>(node.as!<span class="d_keyword">int</span> == 42);
|
||||
<span class="d_keyword">assert</span>(node.as!string == <span class="d_string">"42"</span>);
|
||||
<span class="d_keyword">assert</span>(node.as!<span class="d_keyword">double</span> == 42.0);
|
||||
</pre>
|
||||
|
||||
</div>
|
||||
|
|
|
@ -95,7 +95,7 @@
|
|||
Node representMyStruct(<span class="d_keyword">ref</span> Node node, Representer representer)
|
||||
{
|
||||
<span class="d_comment">//The node is guaranteed to be MyStruct as we add representer for MyStruct.
|
||||
</span> <span class="d_keyword">auto</span> value = node.get!MyStruct;
|
||||
</span> <span class="d_keyword">auto</span> value = node.as!MyStruct;
|
||||
<span class="d_comment">//Using custom scalar format, x:y:z.
|
||||
</span> <span class="d_keyword">auto</span> scalar = format(value.x, <span class="d_string">":"</span>, value.y, <span class="d_string">":"</span>, value.z);
|
||||
<span class="d_comment">//Representing as a scalar, with custom tag to specify this data type.
|
||||
|
@ -136,7 +136,7 @@
|
|||
<span class="d_keyword">return</span> x == t.x && y == t.y && z == t.z;
|
||||
}
|
||||
|
||||
<span class="d_comment">///Useful for Node.get!string .
|
||||
<span class="d_comment">///Useful for Node.as!string .
|
||||
</span> <span class="d_keyword">override</span> string toString()
|
||||
{
|
||||
<span class="d_keyword">return</span> format(<span class="d_string">"MyClass("), x, <span class="d_string">", "</span>, y, <span class="d_string">", "</span>, z, <span class="d_string">"</span>"</span>);
|
||||
|
@ -147,7 +147,7 @@
|
|||
</span> Node representMyClass(<span class="d_keyword">ref</span> Node node, Representer representer)
|
||||
{
|
||||
<span class="d_comment">//The node is guaranteed to be MyClass as we add representer for MyClass.
|
||||
</span> <span class="d_keyword">auto</span> value = node.get!MyClass;
|
||||
</span> <span class="d_keyword">auto</span> value = node.as!MyClass;
|
||||
<span class="d_comment">//Using custom scalar format, x:y:z.
|
||||
</span> <span class="d_keyword">auto</span> scalar = format(value.x, <span class="d_string">":"</span>, value.y, <span class="d_string">":"</span>, value.z);
|
||||
<span class="d_comment">//Representing as a scalar, with custom tag to specify this data type.
|
||||
|
@ -189,7 +189,7 @@
|
|||
|
||||
Node representMyStruct(<span class="d_keyword">ref</span> Node node, Representer representer)
|
||||
{
|
||||
<span class="d_keyword">auto</span> value = node.get!MyStruct;
|
||||
<span class="d_keyword">auto</span> value = node.as!MyStruct;
|
||||
<span class="d_keyword">auto</span> <span class="d_param">scalar</span> = format(value.x, <span class="d_string">":"</span>, value.y, <span class="d_string">":"</span>, value.z);
|
||||
<span class="d_keyword">return</span> representer.<span class="d_psymbol">representScalar</span>(<span class="d_string">"!mystruct.tag"</span>, <span class="d_param">scalar</span>);
|
||||
}
|
||||
|
@ -223,7 +223,7 @@
|
|||
|
||||
Node representMyStruct(<span class="d_keyword">ref</span> Node node, Representer representer)
|
||||
{
|
||||
<span class="d_keyword">auto</span> value = node.get!MyStruct;
|
||||
<span class="d_keyword">auto</span> value = node.as!MyStruct;
|
||||
<span class="d_keyword">auto</span> nodes = [Node(value.x), Node(value.y), Node(value.z)];
|
||||
<span class="d_keyword">return</span> representer.<span class="d_psymbol">representSequence</span>(<span class="d_string">"!mystruct.tag"</span>, nodes);
|
||||
}
|
||||
|
@ -257,7 +257,7 @@
|
|||
|
||||
Node representMyStruct(<span class="d_keyword">ref</span> Node node, Representer representer)
|
||||
{
|
||||
<span class="d_keyword">auto</span> value = node.get!MyStruct;
|
||||
<span class="d_keyword">auto</span> value = node.as!MyStruct;
|
||||
<span class="d_keyword">auto</span> <span class="d_param">pairs</span> = [Node.Pair(<span class="d_string">"x"</span>, value.x),
|
||||
Node.Pair(<span class="d_string">"y"</span>, value.y),
|
||||
Node.Pair(<span class="d_string">"z"</span>, value.z)];
|
||||
|
|
|
@ -138,7 +138,7 @@ struct appears in Phobos.</p>
|
|||
</div>
|
||||
<div class="footer">
|
||||
© Copyright 2011, Ferdinand Majerech. Based on PyYAML http://www.pyyaml.org by Kirill Simonov.
|
||||
Last updated on Oct 18, 2011.
|
||||
Last updated on Oct 22, 2011.
|
||||
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.7.
|
||||
</div>
|
||||
</body>
|
||||
|
|
|
@ -104,7 +104,7 @@
|
|||
</div>
|
||||
<div class="footer">
|
||||
© Copyright 2011, Ferdinand Majerech. Based on PyYAML http://www.pyyaml.org by Kirill Simonov.
|
||||
Last updated on Oct 18, 2011.
|
||||
Last updated on Oct 22, 2011.
|
||||
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.7.
|
||||
</div>
|
||||
</body>
|
||||
|
|
|
@ -87,7 +87,7 @@
|
|||
</div>
|
||||
<div class="footer">
|
||||
© Copyright 2011, Ferdinand Majerech. Based on PyYAML http://www.pyyaml.org by Kirill Simonov.
|
||||
Last updated on Oct 18, 2011.
|
||||
Last updated on Oct 22, 2011.
|
||||
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.7.
|
||||
</div>
|
||||
</body>
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -87,7 +87,7 @@ from a mapping, where we use the following format: {r:RRR, g:GGG, b:BBB} . Code
|
|||
of these functions:</p>
|
||||
<div class="highlight-d"><div class="highlight"><pre><span class="n">Color</span> <span class="n">constructColorScalar</span><span class="p">(</span><span class="n">Mark</span> <span class="n">start</span><span class="p">,</span> <span class="n">Mark</span> <span class="n">end</span><span class="p">,</span> <span class="k">ref</span> <span class="n">Node</span> <span class="n">node</span><span class="p">)</span>
|
||||
<span class="p">{</span>
|
||||
<span class="nb">string</span> <span class="n">value</span> <span class="p">=</span> <span class="n">node</span><span class="p">.</span><span class="n">get</span><span class="p">!</span><span class="nb">string</span><span class="p">;</span>
|
||||
<span class="nb">string</span> <span class="n">value</span> <span class="p">=</span> <span class="n">node</span><span class="p">.</span><span class="n">as</span><span class="p">!</span><span class="nb">string</span><span class="p">;</span>
|
||||
|
||||
<span class="k">if</span><span class="p">(</span><span class="n">value</span><span class="p">.</span><span class="n">length</span> <span class="p">!=</span> <span class="mi">6</span><span class="p">)</span>
|
||||
<span class="p">{</span>
|
||||
|
@ -126,9 +126,9 @@ of these functions:</p>
|
|||
<span class="c1">//Might throw if a value is missing is not an integer, or is out of range.</span>
|
||||
<span class="k">try</span>
|
||||
<span class="p">{</span>
|
||||
<span class="n">r</span> <span class="p">=</span> <span class="n">node</span><span class="p">[</span><span class="s">"r"</span><span class="p">].</span><span class="n">get</span><span class="p">!</span><span class="kt">ubyte</span><span class="p">;</span>
|
||||
<span class="n">g</span> <span class="p">=</span> <span class="n">node</span><span class="p">[</span><span class="s">"g"</span><span class="p">].</span><span class="n">get</span><span class="p">!</span><span class="kt">ubyte</span><span class="p">;</span>
|
||||
<span class="n">b</span> <span class="p">=</span> <span class="n">node</span><span class="p">[</span><span class="s">"b"</span><span class="p">].</span><span class="n">get</span><span class="p">!</span><span class="kt">ubyte</span><span class="p">;</span>
|
||||
<span class="n">r</span> <span class="p">=</span> <span class="n">node</span><span class="p">[</span><span class="s">"r"</span><span class="p">].</span><span class="n">as</span><span class="p">!</span><span class="kt">ubyte</span><span class="p">;</span>
|
||||
<span class="n">g</span> <span class="p">=</span> <span class="n">node</span><span class="p">[</span><span class="s">"g"</span><span class="p">].</span><span class="n">as</span><span class="p">!</span><span class="kt">ubyte</span><span class="p">;</span>
|
||||
<span class="n">b</span> <span class="p">=</span> <span class="n">node</span><span class="p">[</span><span class="s">"b"</span><span class="p">].</span><span class="n">as</span><span class="p">!</span><span class="kt">ubyte</span><span class="p">;</span>
|
||||
<span class="p">}</span>
|
||||
<span class="k">catch</span><span class="p">(</span><span class="n">NodeException</span> <span class="n">e</span><span class="p">)</span>
|
||||
<span class="p">{</span>
|
||||
|
@ -171,10 +171,10 @@ of these functions:</p>
|
|||
|
||||
<span class="k">auto</span> <span class="n">root</span> <span class="p">=</span> <span class="n">loader</span><span class="p">.</span><span class="n">load</span><span class="p">();</span>
|
||||
|
||||
<span class="k">if</span><span class="p">(</span><span class="n">root</span><span class="p">[</span><span class="s">"scalar-red"</span><span class="p">].</span><span class="n">get</span><span class="p">!</span><span class="n">Color</span> <span class="p">==</span> <span class="n">red</span> <span class="p">&&</span>
|
||||
<span class="n">root</span><span class="p">[</span><span class="s">"mapping-red"</span><span class="p">].</span><span class="n">get</span><span class="p">!</span><span class="n">Color</span> <span class="p">==</span> <span class="n">red</span> <span class="p">&&</span>
|
||||
<span class="n">root</span><span class="p">[</span><span class="s">"scalar-orange"</span><span class="p">].</span><span class="n">get</span><span class="p">!</span><span class="n">Color</span> <span class="p">==</span> <span class="n">orange</span> <span class="p">&&</span>
|
||||
<span class="n">root</span><span class="p">[</span><span class="s">"mapping-orange"</span><span class="p">].</span><span class="n">get</span><span class="p">!</span><span class="n">Color</span> <span class="p">==</span> <span class="n">orange</span><span class="p">)</span>
|
||||
<span class="k">if</span><span class="p">(</span><span class="n">root</span><span class="p">[</span><span class="s">"scalar-red"</span><span class="p">].</span><span class="n">as</span><span class="p">!</span><span class="n">Color</span> <span class="p">==</span> <span class="n">red</span> <span class="p">&&</span>
|
||||
<span class="n">root</span><span class="p">[</span><span class="s">"mapping-red"</span><span class="p">].</span><span class="n">as</span><span class="p">!</span><span class="n">Color</span> <span class="p">==</span> <span class="n">red</span> <span class="p">&&</span>
|
||||
<span class="n">root</span><span class="p">[</span><span class="s">"scalar-orange"</span><span class="p">].</span><span class="n">as</span><span class="p">!</span><span class="n">Color</span> <span class="p">==</span> <span class="n">orange</span> <span class="p">&&</span>
|
||||
<span class="n">root</span><span class="p">[</span><span class="s">"mapping-orange"</span><span class="p">].</span><span class="n">as</span><span class="p">!</span><span class="n">Color</span> <span class="p">==</span> <span class="n">orange</span><span class="p">)</span>
|
||||
<span class="p">{</span>
|
||||
<span class="n">writeln</span><span class="p">(</span><span class="s">"SUCCESS"</span><span class="p">);</span>
|
||||
<span class="k">return</span><span class="p">;</span>
|
||||
|
@ -191,8 +191,8 @@ of these functions:</p>
|
|||
</div>
|
||||
<p>First, we create a <em>Constructor</em> and pass functions to handle the <tt class="docutils literal"><span class="pre">!color</span></tt>
|
||||
and <tt class="docutils literal"><span class="pre">!color-mapping</span></tt> tag. We construct a <em>Loader</em> and pass the <em>Constructor</em>
|
||||
to it. We then load the YAML document, and finally, read the colors using
|
||||
<em>get()</em> method to test if they were loaded as expected.</p>
|
||||
to it. We then load the YAML document, and finally, read the colors to test if
|
||||
they were loaded as expected.</p>
|
||||
<p>You can find the source code for what we’ve done so far in the
|
||||
<tt class="docutils literal"><span class="pre">examples/constructor</span></tt> directory in the D:YAML package.</p>
|
||||
</div>
|
||||
|
@ -263,7 +263,7 @@ loading the output.</p>
|
|||
<div class="highlight-d"><div class="highlight"><pre><span class="n">Node</span> <span class="n">representColor</span><span class="p">(</span><span class="k">ref</span> <span class="n">Node</span> <span class="n">node</span><span class="p">,</span> <span class="n">Representer</span> <span class="n">representer</span><span class="p">)</span>
|
||||
<span class="p">{</span>
|
||||
<span class="c1">//The node is guaranteed to be Color as we add representer for Color.</span>
|
||||
<span class="n">Color</span> <span class="n">color</span> <span class="p">=</span> <span class="n">node</span><span class="p">.</span><span class="n">get</span><span class="p">!</span><span class="n">Color</span><span class="p">;</span>
|
||||
<span class="n">Color</span> <span class="n">color</span> <span class="p">=</span> <span class="n">node</span><span class="p">.</span><span class="n">as</span><span class="p">!</span><span class="n">Color</span><span class="p">;</span>
|
||||
|
||||
<span class="k">static</span> <span class="n">immutable</span> <span class="n">hex</span> <span class="p">=</span> <span class="s">"0123456789ABCDEF"</span><span class="p">;</span>
|
||||
|
||||
|
@ -369,7 +369,7 @@ directory of the D:YAML package.</p>
|
|||
</div>
|
||||
<div class="footer">
|
||||
© Copyright 2011, Ferdinand Majerech. Based on PyYAML http://www.pyyaml.org by Kirill Simonov.
|
||||
Last updated on Oct 18, 2011.
|
||||
Last updated on Oct 22, 2011.
|
||||
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.7.
|
||||
</div>
|
||||
</body>
|
||||
|
|
|
@ -116,7 +116,7 @@ into the file:</p>
|
|||
<span class="p">{</span>
|
||||
<span class="n">writeln</span><span class="p">(</span><span class="n">word</span><span class="p">);</span>
|
||||
<span class="p">}</span>
|
||||
<span class="n">writeln</span><span class="p">(</span><span class="s">"The answer is "</span><span class="p">,</span> <span class="n">root</span><span class="p">[</span><span class="s">"Answer"</span><span class="p">].</span><span class="n">get</span><span class="p">!</span><span class="kt">int</span><span class="p">);</span>
|
||||
<span class="n">writeln</span><span class="p">(</span><span class="s">"The answer is "</span><span class="p">,</span> <span class="n">root</span><span class="p">[</span><span class="s">"Answer"</span><span class="p">].</span><span class="n">as</span><span class="p">!</span><span class="kt">int</span><span class="p">);</span>
|
||||
|
||||
<span class="c1">//Dump the loaded document to output.yaml.</span>
|
||||
<span class="n">Dumper</span><span class="p">(</span><span class="s">"output.yaml"</span><span class="p">).</span><span class="n">dump</span><span class="p">(</span><span class="n">root</span><span class="p">);</span>
|
||||
|
@ -137,7 +137,7 @@ possible.</p>
|
|||
mapping (associative array) or a scalar (value). Here the root node is a
|
||||
mapping, and we use the index operator to get subnodes with keys “Hello World”
|
||||
and “Answer”. We iterate over the first, as it is a sequence, and use the
|
||||
<em>Node.get()</em> method on the second to get its value as an integer.</p>
|
||||
<em>Node.as()</em> method on the second to read its value as an integer.</p>
|
||||
<p>You can iterate over a mapping or sequence as if it was an associative or normal
|
||||
array. If you try to iterate over a scalar, it will throw a <em>YAMLException</em>.</p>
|
||||
<p>You can iterate over subnodes using <em>Node</em> as the iterated type, or specify
|
||||
|
@ -147,9 +147,9 @@ so we iterate over the “Hello World” sequence as an array of strings
|
|||
not possible to convert to iterated type, a <em>YAMLException</em> is thrown. For
|
||||
instance, if we specified <em>int</em> here, we would get an error, as “Hello”
|
||||
cannot be converted to an integer.</p>
|
||||
<p>The <em>Node.get()</em> method is used to get value of a scalar node, allowing to
|
||||
specify type. D:YAML will try to return the scalar as this type, converting if
|
||||
needed, throwing <em>YAMLException</em> if not possible.</p>
|
||||
<p>The <em>Node.as()</em> method is used to read value of a scalar node as specified type.
|
||||
D:YAML will try to return the scalar as this type, converting if needed,
|
||||
throwing <em>YAMLException</em> if not possible.</p>
|
||||
<p>Finally we dump the document we just read to <tt class="docutils literal"><span class="pre">output.yaml</span></tt> with the
|
||||
<em>Dumper.dump()</em> method. <em>Dumper</em> is a struct used to dump YAML documents.
|
||||
The <em>dump()</em> method writes one or more documents to a file, throwing
|
||||
|
@ -237,7 +237,7 @@ example in the <tt class="docutils literal"><span class="pre">example/getting_st
|
|||
</div>
|
||||
<div class="footer">
|
||||
© Copyright 2011, Ferdinand Majerech. Based on PyYAML http://www.pyyaml.org by Kirill Simonov.
|
||||
Last updated on Oct 18, 2011.
|
||||
Last updated on Oct 22, 2011.
|
||||
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.7.
|
||||
</div>
|
||||
</body>
|
||||
|
|
|
@ -330,7 +330,7 @@ Some of these might change in the future (especially !!map and !!set).</p>
|
|||
</div>
|
||||
<div class="footer">
|
||||
© Copyright 2011, Ferdinand Majerech. Based on PyYAML http://www.pyyaml.org by Kirill Simonov.
|
||||
Last updated on Oct 18, 2011.
|
||||
Last updated on Oct 22, 2011.
|
||||
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.7.
|
||||
</div>
|
||||
</body>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue