YAML parser for Lua
This parser uses libYAML by Kirill Siminov with bindings by Andrew Danforth and some modifications by Gaspard Bucher. libYAML version 0.1.3. LibYAML is generally considered to be the best C YAML 1.1 implementation.
MIT license © Kirill Siminov 2006, Andrew Danforth 2009.
Installation
With luarocks:
$ luarocks install yaml
Usage example
local data = yaml.load(some_yaml) local yaml_string = yaml.dump(some_table)
.VERSION = '1.1.2'
Current version respecting semantic versioning.
.DEPENDS = {
'lua >= 5.1, < 5.4'
Compatible with Lua 5.1 to 5.3 and LuaJIT
'lub >= 1.0.3, < 2'
Uses Lubyk base library
}
Lua table
Lua tables works as sets. In Lua, there is no distinction between an array and a dictionary whereas YAML makes this distinction. When dumping a table, the library has to choose between these two types. Here is the selection rule:
array | The table contains an element in index position 1 . |
Lua
Some lua code
local data = { { job = 'Car wash', duration = 1.5, comment = 'The car was really filthy.' }, { job = 'Create website', duration = 6.25, comment = 'Using bootstrap template.' } } -- Create a loop data[1].link = data
YAML
And the equivalent yaml using yaml.dump(data)
:
--- &0 - link: *0 duration: 1.5 comment: The car was really filthy. job: Car wash - duration: 6.25 comment: Using bootstrap template. job: Create website
Class methods
.load (string, safe)
Parse a string
containing yaml content and return lua values. If the second argument safe
is true, the library will not generate aliases in tables.
Note that if there are more then on value in the YAML content, this function will return multiple values. Example:
local a, b, c = yaml.load [[ --- 3 --- 4 --- 5 ... ]] --> a = 3, b = 4, c = 5
.loadpath (path)
Parse the YAML content of the file at path
and return lua values. Uses yaml.load internally.
.dump (...)
Dump all lua values in the vararg as YAML. Note that when using 'load' on the produced content, multiple values will be returned.
.configure (options)
Configure parser. Pass a table setting options to true or false. The default value is shown in parenthesis.
WARN Please note that this configuration alters parsing globaly and should be avoided. If such configuration is often needed, please consult the maintainer so that we can move such configuration inside a Parser object instead of using global settings.
dump_auto_array | (true) |
dump_error_on_unsupported | (false) |
dump_check_metatables | (true) |
load_set_metatables | (true) |
load_numeric_scalars | (true) |
load_nulls_as_nil | (false) |
sort_table_keys | (false) |