19 KiB
Data
The data formats (JSON, XML, YAML, TOML, CSV, INI, MessagePack, CBOR, plist) decode to plain Luau values: tables,
strings, numbers, booleans. Three helpers fill the gaps Luau tables leave: F.data.null (a null that can sit in a
table), ordered objects (F.Types.object: key order, and {} that stays an object), and F.Types.Date (TOML,
YAML, plist, CBOR and MessagePack dates). XML decodes to a tree of XmlNodes. Minecraft's NBT and SNBT (typed
numbers kept) are on the Minecraft and voxels page.
local v = assert(F.data.load('{"name": "ada", "tags": ["x", "y"], "boss": null}'))
assert(v.name == "ada" and v.tags[2] == "y")
assert(v.boss == F.data.null) -- JSON null is a sentinel, not nil
v.level = 3
local json = assert(F.saveString(v, { format = "json" }))
assert(json == '{"boss":null,"level":3,"name":"ada","tags":["x","y"]}') -- plain tables: keys sorted
local yaml = assert(F.convert('{"a": [1, 2]}', "yaml", { format = "json" })) -- opts.format: the input's format
assert(buffer.tostring(yaml) == "a:\n - 1\n - 2\n")
Values
| Data | Luau value |
|---|---|
| object / map / table / dict | a table with string keys; maps of MessagePack / CBOR can have any key type |
| array / sequence / list | an array { ... } (1-based) |
| string | string (UTF-8) |
| number | number (one type: integers and floats are both doubles; integers past 2^53 lose precision unless bigInts = "string") |
| true / false | boolean |
null (null, ~, CBOR null, msgpack nil) |
F.data.null (same as F.null; tostring gives "null") |
bytes (msgpack bin, CBOR byte string, plist <data>, YAML !!binary) |
buffer |
date / time (TOML, YAML !!timestamp, plist <date>, CBOR tags 0 / 1, msgpack timestamp) |
F.Types.Date |
Decoders give objects as F.Types.object tables when the key order is known: always for YAML, TOML, INI, CSV
(header = true), MessagePack, CBOR and plist; for JSON only empty objects, unless ordered = true. Encoders write
an object's keys in that order; plain tables are written with their keys sorted.
Ordered objects and empty tables
local Types = F.Types
local o = Types.object() -- an empty object: writes as {} (a plain {} writes as [])
Types.set(o, "zeta", 1) -- assigns and records the key order
Types.set(o, "alpha", 2)
o.later = 3 -- plain assignment works too; keys not in the order come last, sorted
assert(table.concat(Types.keys(o), ",") == "zeta,alpha,later")
assert(Types.isObject(o) and not Types.isObject({}))
assert(F.saveString(o, { format = "json" }) == '{"zeta":1,"alpha":2,"later":3}')
local keep = assert(F.data.load('{"b": 1, "a": 2}', { ordered = true }))
assert(F.saveString(keep, { format = "json" }) == '{"b":1,"a":2}')
assert(F.saveString({}, { format = "json" }) == "[]")
assert(F.saveString(Types.object(), { format = "json" }) == "{}")
assert(F.saveString({}, { format = "json", emptyTable = "object" }) == "{}")
Types.object(t, keys) marks an existing table and gives its order; Types.keys(t) works on any table (plain
tables: sorted, numbers before strings).
Dates
F.Types.Date values are tables with a metatable (tostring gives RFC 3339; == compares them).
| Field | Meaning |
|---|---|
kind |
"datetime" (has an offset: an instant), "datetime-local", "date-local", "time-local" |
year, month, day |
the date (month 1..12, day 1..31) |
hour, minute, second |
the time of day |
nanosecond |
0 .. 999999999 |
offset |
minutes east of UTC (-480 = -08:00); nil for local kinds |
local Date = F.Types.Date
local d = assert(Date.parse("1979-05-27T07:32:00-08:00"))
assert(d.kind == "datetime" and d.offset == -480 and d.hour == 7)
assert(Date.toUnix(d) == 296667120) -- seconds since 1970-01-01T00:00:00Z
assert(tostring(Date.fromUnix(0)) == "1970-01-01T00:00:00Z")
assert(Date.new({ year = 2026, month = 3, day = 4 }).kind == "date-local")
assert(Date.is(d) and Date.valid(d))
assert(Date.parse("2026-02-30") == nil) -- out-of-range fields
Load
F.data.load(src, opts?) or F.load(src, opts); src is a string, a buffer or (on hosts with files) a path. Text
formats sniff weakly (YAML, TOML and INI look alike; MessagePack and CBOR without its self-describe tag have no magic),
so pass format (a name or extension) unless the data comes from a named file:
local cfg = assert(F.data.load('title = "x"\n[server]\nport = 8080\n', { format = "toml" }))
assert(cfg.server.port == 8080)
local msg = assert(F.data.load(msgpackBytes, { format = "msgpack" }))
assert(msg.name == "ada" and msg.none == F.data.null)
assert(typeof(msg.raw) == "buffer" and buffer.len(msg.raw) == 3) -- bin -> buffer
local bad, err = F.data.load("{bad", { format = "json" })
assert(bad == nil and err.code == F.Err.CORRUPT)
print(tostring(err)) -- json: CORRUPT at 0x1: expected a key
local scores = assert(F.load("assets/scores.csv", { header = true, numbers = true })) -- the extension decides
assert(scores[1].name == "ada" and scores[1].score == 1200)
JSON
| Option | Direction | Meaning |
|---|---|---|
ordered |
decode | every object as a Types.object with its key order |
bigInts |
decode | "string": integers past 2^53 stay their exact text |
null |
decode | the value used for null (default F.data.null) |
lines |
both | JSON Lines: an array of values, one per line |
indent |
encode | pretty print: spaces per level, or the indent string |
sortKeys |
encode | plain tables' keys sorted (default true) |
emptyTable |
encode | "array" (default) or "object" for a plain {} |
ascii |
encode | escape non-ASCII as \uXXXX |
nan |
encode | "error" (default) or "null" for NaN / inf |
local v = assert(F.data.load('{"a": [1, 2.5, null, true], "e": {}}'))
local pretty = assert(F.saveString(v, { format = "json", indent = 2 }))
assert(string.find(pretty, '\n "a": [\n 1,', 1, true))
local rows = assert(F.data.load('{"id": 1}\n{"id": 2}\n', { format = "json", lines = true }))
assert(#rows == 2 and rows[2].id == 2)
assert(F.saveString(rows, { format = "json", lines = true }) == '{"id":1}\n{"id":2}\n')
XML
XML decodes to a "#document" node; doc.root is the root element.
XmlNode field |
Type | Meaning |
|---|---|---|
name |
string | the qualified element name ("svg:rect"), or "#document", "#comment", "#cdata", "#pi", "#doctype" |
attrs |
Types.object of strings |
attributes, in document order |
children |
{ XmlNode | string } |
child nodes; text is plain strings |
parent |
XmlNode? | the parent (unless parents = false) |
text |
string? | the content of comment / CDATA / PI / doctype nodes |
target |
string? | a PI's target |
ns |
string? | the element's namespace URI (with namespaces = true) |
root, declaration, doctype |
on the document: the root element, { version, encoding, standalone }, { name, publicId, systemId, ... } |
Decode options: comments, cdata, pi (keep those nodes; by default comments and PIs are dropped and CDATA
merges into the text), whitespace (keep whitespace-only text), namespaces, entities (extra named entities),
maxDepth. Encode options: indent, declaration (default true for a document), selfClose (default true).
local doc = assert(F.data.load([[<?xml version="1.0"?>
<library><book id="b1" lang="en">Dune <em>classic</em></book><!-- shelf 2 --><book id="b2"/></library>]],
{ format = "xml", comments = true }))
local lib = doc.root
assert(lib.name == "library" and #lib.children == 3)
local function text(node) -- all character data below a node
local out = {}
for _, c in node.children do
table.insert(out, if type(c) == "string" then c else text(c))
end
return table.concat(out)
end
local book = lib.children[1]
assert(book.attrs.id == "b1" and text(book) == "Dune classic")
assert(table.concat(F.Types.keys(book.attrs), ",") == "id,lang") -- attribute order kept
assert(lib.children[2].name == "#comment" and lib.children[2].text == " shelf 2 ")
-- edit: add an element, then write the document back
local new = { name = "book", attrs = F.Types.object({ id = "b3" }, { "id" }), children = { "Emma" } }
table.insert(lib.children, new)
local xml = assert(F.saveString(doc, { format = "xml" }))
assert(string.find(xml, '<book id="b3">Emma</book></library>', 1, true))
YAML
Maps decode to Types.object, ~ / null to F.data.null; anchors and aliases become shared tables (the same
Luau table twice). Decode options: all (every document of a multi-document stream, as an array), tags
("ignore" resolves unknown tags as untagged), null, maxAliasNodes (the alias-bomb guard), maxDepth. Encode
options: indent (default 2), all, explicitStart, sortKeys. Shared tables are written as anchors.
local y = assert(F.data.load("base: &b {color: red}\nitem: *b\nlist: [1, two, ~]\n", { format = "yaml" }))
assert(y.item == y.base and y.item.color == "red")
assert(y.list[3] == F.data.null)
local docs = assert(F.data.load("---\nname: a\n---\nname: b\n", { format = "yaml", all = true }))
assert(#docs == 2 and docs[2].name == "b")
local out = assert(F.saveString(y, { format = "yaml" }))
assert(string.find(out, "base: &", 1, true) and string.find(out, "item: *", 1, true))
YAML 1.2 core schema: yes / no / on / off are strings, not booleans.
TOML
Tables decode to Types.object (file order kept), datetimes to Types.Date (all four kinds). Decode option:
bigInts. Encode options: inline (every sub-table inline), indent (indent keys of nested sections).
local cfg = assert(F.data.load([=[
title = "demo"
[server]
port = 8080
started = 1979-05-27T07:32:00-08:00
[[users]]
name = "ada"
[[users]]
name = "bob"
]=], { format = "toml" }))
assert(cfg.server.port == 8080 and #cfg.users == 2)
assert(F.Types.Date.is(cfg.server.started) and tostring(cfg.server.started) == "1979-05-27T07:32:00-08:00")
cfg.server.port = 9090
local text = assert(F.saveString(cfg, { format = "toml" }))
assert(string.find(text, "[server]\nport = 9090", 1, true))
CSV
Rows decode to arrays of strings; with header = true the first row names the columns and every other row is a
Types.object keyed by them (column order kept). Fields are strings unless numbers = true.
| Option | Direction | Meaning |
|---|---|---|
header |
both | decode: rows as records; encode (records): write the header row (default true) |
numbers |
decode | numeric fields become numbers |
delimiter |
both | one byte (default ","; decode also "auto": picks among , ; tab |) |
quote |
both | the quote byte (default ") |
maxRows |
decode | refuse more rows |
newline |
encode | row terminator (default "\r\n") |
quoteAll |
encode | quote every field |
columns |
encode | record column order (default: first-seen) |
local rows = assert(F.data.load(csvText, { format = "csv" }))
assert(rows[1][1] == "name" and rows[4][1] == "lee, jr") -- quoted field with a comma
local recs = assert(F.data.load(csvText, { format = "csv", header = true, numbers = true }))
assert(#recs == 3 and recs[2].name == "bob" and recs[2].score == 950)
table.insert(recs, F.Types.object({ name = "cy", score = 10, level = 1 }, { "name", "score", "level" }))
local out = assert(F.saveString(recs, { format = "csv", newline = "\n" }))
assert(string.sub(out, 1, 17) == "name,score,level\n")
assert(string.find(out, '"lee, jr",400,1\ncy,10,1\n', 1, true))
local tsv = assert(F.saveString({ { "a", "b" }, { "1", "2" } }, { format = "csv", delimiter = "\t" }))
assert(tsv == "a\tb\r\n1\t2\r\n")
INI
A Types.object of sections, each a Types.object of string values; keys before the first section sit at the top
level. Decode options: types (numbers and true / false / yes / no / on / off become numbers / booleans),
inlineComments (strip ;... / #...), allowNoValue, strict (duplicates are errors). Encode option:
delimiter (default " = ").
local ini = assert(F.data.load("top = 1\n[server]\nhost = example.com\nport = 80\n", { format = "ini" }))
assert(ini.top == "1" and ini.server.port == "80") -- strings by default
local typed = assert(F.data.load("[s]\nport = 80\ndebug = yes\n", { format = "ini", types = true }))
assert(typed.s.port == 80 and typed.s.debug == true)
local text = assert(F.saveString(ini, { format = "ini" }))
assert(string.find(text, "[server]\nhost = example.com\n", 1, true))
MessagePack, CBOR
Both decode maps to Types.object (any key type), bin / byte strings to buffers, timestamps (msgpack ext -1, CBOR
tags 0 / 1) to Types.Date. Decode options: all (a stream of concatenated values, as an array), bigInts,
null, maxDepth; msgpack timestamps / ordered (default true), CBOR dates (default true). Encode options:
all, sortKeys, emptyTable ("array" or "map"); msgpack float32, stringsAsBin; CBOR compactFloats,
dates ("string" or "epoch"), selfDescribe (prefix tag 55799, which also makes the bytes detectable).
local c = assert(F.data.load(cborBytes, { format = "cbor" }))
assert(c.name == "ada" and c.ratio == 0.5 and c.tags[1] == "x")
local packed = assert(F.save({ id = 7, blob = buffer.fromstring("\0\1") }, nil, { format = "msgpack" }))
local back = assert(F.data.load(packed, { format = "msgpack" }))
assert(back.id == 7 and buffer.len(back.blob) == 2)
local tagged = assert(F.save({ when = F.Types.Date.fromUnix(0) }, nil, { format = "cbor", selfDescribe = true }))
assert(F.detect(tagged) == "cbor")
assert(F.data.load(tagged).when.year == 1970)
Extension and tag values without a mapping keep their type: F.format("msgpack").ext(type, data) /
isExt(v); F.format("cbor").tagged(tag, value), isTagged, simple(n), undefined.
Plist
XML and binary (bplist00) plists load; writing is XML. dict is a Types.object, <date> a Types.Date,
<data> a buffer, binary UIDs F.format("plist").uid(n) values. Decode option: bigInts; encode option:
sortKeys (plain tables).
local p = assert(F.data.load(bplistBytes)) -- binary plists are detected
assert(p.CFBundleName == "Demo" and p.Version == 3 and p.Beta == false)
assert(p.Built.year == 2026 and typeof(p.Icon) == "buffer")
local xml = assert(F.saveString(p, { format = "plist" }))
assert(string.find(xml, "<key>CFBundleName</key>\n\t<string>Demo</string>", 1, true))
Godot resources (.tres)
Godot text resources load as a table: type (the resource class), format, uid, scriptClass, external (the
ext_resource list: id, type, path, uid), subresources (id, type, properties) and properties (the
[resource] section, in file order). Godot values that Luau lacks are { type, args } tables: Vector3(1, 2, 3) is
{ type = "Vector3", args = { 1, 2, 3 } }, likewise Color, Transform3D, PackedFloat32Array, NodePath,
&"StringName" (type = "StringName"), SubResource("id") and ExtResource("id"); Object(Class, ...) has class
and its properties in args[1]. Godot dictionaries keep their order and may have number keys. Godot tells integers
from floats, so integral floats read from a file (1.0) are written back as floats.
F.format("tres") and the codec F._internal("codecs/text/GodotVariant") (value, format, isCtor, ctor)
help with the values. Scenes (.tscn) are models: see the scene guide.
local res = assert(F.data.load(tresText))
assert(res.type == "StandardMaterial3D" and res.external[1].path == "res://brick.png")
local c = res.properties.albedo_color
assert(c.type == "Color" and c.args[1] == 0.8)
assert(res.properties.albedo_texture.type == "ExtResource")
res.properties.metallic = 0.5
local text = assert(F.saveString(res, { format = "tres" }))
assert(string.find(text, "metallic = 0.5", 1, true) and string.find(text, "roughness = 1.0", 1, true))
Save and convert
Data values have no kind, so saving needs opts.format or a destination extension (an XmlNode document too):
F.save(value, nil, { format = "json" }) returns a buffer, F.saveString a string. F.convert(src, format, opts)
loads with opts (where opts.format names the input's format) and saves as format with the same options; a file
name hint (name = "in.json") also names the input:
local yaml = assert(F.convert('{"name": "ada", "tags": ["x"]}', "yaml", { name = "in.json" }))
local v = assert(F.data.load(yaml, { format = "yaml" }))
assert(v.name == "ada" and v.tags[1] == "x")
local toml = assert(F.saveString(v, { format = "toml" }))
local mp = assert(F.save(v, nil, { format = "msgpack" }))
assert(F.data.load(mp, { format = "msgpack" }).tags[1] == "x")
What survives a conversion: everything maps across except types a target lacks. TOML has no null (F.data.null
is an error there), CSV needs rows, INI needs sections of scalars, JSON has no bytes or dates (encode them yourself:
tostring(date)), plist cannot hold null.
Formats
| Format | Read | Write | Decodes to |
|---|---|---|---|
json (.json, .jsonl, .ndjson, .geojson) |
yes | yes | values; objects plain (or ordered) |
xml (.xml, .xsd, .rss, .atom, .xhtml, ...) |
yes | yes | #document XmlNode tree |
yaml (.yaml, .yml) |
yes | yes | values, ordered maps, shared aliases |
toml (.toml) |
yes | yes | ordered tables, Dates |
csv (.csv, .tsv) |
yes | yes | rows (arrays or records) |
ini (.ini, .cfg, .conf, .desktop) |
yes | yes | sections of strings |
msgpack (.msgpack, .mpk) |
yes | yes | values, buffers, Dates |
cbor (.cbor) |
yes | yes | values, buffers, Dates, tagged values |
plist (.plist) |
XML + binary | XML | values, buffers, Dates |
nbt (.nbt, .dat, .mcstructure), snbt (.snbt) |
yes | yes | typed values: see Minecraft and voxels |
tres (.tres) |
yes | yes | Godot resources: sections, Variant values as { type, args } |
Full capability list: FORMATS.md.
Gotchas
nilcannot sit in a table: checkv == F.data.null, notv == nil, for JSON / YAML / CBOR nulls. Assigningnilremoves the key (it is not written).- Luau has one number type:
1.0reads back as1, and TOML / plist write integral floats as integers. Integers past 2^53 needbigInts = "string"to survive. - An empty plain table
{}is written as an array; useF.Types.object()(oremptyTable = "object") for{}. - Text formats sniff weakly: give
format(or a file name) for YAML, TOML, INI, CSV, MessagePack and CBOR without its self-describe tag. - XML whitespace-only text between elements is dropped by default (
whitespace = truekeeps it); text is never trimmed otherwise. - YAML aliases are shared tables: editing
y.itemalso editsy.base.