YAML 编码
YAML 编码
Section titled “YAML 编码”将 YAML 文档解析为 Lua 表,并将 Lua 值序列化为 YAML 字符串。
local yaml = require("yaml")将 Lua 表编码为 YAML 格式。
-- Simple key-valuelocal config = { name = "myapp", port = 8080, debug = true}local out = yaml.encode(config)-- name: myapp-- port: 8080-- debug: true
-- Arrays become YAML listslocal items = {"apple", "banana", "cherry"}yaml.encode(items)-- - apple-- - banana-- - cherry
-- Nested structureslocal server = { http = { address = ":8080", timeout = "30s" }, database = { host = "localhost", port = 5432 }}yaml.encode(server)| 参数 | 类型 | 描述 |
|---|---|---|
data | table | 要编码的 Lua 表 |
options | table? | 可选的编码选项 |
| 字段 | 类型 | 描述 |
|---|---|---|
field_order | string[] | 自定义字段顺序 - 字段按此顺序出现 |
sort_unordered | boolean | 将不在 field_order 中的字段按字母排序 |
-- Control field order in outputlocal entry = { zebra = 1, alpha = 2, name = "test", kind = "demo"}
-- Fields appear in specified order, remaining sorted alphabeticallylocal result = yaml.encode(entry, { field_order = {"name", "kind"}, sort_unordered = true})-- name: test-- kind: demo-- alpha: 2-- zebra: 1
-- Just sort all fields alphabeticallyyaml.encode(entry, {sort_unordered = true})-- alpha: 2-- kind: demo-- name: test-- zebra: 1返回: string, error
将 YAML 字符串解析为 Lua 表。
-- Parse configurationlocal config, err = yaml.decode([[server: host: localhost port: 8080features: - auth - logging - metrics]])if err then return nil, errend
print(config.server.host) -- "localhost"print(config.server.port) -- 8080print(config.features[1]) -- "auth"
-- Parse from file contentlocal content = fs.read("config.yaml")local settings, err = yaml.decode(content)if err then return nil, errors.wrap(err, "invalid config file")end
-- Handle mixed typeslocal data = yaml.decode([[name: testcount: 42ratio: 3.14enabled: truetags: - lua - wippy]])print(type(data.count)) -- "number"print(type(data.enabled)) -- "boolean"print(type(data.tags)) -- "table"| 参数 | 类型 | 描述 |
|---|---|---|
data | string | 要解析的 YAML 字符串 |
返回: any, error - 根据 YAML 内容返回表、数组、字符串、数字或布尔值
| 条件 | 类型 | 可重试 |
|---|---|---|
| 输入不是表(编码) | errors.INVALID | 否 |
| 输入不是字符串(解码) | errors.INVALID | 否 |
| 空字符串(解码) | errors.INVALID | 否 |
| 无效的 YAML 语法 | errors.INTERNAL | 否 |
参见 错误处理 了解错误处理方法。