yaml.decode(src, [vars])function
Parses the given YAML string and returns the resulting Lua value. This function supports multiple documents, placeholder substitution, anchors, and type conversion.
Placeholders in the form {{ var }} can be replaced with values from the provided table, from local variables, upvalues, or globals. Unresolved placeholders become yaml.null.
Conversion to Lua values follows this rules:
| YAML | Lua value type |
|---|---|
"string" |
"string" |
number |
number |
boolean |
boolean |
YAML mapping {} |
table |
YAML sequence [] |
table (integer keys) |
null |
yaml.null |
Parameters
src
The YAML string to parse.
[vars]
An optional table of placeholder names to values. If omited, local variables, upvalues and globals will be used for resolution.
Return value
Returns the decoded Lua value (table, string, number, boolean, or yaml.null).
A table of documents is returned if multiple documents were found.
On error, returns nil and an error message string
Example
--! luart-extensions
import yaml
local data = yaml.decode("name: Alice")
print(data.name) -- Alice
local template = "foo: {{ var }}"
local vars = { var = "hello" }
local filled = yaml.decode(template, vars)
print(filled.foo) -- hello