YAMLエンコーディング
YAMLエンコーディング
Section titled “YAMLエンコーディング”YAMLドキュメントをLuaテーブルにパースし、Lua値をYAML文字列にシリアライズ。
local yaml = require("yaml")エンコーディング
Section titled “エンコーディング”値のエンコード
Section titled “値のエンコード”LuaテーブルをYAMLフォーマットにエンコード。
-- シンプルなキー値local config = { name = "myapp", port = 8080, debug = true}local out = yaml.encode(config)-- name: myapp-- port: 8080-- debug: true
-- 配列はYAMLリストになるlocal items = {"apple", "banana", "cherry"}yaml.encode(items)-- - apple-- - banana-- - cherry
-- ネストした構造local 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にないフィールドをアルファベット順にソート |
-- 出力のフィールド順序を制御local entry = { zebra = 1, alpha = 2, name = "test", kind = "demo"}
-- フィールドは指定された順序で表示、残りはアルファベット順にソートlocal result = yaml.encode(entry, { field_order = {"name", "kind"}, sort_unordered = true})-- name: test-- kind: demo-- alpha: 2-- zebra: 1
-- すべてのフィールドをアルファベット順にソートyaml.encode(entry, {sort_unordered = true})-- alpha: 2-- kind: demo-- name: test-- zebra: 1戻り値: string, error
デコーディング
Section titled “デコーディング”文字列のデコード
Section titled “文字列のデコード”YAML文字列をLuaテーブルにパース。
-- 設定をパースlocal 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"
-- ファイル内容からパースlocal content = fs.read("config.yaml")local settings, err = yaml.decode(content)if err then return nil, errors.wrap(err, "invalid config file")end
-- 混合型を処理local 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内容に応じてtable、array、string、number、またはbooleanを返す
| 条件 | 種別 | 再試行可能 |
|---|---|---|
| 入力がテーブルではない(encode) | errors.INVALID | no |
| 入力が文字列ではない(decode) | errors.INVALID | no |
| 空文字列(decode) | errors.INVALID | no |
| 無効なYAML構文 | errors.INTERNAL | no |
エラーの処理についてはエラー処理を参照。