Files

164 lines
5.7 KiB
Lua
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
--[[--
宽松 JSON:书源规则里的选项常写单引号、尾逗号、不加引号的键。数组与对象用元表区分,null 是 Json.null,
JsonPath 因此能区分 [] / {} / null。
@module moonsource_legadorule.json
--]]
local Json = {}
local Arr, Obj = {}, {}
Json.Arr, Json.Obj = Arr, Obj
Json.null = setmetatable({}, { __tostring = function() return "null" end })
function Json.isArray(v) return type(v) == "table" and getmetatable(v) == Arr end
function Json.isObject(v) return type(v) == "table" and getmetatable(v) == Obj end
function Json.newArray() return setmetatable({}, Arr) end
function Json.newObject() return setmetatable({}, Obj) end
local function utf8(cp)
if cp < 0x80 then return string.char(cp) end
if cp < 0x800 then return string.char(0xC0 + math.floor(cp / 64), 0x80 + cp % 64) end
if cp < 0x10000 then
return string.char(0xE0 + math.floor(cp / 0x1000), 0x80 + math.floor(cp / 64) % 64, 0x80 + cp % 64)
end
return string.char(0xF0 + math.floor(cp / 0x40000), 0x80 + math.floor(cp / 0x1000) % 64,
0x80 + math.floor(cp / 64) % 64, 0x80 + cp % 64)
end
local ESC = { n = "\n", t = "\t", r = "\r", b = "\b", f = "\f", ["/"] = "/", ["\\"] = "\\", ['"'] = '"', ["'"] = "'" }
local function fail(s, i, what) error("JSON " .. what .. " @" .. i, 0) end
local function skip(s, i)
while true do
i = s:find("[^%s]", i) or #s + 1
if s:sub(i, i + 1) == "//" then
i = (s:find("\n", i, true) or #s) + 1
elseif s:sub(i, i + 1) == "/*" then
local _, e = s:find("*/", i + 2, true)
i = (e or #s) + 1
else
return i
end
end
end
local value
local function str(s, i)
local q = s:sub(i, i)
local out, j = {}, i + 1
while true do
local k = s:find("[\\" .. q .. "]", j)
if not k then fail(s, i, "unterminated string") end
out[#out + 1] = s:sub(j, k - 1)
if s:sub(k, k) == q then return table.concat(out), k + 1 end
local c = s:sub(k + 1, k + 1)
if c == "u" then
local cp = tonumber(s:sub(k + 2, k + 5), 16) or 63
j = k + 6
if cp >= 0xD800 and cp < 0xDC00 and s:sub(j, j + 1) == "\\u" then
local lo = tonumber(s:sub(j + 2, j + 5), 16)
if lo and lo >= 0xDC00 and lo < 0xE000 then
cp = 0x10000 + (cp - 0xD800) * 0x400 + (lo - 0xDC00)
j = j + 6
end
end
out[#out + 1] = utf8(cp)
else
out[#out + 1] = ESC[c] or c
j = k + 2
end
end
end
function value(s, i)
i = skip(s, i)
local c = s:sub(i, i)
if c == "{" then
local obj = setmetatable({}, Obj)
i = skip(s, i + 1)
while s:sub(i, i) ~= "}" do
local key
local q = s:sub(i, i)
if q == '"' or q == "'" then
key, i = str(s, i)
else
key = s:match("^[%w_$%-]+", i)
if not key then fail(s, i, "bad key") end
i = i + #key
end
i = skip(s, i)
if s:sub(i, i) ~= ":" then fail(s, i, "expected :") end
obj[key], i = value(s, i + 1)
i = skip(s, i)
if s:sub(i, i) == "," then i = skip(s, i + 1) elseif s:sub(i, i) ~= "}" then fail(s, i, "expected }") end
end
return obj, i + 1
elseif c == "[" then
local arr = setmetatable({}, Arr)
i = skip(s, i + 1)
local n = 0
while s:sub(i, i) ~= "]" do
n = n + 1
arr[n], i = value(s, i)
i = skip(s, i)
if s:sub(i, i) == "," then i = skip(s, i + 1) elseif s:sub(i, i) ~= "]" then fail(s, i, "expected ]") end
end
return arr, i + 1
elseif c == '"' or c == "'" then
return str(s, i)
end
local num = s:match("^-?%d+%.?%d*[eE]?[+-]?%d*", i)
if num and num ~= "" and num ~= "-" then return tonumber(num) or fail(s, i, "bad number"), i + #num end
local word = s:match("^%a+", i)
if word == "true" then return true, i + 4 end
if word == "false" then return false, i + 5 end
if word == "null" then return Json.null, i + 4 end
fail(s, i, "unexpected token")
end
--- 解码;失败返回 nil, 错误。
function Json.decode(s)
local ok, v, i = pcall(value, s, 1)
if not ok then return nil, v end
i = skip(s, i)
if i <= #s then return nil, "JSON trailing data" end
return v
end
local function quote(s)
return '"' .. s:gsub('[%c"\\]', function(c)
if c == '"' then return '\\"' end
if c == "\\" then return "\\\\" end
if c == "\n" then return "\\n" end
if c == "\r" then return "\\r" end
if c == "\t" then return "\\t" end
return string.format("\\u%04x", c:byte())
end) .. '"'
end
--- 编码;数组 / 对象按元表,未标记的表有 [1] 当数组。键序排序,输出稳定。
function Json.encode(v)
local t = type(v)
if v == Json.null or v == nil then return "null" end
if t == "string" then return quote(v) end
if t == "number" then
if v == math.floor(v) and math.abs(v) < 1e15 then return string.format("%d", v) end
return tostring(v)
end
if t == "boolean" then return tostring(v) end
local out = {}
if getmetatable(v) == Arr or (getmetatable(v) ~= Obj and v[1] ~= nil) then
for k = 1, #v do out[k] = Json.encode(v[k]) end
return "[" .. table.concat(out, ",") .. "]"
end
local keys = {}
for k in pairs(v) do keys[#keys + 1] = k end
table.sort(keys, function(a, b) return tostring(a) < tostring(b) end)
for _, k in ipairs(keys) do out[#out + 1] = quote(tostring(k)) .. ":" .. Json.encode(v[k]) end
return "{" .. table.concat(out, ",") .. "}"
end
return Json