NimYAML/index.html

224 lines
23 KiB
HTML
Raw Normal View History

2016-01-05 23:17:39 +01:00
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>NimYAML - NimYAML</title>
<link href="docutils.css" rel="stylesheet" type="text/css"/>
<link href="style.css" rel="stylesheet" type="text/css"/>
<link href='http://fonts.googleapis.com/css?family=Raleway:400,600,900' rel='stylesheet' type='text/css'/>
<link href='http://fonts.googleapis.com/css?family=Source+Code+Pro:400,500,600' rel='stylesheet' type='text/css'/>
</head>
<body>
2016-02-25 22:45:48 +01:00
<a href="https://github.com/flyx/NimYAML"><img style="position: fixed; top: 0; right: 0; border: 0; z-index: 10;" src="https://camo.githubusercontent.com/652c5b9acfaddf3a9c326fa6bde407b87f7be0f4/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f6f72616e67655f6666373630302e706e67" alt="Fork me on GitHub" data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_right_orange_ff7600.png"></a>
<header>
2016-02-25 22:45:48 +01:00
<a class="pagetitle" href="index.html">NimYAML</a>
<a href="index.html">Home</a>
<a href="testing.html">Testing Ground</a>
2016-02-15 22:56:36 +01:00
<span>Docs:</span>
2016-02-15 19:51:05 +01:00
<a href="api.html">Overview</a>
2016-02-15 22:56:36 +01:00
<a href="serialization.html">Serialization</a>
<a href="yaml.html">Module yaml</a>
</header>
<article id="documentId">
<div class="container">
<h1 class="title">NimYAML</h1>
2016-02-12 20:46:01 +01:00
<h1 id="introduction">Introduction</h1><p><strong>NimYAML</strong> is a pure YAML implementation for Nim. It is able to read from and write to YAML character streams, and to serialize from and construct to native Nim types. It exclusively supports <a class="reference external" href="#http://www.yaml.org/spec/1.2/spec.html">YAML 1.2</a>.</p>
2016-02-16 20:45:09 +01:00
<p>Source code can be found on <a class="reference external" href="https://github.com/flyx/NimYAML">GitHub</a>. You can install it with <a class="reference external" href="https://github.com/nim-lang/nimble">Nimble</a>:</p>
2016-03-20 23:00:15 +01:00
<pre class = "listing">nimble install nimyaml</pre>
<h1 id="quickstart">Quickstart</h1>
<h2 id="dumping-nim-objects-as-yaml">Dumping Nim objects as YAML</h2><table class="quickstart-example"><thead><tr><th>code.nim</th>
<th>out.yaml</th></tr></thead><tbody><tr><td><pre class = "listing"><span class="Keyword">import</span> <span class="Identifier">yaml</span>
<span class="Keyword">type</span> <span class="Identifier">Person</span> <span class="Operator">=</span> <span class="Keyword">object</span>
<span class="Identifier">name</span> <span class="Punctuation">:</span> <span class="Identifier">string</span>
<span class="Identifier">age</span> <span class="Punctuation">:</span> <span class="Identifier">int32</span>
<span class="Keyword">var</span> <span class="Identifier">personList</span> <span class="Operator">=</span> <span class="Identifier">newSeq</span><span class="Punctuation">[</span><span class="Identifier">Person</span><span class="Punctuation">]</span><span class="Punctuation">(</span><span class="Punctuation">)</span>
<span class="Identifier">personList</span><span class="Operator">.</span><span class="Identifier">add</span><span class="Punctuation">(</span><span class="Identifier">Person</span><span class="Punctuation">(</span><span class="Identifier">name</span><span class="Punctuation">:</span> <span class="StringLit">&quot;Karl Koch&quot;</span><span class="Punctuation">,</span> <span class="Identifier">age</span><span class="Punctuation">:</span> <span class="DecNumber">23</span><span class="Punctuation">)</span><span class="Punctuation">)</span>
<span class="Identifier">personList</span><span class="Operator">.</span><span class="Identifier">add</span><span class="Punctuation">(</span><span class="Identifier">Person</span><span class="Punctuation">(</span><span class="Identifier">name</span><span class="Punctuation">:</span> <span class="StringLit">&quot;Peter Pan&quot;</span><span class="Punctuation">,</span> <span class="Identifier">age</span><span class="Punctuation">:</span> <span class="DecNumber">12</span><span class="Punctuation">)</span><span class="Punctuation">)</span>
<span class="Keyword">var</span> <span class="Identifier">s</span> <span class="Operator">=</span> <span class="Identifier">newFileStream</span><span class="Punctuation">(</span><span class="StringLit">&quot;out.yaml&quot;</span><span class="Punctuation">,</span> <span class="Identifier">fmWrite</span><span class="Punctuation">)</span>
<span class="Identifier">dump</span><span class="Punctuation">(</span><span class="Identifier">personList</span><span class="Punctuation">,</span> <span class="Identifier">s</span><span class="Punctuation">)</span>
<span class="Identifier">s</span><span class="Operator">.</span><span class="Identifier">close</span><span class="Punctuation">(</span><span class="Punctuation">)</span></pre></td>
<td><pre class = "listing">%YAML 1.2
--- !nim:system:seq(nim:custom:Person)
-
name: Karl Koch
age: 23
2016-02-12 20:46:01 +01:00
-
2016-03-20 23:00:15 +01:00
name: Peter Pan
age: 12</pre></td></tr></tbody></table>
<h2 id="loading-nim-objects-from-yaml">Loading Nim objects from YAML</h2><table class="quickstart-example"><thead><tr><th>code.nim</th>
<th>in.yaml</th></tr></thead><tbody><tr><td><pre class = "listing"><span class="Keyword">import</span> <span class="Identifier">yaml</span>
<span class="Keyword">type</span> <span class="Identifier">Person</span> <span class="Operator">=</span> <span class="Keyword">object</span>
<span class="Identifier">name</span> <span class="Punctuation">:</span> <span class="Identifier">string</span>
<span class="Identifier">age</span> <span class="Punctuation">:</span> <span class="Identifier">int32</span>
<span class="Keyword">var</span> <span class="Identifier">personList</span><span class="Punctuation">:</span> <span class="Identifier">seq</span><span class="Punctuation">[</span><span class="Identifier">Person</span><span class="Punctuation">]</span>
<span class="Keyword">var</span> <span class="Identifier">s</span> <span class="Operator">=</span> <span class="Identifier">newFileStream</span><span class="Punctuation">(</span><span class="StringLit">&quot;in.yaml&quot;</span><span class="Punctuation">)</span>
<span class="Identifier">load</span><span class="Punctuation">(</span><span class="Identifier">s</span><span class="Punctuation">,</span> <span class="Identifier">personList</span><span class="Punctuation">)</span>
<span class="Identifier">s</span><span class="Operator">.</span><span class="Identifier">close</span><span class="Punctuation">(</span><span class="Punctuation">)</span></pre></td>
<td><pre class = "listing">%YAML 1.2
---
- { name: Karl Koch, age: 23 }
- { name: Peter Pan, age: 12 }</pre></td></tr></tbody></table>
<h2 id="customizing-output-style">Customizing output style</h2><table class="quickstart-example"><thead><tr><th>code.nim</th>
<th>out.yaml</th></tr></thead><tbody><tr><td><pre class = "listing"><span class="Keyword">import</span> <span class="Identifier">yaml</span>
<span class="Keyword">type</span> <span class="Identifier">Person</span> <span class="Operator">=</span> <span class="Keyword">object</span>
<span class="Identifier">name</span><span class="Punctuation">:</span> <span class="Identifier">string</span>
<span class="Identifier">age</span><span class="Punctuation">:</span> <span class="Identifier">int32</span>
<span class="Keyword">var</span> <span class="Identifier">personList</span><span class="Punctuation">:</span> <span class="Identifier">seq</span><span class="Punctuation">[</span><span class="Identifier">Person</span><span class="Punctuation">]</span>
<span class="Identifier">personList</span><span class="Operator">.</span><span class="Identifier">add</span><span class="Punctuation">(</span><span class="Identifier">Person</span><span class="Punctuation">(</span><span class="Identifier">name</span><span class="Punctuation">:</span> <span class="StringLit">&quot;Karl Koch&quot;</span><span class="Punctuation">,</span> <span class="Identifier">age</span><span class="Punctuation">:</span> <span class="DecNumber">23</span><span class="Punctuation">)</span><span class="Punctuation">)</span>
<span class="Identifier">personList</span><span class="Operator">.</span><span class="Identifier">add</span><span class="Punctuation">(</span><span class="Identifier">Person</span><span class="Punctuation">(</span><span class="Identifier">name</span><span class="Punctuation">:</span> <span class="StringLit">&quot;Peter Pan&quot;</span><span class="Punctuation">,</span> <span class="Identifier">age</span><span class="Punctuation">:</span> <span class="DecNumber">12</span><span class="Punctuation">)</span><span class="Punctuation">)</span>
<span class="Keyword">var</span> <span class="Identifier">s</span> <span class="Operator">=</span> <span class="Identifier">newFileStream</span><span class="Punctuation">(</span><span class="StringLit">&quot;out.yaml&quot;</span><span class="Punctuation">)</span>
<span class="Identifier">dump</span><span class="Punctuation">(</span><span class="Identifier">personList</span><span class="Punctuation">,</span> <span class="Identifier">s</span><span class="Punctuation">,</span> <span class="Identifier">options</span> <span class="Operator">=</span> <span class="Identifier">defineOptions</span><span class="Punctuation">(</span>
<span class="Identifier">style</span> <span class="Operator">=</span> <span class="Identifier">psCanonical</span><span class="Punctuation">,</span>
<span class="Identifier">indentationStep</span> <span class="Operator">=</span> <span class="DecNumber">3</span><span class="Punctuation">,</span>
<span class="Identifier">newlines</span> <span class="Operator">=</span> <span class="Identifier">nlLF</span><span class="Punctuation">,</span>
<span class="Identifier">outputVersion</span> <span class="Operator">=</span> <span class="Identifier">ov1_1</span><span class="Punctuation">)</span><span class="Punctuation">)</span>
<span class="Identifier">s</span><span class="Operator">.</span><span class="Identifier">close</span><span class="Punctuation">(</span><span class="Punctuation">)</span></pre></td><td><pre class = "listing">%YAML 1.1
--- !nim:system:seq(nim:custom:Person)
[
!nim:custom:Person {
? !!str &quot;name&quot;
: !!str &quot;Karl Koch&quot;,
? !!str &quot;age&quot;
: !nim:system:int32 &quot;23&quot;
},
!nim:custom:Person {
? !!str &quot;name&quot;
: !!str &quot;Peter Pan&quot;,
? !!str &quot;age&quot;
: !nim:system:int32 &quot;12&quot;
}
]</pre></td></tr></tbody></table>
<h2 id="dumping-reference-types-and-cyclic-structures">Dumping reference types and cyclic structures</h2><table class="quickstart-example"><thead><tr><th>code.nim</th>
<th>out.yaml</th></tr></thead><tbody><tr><td><pre class = "listing"><span class="Keyword">import</span> <span class="Identifier">yaml</span>
<span class="Keyword">type</span>
<span class="Identifier">Node</span> <span class="Operator">=</span> <span class="Keyword">ref</span> <span class="Identifier">NodeObj</span>
<span class="Identifier">NodeObj</span> <span class="Operator">=</span> <span class="Keyword">object</span>
<span class="Identifier">name</span><span class="Punctuation">:</span> <span class="Identifier">string</span>
<span class="Identifier">left</span><span class="Punctuation">,</span> <span class="Identifier">right</span><span class="Punctuation">:</span> <span class="Identifier">Node</span>
<span class="Keyword">var</span> <span class="Identifier">node1</span><span class="Punctuation">,</span> <span class="Identifier">node2</span><span class="Punctuation">,</span> <span class="Identifier">node3</span><span class="Punctuation">:</span> <span class="Identifier">Node</span>
<span class="Identifier">new</span><span class="Punctuation">(</span><span class="Identifier">node1</span><span class="Punctuation">)</span><span class="Punctuation">;</span> <span class="Identifier">new</span><span class="Punctuation">(</span><span class="Identifier">node2</span><span class="Punctuation">)</span><span class="Punctuation">;</span> <span class="Identifier">new</span><span class="Punctuation">(</span><span class="Identifier">node3</span><span class="Punctuation">)</span>
<span class="Identifier">node1</span><span class="Operator">.</span><span class="Identifier">name</span> <span class="Operator">=</span> <span class="StringLit">&quot;Node 1&quot;</span>
<span class="Identifier">node2</span><span class="Operator">.</span><span class="Identifier">name</span> <span class="Operator">=</span> <span class="StringLit">&quot;Node 2&quot;</span>
<span class="Identifier">node3</span><span class="Operator">.</span><span class="Identifier">name</span> <span class="Operator">=</span> <span class="StringLit">&quot;Node 3&quot;</span>
<span class="Identifier">node1</span><span class="Operator">.</span><span class="Identifier">left</span> <span class="Operator">=</span> <span class="Identifier">node2</span>
<span class="Identifier">node1</span><span class="Operator">.</span><span class="Identifier">right</span> <span class="Operator">=</span> <span class="Identifier">node3</span>
<span class="Identifier">node2</span><span class="Operator">.</span><span class="Identifier">right</span> <span class="Operator">=</span> <span class="Identifier">node3</span>
<span class="Identifier">node3</span><span class="Operator">.</span><span class="Identifier">left</span> <span class="Operator">=</span> <span class="Identifier">node1</span>
<span class="Keyword">var</span> <span class="Identifier">s</span> <span class="Operator">=</span> <span class="Identifier">newFileStream</span><span class="Punctuation">(</span><span class="StringLit">&quot;out.yaml&quot;</span><span class="Punctuation">,</span> <span class="Identifier">fmWrite</span><span class="Punctuation">)</span>
<span class="Identifier">dump</span><span class="Punctuation">(</span><span class="Identifier">node1</span><span class="Punctuation">,</span> <span class="Identifier">s</span><span class="Punctuation">)</span>
<span class="Identifier">s</span><span class="Operator">.</span><span class="Identifier">close</span><span class="Punctuation">(</span><span class="Punctuation">)</span></pre></td><td><pre class = "listing">%YAML 1.2
--- !nim:custom:NodeObj &amp;a
name: Node 1
left:
name: Node 2
left: !!null ~
right: &amp;b
name: Node 3
left: *a
right: !!null ~
right: *b</pre></td></tr></tbody></table>
<h2 id="loading-reference-types-and-cyclic-structures">Loading reference types and cyclic structures</h2><table class="quickstart-example"><thead><tr><th>code.nim</th>
<th>in.yaml</th></tr></thead><tbody><tr><td><pre class = "listing"><span class="Keyword">import</span> <span class="Identifier">yaml</span>
<span class="Keyword">type</span>
<span class="Identifier">Node</span> <span class="Operator">=</span> <span class="Keyword">ref</span> <span class="Identifier">NodeObj</span>
<span class="Identifier">NodeObj</span> <span class="Operator">=</span> <span class="Keyword">object</span>
<span class="Identifier">name</span><span class="Punctuation">:</span> <span class="Identifier">string</span>
<span class="Identifier">left</span><span class="Punctuation">,</span> <span class="Identifier">right</span><span class="Punctuation">:</span> <span class="Identifier">Node</span>
<span class="Keyword">var</span> <span class="Identifier">node1</span><span class="Punctuation">:</span> <span class="Identifier">Node</span>
<span class="Keyword">var</span> <span class="Identifier">s</span> <span class="Operator">=</span> <span class="Identifier">newFileStream</span><span class="Punctuation">(</span><span class="StringLit">&quot;in.yaml&quot;</span><span class="Punctuation">)</span>
<span class="Identifier">load</span><span class="Punctuation">(</span><span class="Identifier">s</span><span class="Punctuation">,</span> <span class="Identifier">node1</span><span class="Punctuation">)</span>
<span class="Identifier">s</span><span class="Operator">.</span><span class="Identifier">close</span><span class="Punctuation">(</span><span class="Punctuation">)</span></pre></td><td><pre class = "listing">%YAML 1.2
--- !nim:custom:NodeObj &amp;a
name: Node 1
left:
name: Node 2
left: ~
right: &amp;b
name: Node 3
left: *a
right: ~
right: *b</pre></td></tr></tbody></table>
<h2 id="defining-a-custom-tag-uri-for-a-type">Defining a custom tag uri for a type</h2><table class="quickstart-example"><thead><tr><th>code.nim</th>
<th>out.yaml</th></tr></thead><tbody><tr><td><pre class = "listing"><span class="Keyword">import</span> <span class="Identifier">yaml</span>
<span class="Keyword">type</span> <span class="Identifier">Mob</span> <span class="Operator">=</span> <span class="Keyword">object</span>
<span class="Identifier">level</span><span class="Punctuation">,</span> <span class="Identifier">experience</span><span class="Punctuation">:</span> <span class="Identifier">int32</span>
<span class="Identifier">setTagUriForType</span><span class="Punctuation">(</span><span class="Identifier">Mob</span><span class="Punctuation">,</span> <span class="StringLit">&quot;!Mob&quot;</span><span class="Punctuation">)</span>
<span class="Keyword">var</span> <span class="Identifier">mob</span> <span class="Operator">=</span> <span class="Identifier">Mob</span><span class="Punctuation">(</span><span class="Identifier">level</span><span class="Punctuation">:</span> <span class="DecNumber">42</span><span class="Punctuation">,</span> <span class="Identifier">experience</span><span class="Punctuation">:</span> <span class="DecNumber">1800</span><span class="Punctuation">)</span>
<span class="Keyword">var</span> <span class="Identifier">s</span> <span class="Operator">=</span> <span class="Identifier">newFileStream</span><span class="Punctuation">(</span><span class="StringLit">&quot;out.yaml&quot;</span><span class="Punctuation">,</span> <span class="Identifier">fmWrite</span><span class="Punctuation">)</span>
<span class="Identifier">dump</span><span class="Punctuation">(</span><span class="Identifier">mob</span><span class="Punctuation">,</span> <span class="Identifier">s</span><span class="Punctuation">)</span>
<span class="Identifier">s</span><span class="Operator">.</span><span class="Identifier">close</span><span class="Punctuation">(</span><span class="Punctuation">)</span></pre></td><td><pre class = "listing">%YAML 1.2
--- !Mob
level: 42
experience: 1800</pre></td></tr></tbody></table>
<h2 id="dumping-nim-objects-as-json">Dumping Nim objects as JSON</h2><table class="quickstart-example"><thead><tr><th>code.nim</th>
<th>out.yaml</th></tr></thead><tbody><tr><td><pre class = "listing"><span class="Keyword">import</span> <span class="Identifier">yaml</span>
<span class="Keyword">type</span> <span class="Identifier">Person</span> <span class="Operator">=</span> <span class="Keyword">object</span>
<span class="Identifier">name</span> <span class="Punctuation">:</span> <span class="Identifier">string</span>
<span class="Identifier">age</span> <span class="Punctuation">:</span> <span class="Identifier">int32</span>
<span class="Keyword">var</span> <span class="Identifier">personList</span> <span class="Operator">=</span> <span class="Identifier">newSeq</span><span class="Punctuation">[</span><span class="Identifier">Person</span><span class="Punctuation">]</span><span class="Punctuation">(</span><span class="Punctuation">)</span>
<span class="Identifier">personList</span><span class="Operator">.</span><span class="Identifier">add</span><span class="Punctuation">(</span><span class="Identifier">Person</span><span class="Punctuation">(</span><span class="Identifier">name</span><span class="Punctuation">:</span> <span class="StringLit">&quot;Karl Koch&quot;</span><span class="Punctuation">,</span> <span class="Identifier">age</span><span class="Punctuation">:</span> <span class="DecNumber">23</span><span class="Punctuation">)</span><span class="Punctuation">)</span>
<span class="Identifier">personList</span><span class="Operator">.</span><span class="Identifier">add</span><span class="Punctuation">(</span><span class="Identifier">Person</span><span class="Punctuation">(</span><span class="Identifier">name</span><span class="Punctuation">:</span> <span class="StringLit">&quot;Peter Pan&quot;</span><span class="Punctuation">,</span> <span class="Identifier">age</span><span class="Punctuation">:</span> <span class="DecNumber">12</span><span class="Punctuation">)</span><span class="Punctuation">)</span>
<span class="Keyword">var</span> <span class="Identifier">s</span> <span class="Operator">=</span> <span class="Identifier">newFileStream</span><span class="Punctuation">(</span><span class="StringLit">&quot;out.yaml&quot;</span><span class="Punctuation">,</span> <span class="Identifier">fmWrite</span><span class="Punctuation">)</span>
<span class="Identifier">dump</span><span class="Punctuation">(</span><span class="Identifier">personList</span><span class="Punctuation">,</span> <span class="Identifier">s</span><span class="Punctuation">,</span> <span class="Identifier">options</span> <span class="Operator">=</span> <span class="Identifier">defineOptions</span><span class="Punctuation">(</span><span class="Identifier">style</span> <span class="Operator">=</span> <span class="Identifier">psJson</span><span class="Punctuation">)</span><span class="Punctuation">)</span>
<span class="Identifier">s</span><span class="Operator">.</span><span class="Identifier">close</span><span class="Punctuation">(</span><span class="Punctuation">)</span></pre></td>
<td><pre class = "listing">[
{
&quot;name&quot;: &quot;Karl Koch&quot;,
&quot;age&quot;: 23
},
{
&quot;name&quot;: &quot;Peter Pan&quot;,
&quot;age&quot;: 12
}
]</pre></td></tr></tbody></table>
<h2 id="loading-nim-objects-from-json">Loading Nim objects from JSON</h2><table class="quickstart-example"><thead><tr><th>code.nim</th>
<th>in.yaml</th></tr></thead><tbody><tr><td><pre class = "listing"><span class="Keyword">import</span> <span class="Identifier">yaml</span>
<span class="Keyword">type</span> <span class="Identifier">Person</span> <span class="Operator">=</span> <span class="Keyword">object</span>
<span class="Identifier">name</span> <span class="Punctuation">:</span> <span class="Identifier">string</span>
<span class="Identifier">age</span> <span class="Punctuation">:</span> <span class="Identifier">int32</span>
<span class="Keyword">var</span> <span class="Identifier">personList</span><span class="Punctuation">:</span> <span class="Identifier">seq</span><span class="Punctuation">[</span><span class="Identifier">Person</span><span class="Punctuation">]</span>
<span class="Keyword">var</span> <span class="Identifier">s</span> <span class="Operator">=</span> <span class="Identifier">newFileStream</span><span class="Punctuation">(</span><span class="StringLit">&quot;in.yaml&quot;</span><span class="Punctuation">)</span>
<span class="Identifier">load</span><span class="Punctuation">(</span><span class="Identifier">s</span><span class="Punctuation">,</span> <span class="Identifier">personList</span><span class="Punctuation">)</span>
<span class="Identifier">s</span><span class="Operator">.</span><span class="Identifier">close</span><span class="Punctuation">(</span><span class="Punctuation">)</span></pre></td>
<td><pre class = "listing">[
{
&quot;name&quot;: &quot;Karl Koch&quot;,
&quot;age&quot;: 23
},
{
&quot;name&quot;: &quot;Peter Pan&quot;,
&quot;age&quot;: 12
}
]</pre></td></tr></tbody></table>
<div class="row">
<div class="twelve-columns footer">
<span class="nim-sprite"></span>
<br/>
2016-03-20 23:00:15 +01:00
<small>Made with Nim. Generated: 2016-03-20 22:58:28 UTC</small>
</div>
</div>
</div>
</article>
</body>
</html>