A lightweight TOON parser for Lua.
cp toon.lua /path/to/your/project/local toon = require("toon")local data = {
name = "Alice",
age = 30,
tags = {"lua", "programming"},
metadata = {version = "1.0"}
}
local str = toon.encode(data)
print(str)Output:
name: Alice
age: 30
tags[2]: lua,programming
metadata:
version: 1.0
local input = [[
name: Bob
age: 25
scores[3]: 95,87,92
contact:
email: [email protected]
]]
local data = toon.decode(input)
print(data.name) -- Bob
print(data.scores[1]) -- 95
print(data.contact.email) -- [email protected]Encode a Lua value to TOON string.
Options:
indent(number): spaces per indent level (default: 2)delimiter(string): array delimiter -",","|","\t"(default: ",")keyFolding(string): fold nested keys into dotted notation -"safe"(default: nil)
Decode a TOON string to Lua table.
Options:
strict(boolean): strict parsing mode (default: false)pathExpansion(boolean): expand dotted keys into nested tables (default: false)
Inline arrays:
toon.encode({tags = {"a", "b"}}) -- tags[2]: a,bTabular arrays:
toon.encode({users = {{id = 1, name = "A"}, {id = 2, name = "B"}}})
-- users[2]{id,name}:
-- 1,A
-- 2,BTest case generation and final validation modifications both utilize AI-assisted technology (MiniMax M2.1), which may result in certain edge cases not being fully covered.
These tests were generated based on the TOON specification.
lua test.luaThis example demonstrates mutual conversion between TOON and JSON using json.lua, following the conversion examples in the TOON examples.
lua example.luaCode review was performed using AI-assisted tools (gemini-code-assist), with modifications made based on its suggestions.