mirror of https://github.com/status-im/NimYAML.git
289 lines
42 KiB
HTML
289 lines
42 KiB
HTML
<!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>
|
|
<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>
|
|
<a class="pagetitle" href="index.html">NimYAML</a>
|
|
<a href="index.html">Home</a>
|
|
<a href="testing.html">Testing Ground</a>
|
|
<span>Docs:</span>
|
|
<a href="api.html">Overview</a>
|
|
<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>
|
|
|
|
<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>
|
|
<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>
|
|
<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">"Karl Koch"</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">"Peter Pan"</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">"out.yaml"</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"><span class="Directive">%YAML 1.2</span>
|
|
<span class="Keyword">---</span> <span class="TagStart">!nim:system:seq(nim:custom:Person)</span>
|
|
<span class="Punctuation">-</span>
|
|
<span class="StringLit">name</span><span class="Punctuation">:</span> <span class="StringLit">Karl Koch</span>
|
|
<span class="StringLit">age</span><span class="Punctuation">:</span> <span class="DecNumber">23</span>
|
|
<span class="Punctuation">-</span>
|
|
<span class="StringLit">name</span><span class="Punctuation">:</span> <span class="StringLit">Peter Pan</span>
|
|
<span class="StringLit">age</span><span class="Punctuation">:</span> <span class="DecNumber">12</span></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">"in.yaml"</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"><span class="Directive">%YAML 1.2</span>
|
|
<span class="Keyword">---</span>
|
|
<span class="Punctuation">-</span> <span class="Punctuation">{</span> <span class="StringLit">name</span><span class="Punctuation">:</span> <span class="StringLit">Karl Koch</span><span class="Punctuation">,</span> <span class="StringLit">age</span><span class="Punctuation">:</span> <span class="DecNumber">23 </span><span class="Punctuation">}</span>
|
|
<span class="Punctuation">-</span> <span class="Punctuation">{</span> <span class="StringLit">name</span><span class="Punctuation">:</span> <span class="StringLit">Peter Pan</span><span class="Punctuation">,</span> <span class="StringLit">age</span><span class="Punctuation">:</span> <span class="DecNumber">12 </span><span class="Punctuation">}</span></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">"Karl Koch"</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">"Peter Pan"</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">"out.yaml"</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"><span class="Directive">%YAML 1.1</span>
|
|
<span class="Keyword">---</span> <span class="TagStart">!nim:system:seq(nim:custom:Person)</span>
|
|
<span class="Punctuation">[</span>
|
|
<span class="TagStart">!nim:custom:Person</span> <span class="Punctuation">{</span>
|
|
<span class="Punctuation">?</span> <span class="TagStart">!!str</span> <span class="StringLit">"</span><span class="StringLit">name"</span>
|
|
<span class="Punctuation">:</span> <span class="TagStart">!!str</span> <span class="StringLit">"</span><span class="StringLit">Karl Koch"</span><span class="Punctuation">,</span>
|
|
<span class="Punctuation">?</span> <span class="TagStart">!!str</span> <span class="StringLit">"</span><span class="StringLit">age"</span>
|
|
<span class="Punctuation">:</span> <span class="TagStart">!nim:system:int32</span> <span class="StringLit">"</span><span class="StringLit">23"</span>
|
|
<span class="Punctuation">}</span><span class="Punctuation">,</span>
|
|
<span class="TagStart">!nim:custom:Person</span> <span class="Punctuation">{</span>
|
|
<span class="Punctuation">?</span> <span class="TagStart">!!str</span> <span class="StringLit">"</span><span class="StringLit">name"</span>
|
|
<span class="Punctuation">:</span> <span class="TagStart">!!str</span> <span class="StringLit">"</span><span class="StringLit">Peter Pan"</span><span class="Punctuation">,</span>
|
|
<span class="Punctuation">?</span> <span class="TagStart">!!str</span> <span class="StringLit">"</span><span class="StringLit">age"</span>
|
|
<span class="Punctuation">:</span> <span class="TagStart">!nim:system:int32</span> <span class="StringLit">"</span><span class="StringLit">12"</span>
|
|
<span class="Punctuation">}</span>
|
|
<span class="Punctuation">]</span></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">"Node 1"</span>
|
|
<span class="Identifier">node2</span><span class="Operator">.</span><span class="Identifier">name</span> <span class="Operator">=</span> <span class="StringLit">"Node 2"</span>
|
|
<span class="Identifier">node3</span><span class="Operator">.</span><span class="Identifier">name</span> <span class="Operator">=</span> <span class="StringLit">"Node 3"</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">"out.yaml"</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"><span class="Directive">%YAML 1.2</span>
|
|
<span class="Keyword">---</span> <span class="TagStart">!nim:custom:NodeObj</span> <span class="Label">&a</span>
|
|
<span class="StringLit">name</span><span class="Punctuation">:</span> <span class="StringLit">Node 1</span>
|
|
<span class="StringLit">left</span><span class="Punctuation">:</span>
|
|
<span class="StringLit">name</span><span class="Punctuation">:</span> <span class="StringLit">Node 2</span>
|
|
<span class="StringLit">left</span><span class="Punctuation">:</span> <span class="TagStart">!!null</span> <span class="StringLit">~</span>
|
|
<span class="StringLit">right</span><span class="Punctuation">:</span> <span class="Label">&b</span>
|
|
<span class="StringLit">name</span><span class="Punctuation">:</span> <span class="StringLit">Node 3</span>
|
|
<span class="StringLit">left</span><span class="Punctuation">:</span> <span class="Reference">*a</span>
|
|
<span class="StringLit">right</span><span class="Punctuation">:</span> <span class="TagStart">!!null</span> <span class="StringLit">~</span>
|
|
<span class="StringLit">right</span><span class="Punctuation">:</span> <span class="Reference">*b</span></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">"in.yaml"</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"><span class="Directive">%YAML 1.2</span>
|
|
<span class="Keyword">---</span> <span class="TagStart">!nim:custom:NodeObj</span> <span class="Label">&a</span>
|
|
<span class="StringLit">name</span><span class="Punctuation">:</span> <span class="StringLit">Node 1</span>
|
|
<span class="StringLit">left</span><span class="Punctuation">:</span>
|
|
<span class="StringLit">name</span><span class="Punctuation">:</span> <span class="StringLit">Node 2</span>
|
|
<span class="StringLit">left</span><span class="Punctuation">:</span> <span class="StringLit">~</span>
|
|
<span class="StringLit">right</span><span class="Punctuation">:</span> <span class="Label">&b</span>
|
|
<span class="StringLit">name</span><span class="Punctuation">:</span> <span class="StringLit">Node 3</span>
|
|
<span class="StringLit">left</span><span class="Punctuation">:</span> <span class="Reference">*a</span>
|
|
<span class="StringLit">right</span><span class="Punctuation">:</span> <span class="StringLit">~</span>
|
|
<span class="StringLit">right</span><span class="Punctuation">:</span> <span class="Reference">*b</span></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">drops</span><span class="Punctuation">:</span> <span class="Identifier">seq</span><span class="Punctuation">[</span><span class="Identifier">string</span><span class="Punctuation">]</span>
|
|
|
|
<span class="Identifier">setTagUri</span><span class="Punctuation">(</span><span class="Identifier">Mob</span><span class="Punctuation">,</span> <span class="StringLit">"!Mob"</span><span class="Punctuation">)</span>
|
|
<span class="Identifier">setTagUri</span><span class="Punctuation">(</span><span class="Identifier">seq</span><span class="Punctuation">[</span><span class="Identifier">string</span><span class="Punctuation">]</span><span class="Punctuation">,</span> <span class="StringLit">"!Drops"</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="Identifier">drops</span><span class="Punctuation">:</span>
|
|
<span class="Operator">@</span><span class="Punctuation">[</span><span class="StringLit">"Sword of Mob Slaying"</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">"out.yaml"</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">options</span> <span class="Operator">=</span> <span class="Identifier">defineOptions</span><span class="Punctuation">(</span><span class="Identifier">tagStyle</span> <span class="Operator">=</span> <span class="Identifier">tsAll</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"><span class="StringLit">YAML 1.2</span>
|
|
<span class="Keyword">---</span> <span class="TagStart">!Mob</span>
|
|
<span class="TagStart">!nim:field</span> <span class="StringLit">level</span><span class="Punctuation">:</span> <span class="TagStart">!nim:system:int32</span> <span class="DecNumber">42</span>
|
|
<span class="TagStart">!nim:field</span> <span class="StringLit">experience</span><span class="Punctuation">:</span> <span class="TagStart">!nim:system:int32</span> <span class="DecNumber">1800</span>
|
|
<span class="TagStart">!nim:field</span> <span class="StringLit">drops</span><span class="Punctuation">:</span> <span class="TagStart">!Drops</span> <span class="Punctuation">[</span><span class="TagStart">!!str</span> <span class="StringLit">Sword of Mob Slaying</span><span class="Punctuation">]</span></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">"Karl Koch"</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">"Peter Pan"</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">"out.yaml"</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"><span class="Punctuation">[</span>
|
|
<span class="Punctuation">{</span>
|
|
<span class="StringLit">"</span><span class="StringLit">name"</span><span class="Punctuation">:</span> <span class="StringLit">"</span><span class="StringLit">Karl Koch"</span><span class="Punctuation">,</span>
|
|
<span class="StringLit">"</span><span class="StringLit">age"</span><span class="Punctuation">:</span> <span class="DecNumber">23</span>
|
|
<span class="Punctuation">}</span><span class="Punctuation">,</span>
|
|
<span class="Punctuation">{</span>
|
|
<span class="StringLit">"</span><span class="StringLit">name"</span><span class="Punctuation">:</span> <span class="StringLit">"</span><span class="StringLit">Peter Pan"</span><span class="Punctuation">,</span>
|
|
<span class="StringLit">"</span><span class="StringLit">age"</span><span class="Punctuation">:</span> <span class="DecNumber">12</span>
|
|
<span class="Punctuation">}</span>
|
|
<span class="Punctuation">]</span></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">"in.yaml"</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"><span class="Punctuation">[</span>
|
|
<span class="Punctuation">{</span>
|
|
<span class="StringLit">"</span><span class="StringLit">name"</span><span class="Punctuation">:</span> <span class="StringLit">"</span><span class="StringLit">Karl Koch"</span><span class="Punctuation">,</span>
|
|
<span class="StringLit">"</span><span class="StringLit">age"</span><span class="Punctuation">:</span> <span class="DecNumber">23</span>
|
|
<span class="Punctuation">}</span><span class="Punctuation">,</span>
|
|
<span class="Punctuation">{</span>
|
|
<span class="StringLit">"</span><span class="StringLit">name"</span><span class="Punctuation">:</span> <span class="StringLit">"</span><span class="StringLit">Peter Pan"</span><span class="Punctuation">,</span>
|
|
<span class="StringLit">"</span><span class="StringLit">age"</span><span class="Punctuation">:</span> <span class="DecNumber">12</span>
|
|
<span class="Punctuation">}</span>
|
|
<span class="Punctuation">]</span></pre></td></tr></tbody></table>
|
|
<h2 id="processing-a-sequence-of-heterogeneous-items">Processing a Sequence of Heterogeneous Items</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">setTagUri</span><span class="Punctuation">(</span><span class="Identifier">Person</span><span class="Punctuation">,</span> <span class="StringLit">"!nim:demo:Person"</span><span class="Punctuation">,</span> <span class="Identifier">yTagPerson</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">"in.yaml"</span><span class="Punctuation">,</span> <span class="Identifier">fmRead</span><span class="Punctuation">)</span>
|
|
<span class="Identifier">context</span> <span class="Operator">=</span> <span class="Identifier">newConstructionContext</span><span class="Punctuation">(</span><span class="Punctuation">)</span>
|
|
<span class="Identifier">parser</span> <span class="Operator">=</span> <span class="Identifier">newYamlParser</span><span class="Punctuation">(</span><span class="Identifier">serializationTagLibrary</span><span class="Punctuation">)</span>
|
|
<span class="Identifier">events</span> <span class="Operator">=</span> <span class="Identifier">parser</span><span class="Operator">.</span><span class="Identifier">parse</span><span class="Punctuation">(</span><span class="Identifier">s</span><span class="Punctuation">)</span>
|
|
|
|
<span class="Identifier">assert</span> <span class="Identifier">events</span><span class="Operator">.</span><span class="Identifier">next</span><span class="Punctuation">(</span><span class="Punctuation">)</span><span class="Operator">.</span><span class="Identifier">kind</span> <span class="Operator">==</span> <span class="Identifier">yamlStartDoc</span>
|
|
<span class="Identifier">assert</span> <span class="Identifier">events</span><span class="Operator">.</span><span class="Identifier">next</span><span class="Punctuation">(</span><span class="Punctuation">)</span><span class="Operator">.</span><span class="Identifier">kind</span> <span class="Operator">==</span> <span class="Identifier">yamlStartSeq</span>
|
|
<span class="Keyword">var</span> <span class="Identifier">nextEvent</span> <span class="Operator">=</span> <span class="Identifier">events</span><span class="Operator">.</span><span class="Identifier">peek</span><span class="Punctuation">(</span><span class="Punctuation">)</span>
|
|
<span class="Keyword">while</span> <span class="Identifier">nextEvent</span><span class="Operator">.</span><span class="Identifier">kind</span> <span class="Operator">!=</span> <span class="Identifier">yamlEndSeq</span><span class="Punctuation">:</span>
|
|
<span class="Keyword">var</span> <span class="Identifier">curTag</span> <span class="Operator">=</span> <span class="Identifier">nextEvent</span><span class="Operator">.</span><span class="Identifier">tag</span><span class="Punctuation">(</span><span class="Punctuation">)</span>
|
|
<span class="Keyword">if</span> <span class="Identifier">curTag</span> <span class="Operator">==</span> <span class="Identifier">yTagQuestionMark</span><span class="Punctuation">:</span>
|
|
<span class="Comment"># we only support implicitly tagged scalars</span>
|
|
<span class="Identifier">assert</span> <span class="Identifier">nextEvent</span><span class="Operator">.</span><span class="Identifier">kind</span> <span class="Operator">==</span> <span class="Identifier">yamlScalar</span>
|
|
<span class="Keyword">case</span> <span class="Identifier">guessType</span><span class="Punctuation">(</span><span class="Identifier">nextEvent</span><span class="Operator">.</span><span class="Identifier">scalarContent</span><span class="Punctuation">)</span>
|
|
<span class="Keyword">of</span> <span class="Identifier">yTypeInteger</span><span class="Punctuation">:</span> <span class="Identifier">curTag</span> <span class="Operator">=</span> <span class="Identifier">yTagInteger</span>
|
|
<span class="Keyword">of</span> <span class="Identifier">yTypeBoolTrue</span><span class="Punctuation">,</span> <span class="Identifier">yTypeBoolFalse</span><span class="Punctuation">:</span>
|
|
<span class="Identifier">curTag</span> <span class="Operator">=</span> <span class="Identifier">yTagBoolean</span>
|
|
<span class="Keyword">of</span> <span class="Identifier">yTypeUnknown</span><span class="Punctuation">:</span> <span class="Identifier">curTag</span> <span class="Operator">=</span> <span class="Identifier">yTagString</span>
|
|
<span class="Keyword">else</span><span class="Punctuation">:</span> <span class="Identifier">assert</span> <span class="Identifier">false</span><span class="Punctuation">,</span> <span class="StringLit">"Type not supported!"</span>
|
|
<span class="Keyword">elif</span> <span class="Identifier">curTag</span> <span class="Operator">==</span> <span class="Identifier">yTagExclamationMark</span><span class="Punctuation">:</span>
|
|
<span class="Identifier">curTag</span> <span class="Operator">=</span> <span class="Identifier">yTagString</span>
|
|
<span class="Keyword">case</span> <span class="Identifier">curTag</span>
|
|
<span class="Keyword">of</span> <span class="Identifier">yTagString</span><span class="Punctuation">:</span>
|
|
<span class="Keyword">var</span> <span class="Identifier">s</span><span class="Punctuation">:</span> <span class="Identifier">string</span>
|
|
<span class="Identifier">events</span><span class="Operator">.</span><span class="Identifier">constructChild</span><span class="Punctuation">(</span><span class="Identifier">context</span><span class="Punctuation">,</span> <span class="Identifier">s</span><span class="Punctuation">)</span>
|
|
<span class="Identifier">echo</span> <span class="StringLit">"got string: "</span><span class="Punctuation">,</span> <span class="Identifier">s</span>
|
|
<span class="Keyword">of</span> <span class="Identifier">yTagInteger</span><span class="Punctuation">:</span>
|
|
<span class="Keyword">var</span> <span class="Identifier">i</span><span class="Punctuation">:</span> <span class="Identifier">int32</span>
|
|
<span class="Identifier">events</span><span class="Operator">.</span><span class="Identifier">constructChild</span><span class="Punctuation">(</span><span class="Identifier">context</span><span class="Punctuation">,</span> <span class="Identifier">i</span><span class="Punctuation">)</span>
|
|
<span class="Identifier">echo</span> <span class="StringLit">"got integer: "</span><span class="Punctuation">,</span> <span class="Identifier">i</span>
|
|
<span class="Keyword">of</span> <span class="Identifier">yTagBoolean</span><span class="Punctuation">:</span>
|
|
<span class="Keyword">var</span> <span class="Identifier">b</span><span class="Punctuation">:</span> <span class="Identifier">bool</span>
|
|
<span class="Identifier">events</span><span class="Operator">.</span><span class="Identifier">constructChild</span><span class="Punctuation">(</span><span class="Identifier">context</span><span class="Punctuation">,</span> <span class="Identifier">b</span><span class="Punctuation">)</span>
|
|
<span class="Identifier">echo</span> <span class="StringLit">"got boolean: "</span><span class="Punctuation">,</span> <span class="Identifier">b</span>
|
|
<span class="Keyword">of</span> <span class="Identifier">yTagPerson</span><span class="Punctuation">:</span>
|
|
<span class="Keyword">var</span> <span class="Identifier">p</span><span class="Punctuation">:</span> <span class="Identifier">Person</span>
|
|
<span class="Identifier">events</span><span class="Operator">.</span><span class="Identifier">constructChild</span><span class="Punctuation">(</span><span class="Identifier">context</span><span class="Punctuation">,</span> <span class="Identifier">p</span><span class="Punctuation">)</span>
|
|
<span class="Identifier">echo</span> <span class="StringLit">"got Person with name: "</span><span class="Punctuation">,</span> <span class="Identifier">p</span><span class="Operator">.</span><span class="Identifier">name</span>
|
|
<span class="Keyword">else</span><span class="Punctuation">:</span> <span class="Identifier">assert</span> <span class="Identifier">false</span><span class="Punctuation">,</span> <span class="StringLit">"unsupported tag: "</span> <span class="Operator">&</span> <span class="Operator">$</span><span class="Identifier">curTag</span>
|
|
<span class="Identifier">nextEvent</span> <span class="Operator">=</span> <span class="Identifier">events</span><span class="Operator">.</span><span class="Identifier">peek</span><span class="Punctuation">(</span><span class="Punctuation">)</span>
|
|
<span class="Identifier">assert</span> <span class="Identifier">events</span><span class="Operator">.</span><span class="Identifier">next</span><span class="Punctuation">(</span><span class="Punctuation">)</span><span class="Operator">.</span><span class="Identifier">kind</span> <span class="Operator">==</span> <span class="Identifier">yamlEndSeq</span>
|
|
<span class="Identifier">assert</span> <span class="Identifier">events</span><span class="Operator">.</span><span class="Identifier">next</span><span class="Punctuation">(</span><span class="Punctuation">)</span><span class="Operator">.</span><span class="Identifier">kind</span> <span class="Operator">==</span> <span class="Identifier">yamlEndDoc</span>
|
|
<span class="Identifier">assert</span> <span class="Identifier">events</span><span class="Operator">.</span><span class="Identifier">finished</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"><span class="Directive">%YAML 1.2</span>
|
|
<span class="Keyword">---</span> <span class="TagStart">!!seq</span>
|
|
<span class="Punctuation">-</span> <span class="StringLit">this is a string</span>
|
|
<span class="Punctuation">-</span> <span class="DecNumber">42</span>
|
|
<span class="Punctuation">-</span> <span class="StringLit">false</span>
|
|
<span class="Punctuation">-</span> <span class="TagStart">!!str</span> <span class="DecNumber">23</span>
|
|
<span class="Punctuation">-</span> <span class="TagStart">!nim:demo:Person</span> <span class="Punctuation">{</span><span class="StringLit">name</span><span class="Punctuation">:</span> <span class="StringLit">Trillian</span><span class="Punctuation">}</span></pre></td></tr></tbody></table>
|
|
|
|
|
|
<div class="row">
|
|
<div class="twelve-columns footer">
|
|
<span class="nim-sprite"></span>
|
|
<br/>
|
|
<small>Made with Nim. Generated: 2016-04-21 19:00:52 UTC</small>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</article>
|
|
</body>
|
|
</html>
|