====== Lua Table to JSON ====== Простая библиотека которая экспортирует переменные LUA в формат JSON Пример использования: local tojson = require "tojson" print(tojson(os)) Код самой библиотеки tojson function fromArray(obj, recru) local result = "[" for i = 1,#obj do if not recru and type(obj[i]) == "table" then result = result .. '"' .. tostring(obj[i]) .. '",' else result = result .. toJson(obj[i], recru) .. "," end end return string.sub(result, 1, #result-1) .. "]" end function fromObject(obj, recru) local result = "{" for k, v in pairs(obj) do if not recru and type(obj[i]) == "table" then result = result .. toJson(k) .. ':"' .. tostring(v) .. '",' else result = result .. toJson(k) .. ":" .. toJson(v, recru) .. "," end end return string.sub(result, 1, #result-1) .. "}" end function isAssoc(obj) local n = 1 for k in pairs(obj) do if k ~= n then return false end n = n + 1 end return true end function toJson(any, recru) local atype = type(any) recru = recru == nil and true or recru if atype == "string" then return '"' .. any .. '"' elseif atype == "number" then return tostring(any) elseif atype == "table" then if isAssoc(any) then return fromArray(any, recru) else return fromObject(any, recru) end elseif atype == "nil" then return "NULL" elseif atype == "boolean" then return any and "true" or "false" else return '"' .. tostring(any) .. '"' end end return toJson