@eryx/encoding/yaml Module

JSON

YAML encoding and decoding.

Serialises Luau values to YAML text and parses YAML text back into Luau values. Supports a practical subset of YAML 1.2 (block scalars, block/flow sequences and mappings, anchors & aliases, multi-line strings).

local yaml = require("@eryx/encoding/yaml")

local str = yaml.encode({ name = "Alice", scores = {100, 97} })
print(str)
-- name: Alice
-- scores:
--   - 100
--   - 97

local tbl = yaml.decode("x: 1\ny: 2")
print(tbl.x, tbl.y)  -- 1  2

Mapping rules:

Luau YAML
string scalar
number scalar (numeric)
boolean true / false
nil null / ~
Numeric table sequence (- ...)
Mixed table mapping (k: v)

Summary

Functions

yaml.encode(value: any, options: EncodeOptions?)string
yaml.decode(str: string)any

API Reference

Functions

yaml.encode

Encodes a Luau value as a YAML string.

Tables with sequential integer keys (1 .. n) are encoded as YAML sequences. Tables with string keys are encoded as YAML mappings. Nested structures are fully supported.

local yaml = require("@eryx/encoding/yaml")
print(yaml.encode({ a = 1, b = { 2, 3 } }))
-- a: 1
-- b:
--   - 2
--   - 3
yaml.encode(value: any, options: EncodeOptions?)string

Parameters

value: any

-- The value to encode.

options: EncodeOptions?

Optional formatting options.

Returns

string

The YAML string.

yaml.decode

Decodes a YAML string into a Luau value.

Supports block and flow scalars/sequences/mappings, multi-line strings (literal | and folded >), anchors/aliases, and YAML 1.2 core schema type resolution.

Only the first document is parsed when --- separators are present.

local yaml = require("@eryx/encoding/yaml")
local config = yaml.decode([[
name: My App
version: 1.2
features:
  - auth
  - logging
]])
print(config.name)  -- "My App"
yaml.decode(str: string)any

Parameters

str: string

A valid YAML string.

Returns

any

The decoded Luau value.

Types

EncodeOptions

indent: string?

Indentation string (default " " - two spaces).

sortKeys: boolean?

Sort mapping keys alphabetically.

flowLevel: number?

Use flow (inline) style for nested structures.