{"route":"/en-US-v0.14.1/reference/data-loading/yaml/","title":"YAML","description":"Documentation for the `yaml` function.","part":null,"outline":[{"id":"summary","name":"Summary","children":[]},{"id":"example","name":"Example","children":[]},{"id":"conversion","name":"Conversion","children":[{"id":"notes","name":"Notes","children":[]}]},{"id":"parameters","name":"Parameters","children":[{"id":"parameters-source","name":"source","children":[]}]},{"id":"definitions","name":"Definitions","children":[{"id":"definitions-decode","name":"Decode YAML","children":[{"id":"definitions-decode-data","name":"data","children":[]}]},{"id":"definitions-encode","name":"Encode YAML","children":[{"id":"definitions-encode-value","name":"value","children":[]}]}]}],"body":{"kind":"func","content":{"path":[],"name":"yaml","title":"YAML","keywords":[],"oneliner":"Reads structured data from a YAML file.","element":false,"contextual":false,"deprecationMessage":null,"deprecationUntil":null,"details":[{"kind":"html","content":"<p>Reads structured data from a YAML file.</p>\n<p>The file must contain a valid YAML object or array. The YAML values will be\nconverted into corresponding Typst values as listed in the\n<a href=\"#conversion\">table below</a>.</p>\n<p>The function returns a dictionary, an array or, depending on the YAML file,\nanother YAML data type.</p>\n<p>The YAML files in the example contain objects with authors as keys,\neach with a sequence of their own submapping with the keys\n&quot;title&quot; and &quot;published&quot;.</p>\n<h2 id=\"example\">Example</h2>\n<div class=\"previewed-code\"><pre><code><span class=\"typ-key\">#</span><span class=\"typ-key\">let</span> <span class=\"typ-func\">bookshelf</span><span class=\"typ-punct\">(</span>contents<span class=\"typ-punct\">)</span> <span class=\"typ-op\">=</span> <span class=\"typ-punct\">{</span>\n  <span class=\"typ-key\">for</span> <span class=\"typ-punct\">(</span>author<span class=\"typ-punct\">,</span> works<span class=\"typ-punct\">)</span> <span class=\"typ-key\">in</span> contents <span class=\"typ-punct\">{</span>\n    author\n    <span class=\"typ-key\">for</span> work <span class=\"typ-key\">in</span> works <span class=\"typ-punct\">[</span>\n      <span class=\"typ-marker\">-</span> <span class=\"typ-pol\">#</span><span class=\"typ-pol\">work</span><span class=\"typ-punct\">.</span><span class=\"typ-pol\">title</span> (<span class=\"typ-pol\">#</span><span class=\"typ-pol\">work</span><span class=\"typ-punct\">.</span><span class=\"typ-pol\">published</span>)\n    <span class=\"typ-punct\">]</span>\n  <span class=\"typ-punct\">}</span>\n<span class=\"typ-punct\">}</span>\n\n<span class=\"typ-func\">#</span><span class=\"typ-func\">bookshelf</span><span class=\"typ-punct\">(</span>\n  <span class=\"typ-func\">yaml</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;scifi-authors.yaml&quot;</span><span class=\"typ-punct\">)</span>\n<span class=\"typ-punct\">)</span>\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.14.1/assets/ce1cef3a36cd7879dbe19609834df61b.png\" alt=\"Preview\"></div></div>\n<h2 id=\"conversion\">Conversion details</h2>\n<table><thead><tr><th>YAML value</th><th>Converted into Typst</th></tr></thead><tbody>\n<tr><td>null-values (<code>null</code>, <code>~</code> or empty <code> </code>)</td><td><code><span class=\"typ-key\">none</span></code></td></tr>\n<tr><td>boolean</td><td><a href=\"/en-US-v0.14.1/reference/foundations/bool/\" title=\"`bool`\"><code>bool</code></a></td></tr>\n<tr><td>number</td><td><a href=\"/en-US-v0.14.1/reference/foundations/float/\" title=\"`float`\"><code>float</code></a> or <a href=\"/en-US-v0.14.1/reference/foundations/int/\" title=\"`int`\"><code>int</code></a></td></tr>\n<tr><td>string</td><td><a href=\"/en-US-v0.14.1/reference/foundations/str/\" title=\"`str`\"><code>str</code></a></td></tr>\n<tr><td>sequence</td><td><a href=\"/en-US-v0.14.1/reference/foundations/array/\" title=\"`array`\"><code>array</code></a></td></tr>\n<tr><td>mapping</td><td><a href=\"/en-US-v0.14.1/reference/foundations/dictionary/\" title=\"`dictionary`\"><code>dictionary</code></a></td></tr>\n</tbody></table>\n<table><thead><tr><th>Typst value</th><th>Converted into YAML</th></tr></thead><tbody>\n<tr><td>types that can be converted from YAML</td><td>corresponding YAML value</td></tr>\n<tr><td><a href=\"/en-US-v0.14.1/reference/foundations/bytes/\" title=\"`bytes`\"><code>bytes</code></a></td><td>string via <a href=\"/en-US-v0.14.1/reference/foundations/repr/\" title=\"`repr`\"><code>repr</code></a></td></tr>\n<tr><td><a href=\"/en-US-v0.14.1/reference/foundations/symbol/\" title=\"`symbol`\"><code>symbol</code></a></td><td>string</td></tr>\n<tr><td><a href=\"/en-US-v0.14.1/reference/foundations/content/\" title=\"`content`\"><code>content</code></a></td><td>a mapping describing the content</td></tr>\n<tr><td>other types (<a href=\"/en-US-v0.14.1/reference/layout/length/\" title=\"`length`\"><code>length</code></a>, etc.)</td><td>string via <a href=\"/en-US-v0.14.1/reference/foundations/repr/\" title=\"`repr`\"><code>repr</code></a></td></tr>\n</tbody></table>\n<h3 id=\"notes\">Notes</h3>\n<ul>\n<li>\n<p>In most cases, YAML numbers will be converted to floats or integers\ndepending on whether they are whole numbers. However, be aware that\nintegers larger than 2<sup>63</sup>-1 or smaller than -2<sup>63</sup> will\nbe converted to floating-point numbers, which may result in an\napproximative value.</p>\n</li>\n<li>\n<p>Custom YAML tags are ignored, though the loaded value will still be present.</p>\n</li>\n<li>\n<p>Bytes are not encoded as YAML sequences for performance and readability\nreasons. Consider using <a href=\"/en-US-v0.14.1/reference/data-loading/cbor/#definitions-encode\" title=\"`cbor.encode`\"><code>cbor.encode</code></a> for binary data.</p>\n</li>\n<li>\n<p>The <code>repr</code> function is <a href=\"/en-US-v0.14.1/reference/foundations/repr/#debugging-only\">for debugging purposes only</a>,\nand its output is not guaranteed to be stable across Typst versions.</p>\n</li>\n</ul>"}],"self":false,"params":[{"name":"source","details":[{"kind":"html","content":"<p>A <a href=\"/en-US-v0.14.1/reference/syntax/#paths\">path</a> to a YAML file or raw YAML bytes.</p>"}],"types":["str","bytes"],"strings":[],"default":null,"positional":true,"named":false,"required":true,"variadic":false,"settable":false}],"returns":["any"],"scope":[{"path":["yaml"],"name":"decode","title":"Decode YAML","keywords":[],"oneliner":"Reads structured data from a YAML string/bytes.","element":false,"contextual":false,"deprecationMessage":"`yaml.decode` is deprecated, directly pass bytes to `yaml` instead","deprecationUntil":"0.15.0","details":[{"kind":"html","content":"<p>Reads structured data from a YAML string/bytes.</p>"}],"self":false,"params":[{"name":"data","details":[{"kind":"html","content":"<p>YAML data.</p>"}],"types":["str","bytes"],"strings":[],"default":null,"positional":true,"named":false,"required":true,"variadic":false,"settable":false}],"returns":["any"],"scope":[]},{"path":["yaml"],"name":"encode","title":"Encode YAML","keywords":[],"oneliner":"Encode structured data into a YAML string.","element":false,"contextual":false,"deprecationMessage":null,"deprecationUntil":null,"details":[{"kind":"html","content":"<p>Encode structured data into a YAML string.</p>"}],"self":false,"params":[{"name":"value","details":[{"kind":"html","content":"<p>Value to be encoded.</p>"}],"types":["any"],"strings":[],"default":null,"positional":true,"named":false,"required":true,"variadic":false,"settable":false}],"returns":["str"],"scope":[]}]}}}