NimYAML/doc/index.txt

80 lines
1.9 KiB
Plaintext
Raw Normal View History

2016-01-13 21:43:12 +00:00
=======
NimYAML
=======
Introduction
============
2016-02-02 20:14:56 +00:00
**NimYAML** 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
`YAML 1.2 <#http://www.yaml.org/spec/1.2/spec.html>`_.
2016-02-16 19:44:40 +00:00
Source code can be found on `GitHub <https://github.com/flyx/NimYAML>`_. You can
install it with `Nimble <https://github.com/nim-lang/nimble>`_:
.. code-block:: bash
nimble install nimyaml
Quickstart
----------
Data used in quickstart code may not be accurate and is solely used to showcase
NimYAML's features.
.. code-block:: nim
import yaml
type
GenderKind* = enum
male, female, other
Person* = object
name*: string
gender*: GenderKind
age*: int32
spouse*: ref Person
offspring*: seq[ref Person]
let input = newStringStream("""
%YAML 1.2
---
- &a
name: Luke Skywalker
gender: male
age: 19
spouse: ~
offspring: []
- &b
name: Han Solo
gender: male
age: 35
spouse: &c
name: Leia Organa
gender: female
age: 19
spouse: *b
offspring: []
offspring: []
- *c
-
name: Anakin Skywalker
gender: male
age: 42
spouse: ~
offspring: [*a, *c]
""")
var persons: seq[ref Person]
load(input, persons)
for person in persons:
echo person.name, "\nage ", person.age
if person.spouse != nil:
echo "spouse: ", person.spouse.name
for child in person.offspring:
case child.gender
of male: echo "son: ", child.name
of female: echo "daughter: ", child.name
of other: echo "child: ", child.name
echo "------------------------"
dump(persons, newFileStream(stdout))