← All Modules

json

JSON serialization. No require() needed.

Empty tables

Lua has one composite type covering both arrays and objects, so the encoder has to pick a shape for {}. The default is objectjson.encode({}) returns "{}". To express an empty JSON array, use json.array({}) (or just json.array()) which tags the table via a __jsontype = "array" metatable marker that the encoder honours regardless of contents.

json.encode({})                 -- "{}"
json.encode(json.array({}))     -- "[]"
json.encode(json.array({1,2}))  -- "[1,2]"
json.encode(json.object({"a"})) -- '{"1":"a"}'  (object with stringified key)

For non-empty tables the heuristic still applies: contiguous 1..N integer keys encode as arrays, anything else as objects. The helpers only matter when you need to override that.

yaml

YAML serialization. No require() needed.

toml

TOML serialization. No require() needed.