<h1id="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 <aclass="reference external"href="#http://www.yaml.org/spec/1.2/spec.html">YAML 1.2</a>.</p>
<h2id="quickstart">Quickstart</h2><p>Data used in quickstart code may not be accurate and is solely used to showcase NimYAML's features.</p>
<h1id="api-overview">API Overview</h1><p>NimYAML advocates parsing YAML input into native Nim types. Basic library types like integers, floats and strings, as well as all tuples, enums and objects without private fields are supported out-of-the-box. Reference types are also supported, and NimYAML is able to detect if a reference occurs more than once and will serialize it accordingly. This means that NimYAML is able to dump and load potentially cyclic objects.</p>
<p>While loading into and dumping from native Nim types is the preferred way to use NimYAML, it also gives you complete control over each processing step, so that you can for example only use the parser and process its event stream yourself. The following diagram gives an overview of NimYAML's features based on the YAML processing pipeline. The items and terminology YAML defines is shown in <em>italic</em>, NimYAML's implementation name is shown in <strong>bold</strong>.</p>
<h2id="intermediate-representation">Intermediate Representation</h2><p>The base of all YAML processing with NimYAML is the <aclass="reference external"href="yaml.html#YamlStream">YamlStream</a>. This is basically an iterator over <aclass="reference external"href="yaml.html#YamlStreamEvent">YamlStreamEvent</a> objects. Every proc that represents a single stage of the loading or dumping process will either take a <ttclass="docutils literal"><spanclass="pre">YamlStream</span></tt> as input or return a <ttclass="docutils literal"><spanclass="pre">YamlStream</span></tt>. Procs that implement the whole process in one step hide the <ttclass="docutils literal"><spanclass="pre">YamlStream</span></tt> from the user. Every proc that returns a <ttclass="docutils literal"><spanclass="pre">YamlStream</span></tt> guarantees that this stream is well-formed according to the YAML specification.</p>
<p>This stream-oriented API can efficiently be used to parse large amounts of data. The drawback is that errors in the input are only discovered while processing the <ttclass="docutils literal"><spanclass="pre">YamlStream</span></tt>. If the <ttclass="docutils literal"><spanclass="pre">YamlStream</span></tt> encounters an exception while producing the next event, it will throw a <ttclass="docutils literal"><spanclass="pre">YamlStreamError</span></tt> which contains the original exception as <ttclass="docutils literal"><spanclass="pre">parent</span></tt>. The caller should know which exceptions are possible as parents of <ttclass="docutils literal"><spanclass="pre">YamlStream</span></tt> because they know the source of the <ttclass="docutils literal"><spanclass="pre">YamlStream</span></tt> they provided.</p>
<h2id="loading-yaml">Loading YAML</h2><p>For parsing, a <aclass="reference external"href="yaml.html#YamlParser">YamlParser</a> object is needed. This object stores some state while parsing that may be useful for error reporting to the user. The <aclass="reference external"href="yaml.html#present,YamlStream,Stream,TagLibrary,PresentationStyle,int">parse</a> proc implements the YAML processing step of the same name. All syntax errors in the input character stream are processed by <ttclass="docutils literal"><spanclass="pre">parse</span></tt>, which will raise a <ttclass="docutils literal"><spanclass="pre">YamlParserError</span></tt> if it encounters a syntax error.</p>
<p>Transforming a <ttclass="docutils literal"><spanclass="pre">YamlStream</span></tt> to a native YAML object is done via <ttclass="docutils literal"><spanclass="pre">construct</span></tt>. It skips the <ttclass="docutils literal"><spanclass="pre">compose</span></tt> step for efficiency reasons. As Nim is statically typed, you have to know the target type when you write your loading code. This is different from YAML APIs of dynamically typed languages. If you cannot know the type of your YAML input at compile time, you have to manually process the <ttclass="docutils literal"><spanclass="pre">YamlStream</span></tt> to serve your needs.</p>
<p>If you want to load YAML character data directly into a native Nim variable, you can use <aclass="reference external"href="yaml.html#load,Stream,K">load</a>.</p>
<h2id="dumping-yaml">Dumping YAML</h2><p>Dumping YAML is straightforward: You transform a variable into a <ttclass="docutils literal"><spanclass="pre">YamlStream</span></tt> with <aclass="reference external"href="yaml.html#represent,T,TagStyle,AnchorStyle">represent</a> and then write that to a stream using <aclass="reference external"href="yaml.html#present,YamlStream,Stream,TagLibrary,PresentationStyle,int">present</a>. If you want to execute both steps at once, you can use <aclass="reference external"href="yaml.html#dump,K,Stream,PresentationStyle,TagStyle,AnchorStyle,int">dump</a>.</p>
<p>The <ttclass="docutils literal"><spanclass="pre">present</span></tt> step allows you to specify how you want the output YAML character stream to be formatted. Amongst other options, it is possible to output pure JSON, but only if the stream does not contain any constructs that cannot be presented in JSON.</p>