1
0
mirror of https://github.com/kepler155c/opus synced 2024-11-15 21:24:49 +00:00
opus/sys/apis/json.lua

221 lines
5.0 KiB
Lua
Raw Normal View History

2016-12-11 19:24:52 +00:00
-- credit ElvishJerricco
-- http://pastebin.com/raw.php?i=4nRg9CHU
local json = { }
------------------------------------------------------------------ utils
local controls = {["\n"]="\\n", ["\r"]="\\r", ["\t"]="\\t", ["\b"]="\\b", ["\f"]="\\f", ["\""]="\\\"", ["\\"]="\\\\"}
local function isArray(t)
2018-01-24 22:39:38 +00:00
local max = 0
for k,v in pairs(t) do
if type(k) ~= "number" then
return false
elseif k > max then
max = k
end
end
return max == #t
2016-12-11 19:24:52 +00:00
end
local whites = {['\n']=true; ['\r']=true; ['\t']=true; [' ']=true; [',']=true; [':']=true}
local function removeWhite(str)
2018-01-24 22:39:38 +00:00
while whites[str:sub(1, 1)] do
str = str:sub(2)
end
return str
2016-12-11 19:24:52 +00:00
end
------------------------------------------------------------------ encoding
local function encodeCommon(val, pretty, tabLevel, tTracking)
2018-01-24 22:39:38 +00:00
local str = ""
-- Tabbing util
local function tab(s)
str = str .. ("\t"):rep(tabLevel) .. s
end
local function arrEncoding(val, bracket, closeBracket, iterator, loopFunc)
str = str .. bracket
if pretty then
str = str .. "\n"
tabLevel = tabLevel + 1
end
for k,v in iterator(val) do
tab("")
loopFunc(k,v)
str = str .. ","
if pretty then str = str .. "\n" end
end
if pretty then
tabLevel = tabLevel - 1
end
if str:sub(-2) == ",\n" then
str = str:sub(1, -3) .. "\n"
elseif str:sub(-1) == "," then
str = str:sub(1, -2)
end
tab(closeBracket)
end
-- Table encoding
if type(val) == "table" then
assert(not tTracking[val], "Cannot encode a table holding itself recursively")
tTracking[val] = true
if isArray(val) then
arrEncoding(val, "[", "]", ipairs, function(k,v)
str = str .. encodeCommon(v, pretty, tabLevel, tTracking)
end)
else
arrEncoding(val, "{", "}", pairs, function(k,v)
assert(type(k) == "string", "JSON object keys must be strings", 2)
str = str .. encodeCommon(k, pretty, tabLevel, tTracking)
str = str .. (pretty and ": " or ":") .. encodeCommon(v, pretty, tabLevel, tTracking)
end)
end
-- String encoding
elseif type(val) == "string" then
str = '"' .. val:gsub("[%c\"\\]", controls) .. '"'
-- Number encoding
elseif type(val) == "number" or type(val) == "boolean" then
str = tostring(val)
else
error("JSON only supports arrays, objects, numbers, booleans, and strings", 2)
end
return str
2016-12-11 19:24:52 +00:00
end
function json.encode(val)
2018-01-24 22:39:38 +00:00
return encodeCommon(val, false, 0, {})
2016-12-11 19:24:52 +00:00
end
function json.encodePretty(val)
2018-01-24 22:39:38 +00:00
return encodeCommon(val, true, 0, {})
2016-12-11 19:24:52 +00:00
end
function json.encodeToFile(path, val)
2018-01-24 22:39:38 +00:00
local file = io.open(path, "w")
assert(file, "Unable to open file")
file:write(json.encodePretty(val))
file:close()
end
2016-12-11 19:24:52 +00:00
------------------------------------------------------------------ decoding
local decodeControls = {}
for k,v in pairs(controls) do
2018-01-24 22:39:38 +00:00
decodeControls[v] = k
2016-12-11 19:24:52 +00:00
end
local function parseBoolean(str)
2018-01-24 22:39:38 +00:00
if str:sub(1, 4) == "true" then
return true, removeWhite(str:sub(5))
else
return false, removeWhite(str:sub(6))
end
2016-12-11 19:24:52 +00:00
end
local function parseNull(str)
2018-01-24 22:39:38 +00:00
return nil, removeWhite(str:sub(5))
2016-12-11 19:24:52 +00:00
end
local numChars = {['e']=true; ['E']=true; ['+']=true; ['-']=true; ['.']=true}
local function parseNumber(str)
2018-01-24 22:39:38 +00:00
local i = 1
while numChars[str:sub(i, i)] or tonumber(str:sub(i, i)) do
i = i + 1
end
local val = tonumber(str:sub(1, i - 1))
str = removeWhite(str:sub(i))
return val, str
2016-12-11 19:24:52 +00:00
end
local function parseString(str)
2018-01-24 22:39:38 +00:00
str = str:sub(2)
local s = ""
while str:sub(1,1) ~= "\"" do
local next = str:sub(1,1)
str = str:sub(2)
assert(next ~= "\n", "Unclosed string")
2016-12-11 19:24:52 +00:00
2018-01-24 22:39:38 +00:00
if next == "\\" then
local escape = str:sub(1,1)
str = str:sub(2)
2016-12-11 19:24:52 +00:00
2018-01-24 22:39:38 +00:00
next = assert(decodeControls[next..escape], "Invalid escape character")
end
2016-12-11 19:24:52 +00:00
2018-01-24 22:39:38 +00:00
s = s .. next
end
return s, removeWhite(str:sub(2))
2016-12-11 19:24:52 +00:00
end
function json.parseArray(str)
2018-01-24 22:39:38 +00:00
str = removeWhite(str:sub(2))
2016-12-11 19:24:52 +00:00
2018-01-24 22:39:38 +00:00
local val = {}
local i = 1
while str:sub(1, 1) ~= "]" do
local v
v, str = json.parseValue(str)
val[i] = v
i = i + 1
str = removeWhite(str)
end
str = removeWhite(str:sub(2))
return val, str
2016-12-11 19:24:52 +00:00
end
function json.parseValue(str)
2018-01-24 22:39:38 +00:00
local fchar = str:sub(1, 1)
if fchar == "{" then
return json.parseObject(str)
elseif fchar == "[" then
return json.parseArray(str)
elseif tonumber(fchar) ~= nil or numChars[fchar] then
return parseNumber(str)
elseif str:sub(1, 4) == "true" or str:sub(1, 5) == "false" then
return parseBoolean(str)
elseif fchar == "\"" then
return parseString(str)
elseif str:sub(1, 4) == "null" then
return parseNull(str)
end
2016-12-11 19:24:52 +00:00
end
function json.parseMember(str)
2018-01-24 22:39:38 +00:00
local k, val
k, str = json.parseValue(str)
val, str = json.parseValue(str)
return k, val, str
2016-12-11 19:24:52 +00:00
end
function json.parseObject(str)
2018-01-24 22:39:38 +00:00
str = removeWhite(str:sub(2))
2016-12-11 19:24:52 +00:00
2018-01-24 22:39:38 +00:00
local val = {}
while str:sub(1, 1) ~= "}" do
local k, v
k, v, str = json.parseMember(str)
val[k] = v
str = removeWhite(str)
end
str = removeWhite(str:sub(2))
return val, str
2016-12-11 19:24:52 +00:00
end
function json.decode(str)
2018-01-24 22:39:38 +00:00
str = removeWhite(str)
return json.parseValue(str)
2016-12-11 19:24:52 +00:00
end
function json.decodeFromFile(path)
2018-01-24 22:39:38 +00:00
local file = assert(fs.open(path, "r"))
local decoded = json.decode(file.readAll())
file.close()
return decoded
2016-12-11 19:24:52 +00:00
end
return json