function json(filePath)
local filePath = getWorkingDirectory()..'\\config\\'..(filePath:find('(.+).json') and filePath or filePath..'.json')
local class = {}
if not doesDirectoryExist(getWorkingDirectory()..'\\config') then
createDirectory(getWorkingDirectory()..'\\config')
end
function class:Save(tbl)
if tbl then
local F = io.open(filePath, 'w')
F:write(encodeJson(tbl) or {})
F:close()
return true, 'ok'
end
return false, 'table = nil'
end
function class:Load(defaultTable)
if not doesFileExist(filePath) then
class:Save(defaultTable or {})
end
local F = io.open(filePath, 'r+')
local TABLE = decodeJson(F:read() or {})
F:close()
for def_k, def_v in next, defaultTable do
if TABLE[def_k] == nil then
TABLE[def_k] = def_v
end
end
return TABLE
end
return class
end
--require('jCfg')
local settings = json('cfg123.json'):Load({
['text'] = 'hello world',
['number'] = 550,
['state'] = true
})
function main()
while not isSampAvailable() do wait(0) end
sampRegisterChatCommand('json.print', function()
for k, v in next, settings do
sampAddChatMessage(k..' = '..tostring(v), -1)
end
end)
sampRegisterChatCommand('json.append', function(arg)
if arg:match('(.+) = (.+)') then
local k, v = arg:match('(.+) = (.+)')
settings[k] = v
json('cfg123.json'):Save(settings)
sampAddChatMessage('ok!', -1)
else
sampAddChatMessage('incorrect arg', -1)
end
end)
wait(-1)
end