Skip to content

nim-appkit/yaml

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

nim-yaml

Lexer based Yaml parser for Nim.

Install

nim-yaml is best installed with Nimble, Nims package manager.

nimble install yaml

Parse a simple document

The parser returns the parsed yaml as Value objects, which are supplied by the values package.

Parse a yaml document:

from yaml import nil
# NOTICE: you must import the values package as well, for accesing the parsed data.
import values

var myYaml = """
str: String
quotedString: "String \n String"
inlineSeq: [1, "a", false]
blockSeq:
  - A
  - B
mapping:
  str: Nested String
longText: >
  Text
  Text
  Text
"""

var data = yaml.parseYaml(myYaml)

# Accessing the data.

echo(data.str) # => "String"
var str = data.str[string]

echo(data.mapping.str) # => "Nested String"

echo(data.hasKey("xxx")) # => false

echo(data.inlineSeq[2]) # => "false"
var isFalse = data.inlineSeq[2][bool]

echo(data.blockSeq.len()) # => 2

# Convert ValueSequence to actual seq[T].
# Only works if the sequence only contains items of one type!
var blockSeq = data.blockSeq.asSeq(string) 

var s1: string = blockSeq[0]

Parsing multiple documents

# NOTICE: you must import the values package as well, for accesing the parsed data.
import values

from yaml import nil

var myYaml = """
a: A
id: 1
...
b: B
id: 2
"""

var data = yaml.parseYaml(myYaml)

for document in data:
  echo(document.id)

Parsing a YAML file.

# NOTICE: you must import the values package as well, for accesing the parsed data.
import values

from yaml import nil
var data = yaml.parseYamlFile()

Additional Information

Changelog

See CHANGELOG.md.

Versioning

This project follows SemVer.

License.

This project is under the MIT license.

Releases

No releases published

Packages

No packages published

Languages