dfhack/library/lua/json.lua

81 lines
1.7 KiB
Lua

2015-03-20 17:51:21 -06:00
local _ENV = mkmodule('json')
local internal = require 'json.internal'
2016-04-08 15:40:14 -06:00
local fs = dfhack.filesystem
2015-03-20 17:51:21 -06:00
encode_defaults = {
-- For compatibility with jsonxx (C++)
pretty = true,
indent = '\t',
}
function encode(data, options, msg)
local opts = {}
for k, v in pairs(encode_defaults) do opts[k] = v end
for k, v in pairs(options or {}) do opts[k] = v end
return internal:encode(data, msg, opts)
end
2016-04-08 15:40:14 -06:00
function encode_file(data, path, ...)
if fs.isdir(path) then
2015-03-20 17:51:21 -06:00
error('Is a directory: ' .. path)
end
local contents = encode(data, ...)
local f = io.open(path, 'w')
f:write(contents)
f:close()
end
function decode(data, msg)
return internal:decode(data, msg)
end
function decode_file(path, ...)
local f = io.open(path)
if not f then
error('Could not open ' .. path)
end
local contents = f:read('*all')
2015-03-20 17:51:21 -06:00
f:close()
return decode(contents, ...)
end
2016-04-08 15:40:14 -06:00
local _file = defclass()
function _file:init(opts)
self.path = opts.path
self.data = {}
self.exists = false
self:read(opts.strict)
end
function _file:read(strict)
if not fs.exists(self.path) then
if strict then
error('cannot read file: ' .. self.path)
else
self.data = {}
end
else
self.exists = true
self.data = decode_file(self.path)
end
return self.data
end
function _file:write(data)
if data then
self.data = data
end
encode_file(self.data, self.path)
self.exists = true
end
function _file:__tostring()
return ('<json file: %s>'):format(self.path)
end
function open(path, strict)
return _file{path=path, strict=strict}
end
2015-03-20 17:51:21 -06:00
return _ENV