tinytoml/spec/encoder.lua
FourierTransformer 63efdd4c1e
Add TOML Encoder (#13)
* added a TOML encoder

* updated CI/CD to run toml-test encoder suite
2026-01-03 13:45:52 -06:00

49 lines
1.1 KiB
Lua

#!/usr/bin/env lua
local cjson = require("cjson")
local tinytoml = require("tinytoml")
local exploded_json = cjson.decode(io.read("*a"))
local exact_floats = {
["+inf"] = math.huge,
["inf"] = math.huge,
["-inf"] = -math.huge,
["+nan"] = (0/0),
["nan"] = (0/0),
["-nan"] = (-(0/0)),
}
local function clear_toml_test_tag(table_to_clear)
if table_to_clear["type"] ~= nil and table_to_clear["value"] ~= nil then
if table_to_clear["type"] == "integer" then
return tonumber(table_to_clear["value"])
elseif table_to_clear["type"] == "float" then
if exact_floats[table_to_clear["value"]] then
return exact_floats[table_to_clear["value"]]
else
return tonumber(table_to_clear["value"])
end
elseif table_to_clear["type"] == "bool" then
return table_to_clear["value"] == "true"
else
return table_to_clear["value"]
end
else
for k, v in pairs(table_to_clear) do
table_to_clear[k] = clear_toml_test_tag(v)
end
end
return table_to_clear
end
clear_toml_test_tag(exploded_json)
print(tinytoml.encode(exploded_json))