Files
ashlar/lib/lua/jsonInterface.lua
T
David Cernat c439ece04f [General] Kick players when their JSON files are not created correctly
There are various operating system quirks preventing this from working in all scenarios, but it is an improvement over the previous potential for crashes.
2018-02-14 01:44:57 +02:00

28 lines
740 B
Lua

local json = require ("dkjson");
local jsonInterface = {};
function jsonInterface.load(fileName)
local home = os.getenv("MOD_DIR") .. "/"
local file = assert(io.open(home .. fileName, 'r'), 'Error loading file: ' .. fileName);
local content = file:read("*all");
file:close();
return json.decode(content, 1, nil);
end
function jsonInterface.save(fileName, data, keyOrderArray)
local home = os.getenv("MOD_DIR") .. "/"
local content = json.encode(data, { indent = true, keyorder = keyOrderArray });
local file = io.open(home .. fileName, 'w+b');
if file ~= nil then
file:write(content);
file:close();
return true
else
return false
end
end
return jsonInterface;