NimYAML/index.html

96 lines
7.0 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>
<header>
<a class="pagetitle" href="/">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>
<h2 id="quickstart">Quickstart</h2><p>Data used in quickstart code may not be accurate and is solely used to showcase NimYAML's features.</p>
<pre class = "listing"><span class="Keyword">import</span> <span class="Identifier">yaml</span>
<span class="Keyword">type</span>
<span class="Identifier">GenderKind</span><span class="Operator">*</span> <span class="Operator">=</span> <span class="Keyword">enum</span>
<span class="Identifier">male</span><span class="Punctuation">,</span> <span class="Identifier">female</span><span class="Punctuation">,</span> <span class="Identifier">other</span>
<span class="Identifier">Person</span><span class="Operator">*</span> <span class="Operator">=</span> <span class="Keyword">object</span>
<span class="Identifier">name</span><span class="Operator">*:</span> <span class="Identifier">string</span>
<span class="Identifier">gender</span><span class="Operator">*:</span> <span class="Identifier">GenderKind</span>
<span class="Identifier">age</span><span class="Operator">*:</span> <span class="Identifier">int32</span>
<span class="Identifier">spouse</span><span class="Operator">*:</span> <span class="Keyword">ref</span> <span class="Identifier">Person</span>
<span class="Identifier">offspring</span><span class="Operator">*:</span> <span class="Identifier">seq</span><span class="Punctuation">[</span><span class="Keyword">ref</span> <span class="Identifier">Person</span><span class="Punctuation">]</span>
<span class="Keyword">let</span> <span class="Identifier">input</span> <span class="Operator">=</span> <span class="Identifier">newStringStream</span><span class="Punctuation">(</span><span class="LongStringLit">&quot;&quot;&quot;
%YAML 1.2
---
- &amp;a
name: Luke Skywalker
gender: male
age: 19
spouse: ~
offspring: []
- &amp;b
name: Han Solo
gender: male
age: 35
spouse: &amp;c
name: Leia Organa
gender: female
age: 19
spouse: *b
offspring: []
offspring: []
- *c
-
name: Anakin Skywalker
gender: male
age: 42
spouse: ~
offspring: [*a, *c]
&quot;&quot;&quot;</span><span class="Punctuation">)</span>
<span class="Keyword">var</span> <span class="Identifier">persons</span><span class="Punctuation">:</span> <span class="Identifier">seq</span><span class="Punctuation">[</span><span class="Keyword">ref</span> <span class="Identifier">Person</span><span class="Punctuation">]</span>
<span class="Identifier">load</span><span class="Punctuation">(</span><span class="Identifier">input</span><span class="Punctuation">,</span> <span class="Identifier">persons</span><span class="Punctuation">)</span>
<span class="Keyword">for</span> <span class="Identifier">person</span> <span class="Keyword">in</span> <span class="Identifier">persons</span><span class="Punctuation">:</span>
<span class="Identifier">echo</span> <span class="Identifier">person</span><span class="Operator">.</span><span class="Identifier">name</span><span class="Punctuation">,</span> <span class="StringLit">&quot;</span><span class="EscapeSequence">\n</span><span class="StringLit">age &quot;</span><span class="Punctuation">,</span> <span class="Identifier">person</span><span class="Operator">.</span><span class="Identifier">age</span>
<span class="Keyword">if</span> <span class="Identifier">person</span><span class="Operator">.</span><span class="Identifier">spouse</span> <span class="Operator">!=</span> <span class="Keyword">nil</span><span class="Punctuation">:</span>
<span class="Identifier">echo</span> <span class="StringLit">&quot;spouse: &quot;</span><span class="Punctuation">,</span> <span class="Identifier">person</span><span class="Operator">.</span><span class="Identifier">spouse</span><span class="Operator">.</span><span class="Identifier">name</span>
<span class="Keyword">for</span> <span class="Identifier">child</span> <span class="Keyword">in</span> <span class="Identifier">person</span><span class="Operator">.</span><span class="Identifier">offspring</span><span class="Punctuation">:</span>
<span class="Keyword">case</span> <span class="Identifier">child</span><span class="Operator">.</span><span class="Identifier">gender</span>
<span class="Keyword">of</span> <span class="Identifier">male</span><span class="Punctuation">:</span> <span class="Identifier">echo</span> <span class="StringLit">&quot;son: &quot;</span><span class="Punctuation">,</span> <span class="Identifier">child</span><span class="Operator">.</span><span class="Identifier">name</span>
<span class="Keyword">of</span> <span class="Identifier">female</span><span class="Punctuation">:</span> <span class="Identifier">echo</span> <span class="StringLit">&quot;daughter: &quot;</span><span class="Punctuation">,</span> <span class="Identifier">child</span><span class="Operator">.</span><span class="Identifier">name</span>
<span class="Keyword">of</span> <span class="Identifier">other</span><span class="Punctuation">:</span> <span class="Identifier">echo</span> <span class="StringLit">&quot;child: &quot;</span><span class="Punctuation">,</span> <span class="Identifier">child</span><span class="Operator">.</span><span class="Identifier">name</span>
<span class="Identifier">echo</span> <span class="StringLit">&quot;------------------------&quot;</span>
<span class="Identifier">dump</span><span class="Punctuation">(</span><span class="Identifier">persons</span><span class="Punctuation">,</span> <span class="Identifier">newFileStream</span><span class="Punctuation">(</span><span class="Identifier">stdout</span><span class="Punctuation">)</span><span class="Punctuation">)</span></pre>
<div class="row">
<div class="twelve-columns footer">
<span class="nim-sprite"></span>
<br/>
<small>Made with Nim. Generated: 2016-02-15 22:56:21 UTC</small>
</div>
</div>
</div>
</article>
</body>
</html>