Convert data between formats
The encoder library converts Lua data to transport formats — JSON for APIs, base64 for files, hex for hashes.
Function reference
| Function | Example | Result |
|---|---|---|
encoder.json(v) |
encoder.json({ a = 1 }) |
'{"a":1}' |
encoder.base64(s) |
encoder.base64("hello") |
"aGVsbG8=" |
encoder.base36(s) |
encoder.base36("test") |
"wanek4" |
encoder.hex(s) |
encoder.hex("hello") |
"68656c6c6f" |
encoder.rot13(s) |
encoder.rot13("hello") |
"uryyb" |
encoder.json — The most used function
-- Prepare data for an HTTP POST
local payload = encoder.json({
name = "John",
items = { 1, 2, 3 },
metadata = { source = "wapka" }
})
-- → '{"name":"John","items":[1,2,3],"metadata":{"source":"wapka"}}'
-- Send to an external API
http.post("https://api.example.com/webhook", {
body = payload,
headers = { ["content-type"] = "application/json" }
})
Uses JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES — Unicode stays readable, slashes stay clean.
encoder.base64 — Encode for transport
-- Encode binary data for JSON APIs
local encoded = encoder.base64("binary content here")
-- → "YmluYXJ5IGNvbnRlbnQgaGVyZQ=="
-- Use as part of a data URI
local image_data = encoder.base64(raw_image_bytes)
local src = "data:image/png;base64," .. image_data
encoder.hex — Binary to hex
-- Common in hashing and crypto workflows
local hex = encoder.hex("hello")
-- → "68656c6c6f"
-- Each byte becomes 2 hex characters
print(#hex) -- 10 (5 bytes → 10 hex chars)
encoder.base36 — Compact encoding
-- Shorter than hex, safe for filenames
local encoded = encoder.base36("test")
-- → "wanek4"
encoder.rot13 — Simple obfuscation
-- Not security — just hides text from casual reading
encoder.rot13("hello") → "uryyb"
encoder.rot13("uryyb") → "hello" -- symmetric
Practical: webhook notification
app:post("/notify", function(ctx)
local event = {
type = "user.created",
user_id = 42,
timestamp = os.date("!%Y-%m-%dT%H:%M:%SZ")
}
local body = encoder.json(event)
local sig = hash.hmac(WEBHOOK_SECRET, body)
local res = http.post("https://api.partner.com/webhooks", {
body = body,
headers = {
["content-type"] = "application/json",
["x-signature"] = sig
}
})
return { sent = res.status == 200 }
end)
Next: Decoder — parse incoming data.