pull/48/merge
DarhangeR 2023-04-29 16:42:53 +03:00 committed by GitHub
commit b907c78d2c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 386 additions and 391 deletions

1
.github/FUNDING.yml vendored
View File

@ -1 +0,0 @@
github: rxi

View File

@ -1,5 +1,4 @@
Copyright (c) 2020 rxi Copyright (c) 2020 rxi (v0.1.2) edit by DarhangeR (v0.1.4)
Permission is hereby granted, free of charge, to any person obtaining a copy of Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in this software and associated documentation files (the "Software"), to deal in

773
json.lua
View File

@ -1,388 +1,385 @@
-- --
-- json.lua -- json.lua
-- --
-- Copyright (c) 2020 rxi -- Copyright (c) 2020 rxi (v0.1.2) edit by DarhangeR (v0.1.4)
-- --
-- Permission is hereby granted, free of charge, to any person obtaining a copy of -- Permission is hereby granted, free of charge, to any person obtaining a copy of
-- this software and associated documentation files (the "Software"), to deal in -- this software and associated documentation files (the "Software"), to deal in
-- the Software without restriction, including without limitation the rights to -- the Software without restriction, including without limitation the rights to
-- use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies -- use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
-- of the Software, and to permit persons to whom the Software is furnished to do -- of the Software, and to permit persons to whom the Software is furnished to do
-- so, subject to the following conditions: -- so, subject to the following conditions:
-- --
-- The above copyright notice and this permission notice shall be included in all -- The above copyright notice and this permission notice shall be included in all
-- copies or substantial portions of the Software. -- copies or substantial portions of the Software.
-- --
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-- SOFTWARE. -- SOFTWARE.
-- --
local json = { _version = "0.1.2" } local pairs, string_format, error, rawget, next, string_char, type, ipairs, table_concat, tostring, select, tonumber = pairs, string.format, error, rawget, next, string.char, type, ipairs, table.concat, tostring, select, tonumber
local json = { _version = "0.1.4" };
-------------------------------------------------------------------------------
-- Encode -------------------------------------------------------------------------------
------------------------------------------------------------------------------- -- Encode
-------------------------------------------------------------------------------
local encode
local encode
local escape_char_map = {
[ "\\" ] = "\\", local escape_char_map = {
[ "\"" ] = "\"", [ "\\" ] = "\\",
[ "\b" ] = "b", [ "\"" ] = "\"",
[ "\f" ] = "f", [ "\b" ] = "b",
[ "\n" ] = "n", [ "\f" ] = "f",
[ "\r" ] = "r", [ "\n" ] = "n",
[ "\t" ] = "t", [ "\r" ] = "r",
} [ "\t" ] = "t",
};
local escape_char_map_inv = { [ "/" ] = "/" }
for k, v in pairs(escape_char_map) do local escape_char_map_inv = { [ "/" ] = "/" }
escape_char_map_inv[v] = k for k, v in pairs(escape_char_map) do
end escape_char_map_inv[v] = k;
end;
local function escape_char(c) local function escape_char(c)
return "\\" .. (escape_char_map[c] or string.format("u%04x", c:byte())) return "\\" .. (escape_char_map[c] or string_format("u%04x", c:byte()));
end end;
local function encode_nil(rope)
local function encode_nil(val) rope[#rope + 1] = "null"
return "null" end;
end
local function encode_table(rope, val, stack)
-- Circular reference?
local function encode_table(val, stack) if stack[val] then error("circular reference") end
local res = {} stack[val] = true
stack = stack or {} if rawget(val, 1) ~= nil or next(val) == nil then
-- Treat as array -- check keys are valid and it is not sparse
-- Circular reference? local n = 0;
if stack[val] then error("circular reference") end for k in pairs(val) do
if type(k) ~= "number" then
stack[val] = true error("invalid table: mixed or invalid key types")
end
if rawget(val, 1) ~= nil or next(val) == nil then n = n + 1
-- Treat as array -- check keys are valid and it is not sparse end
local n = 0 if n ~= #val then
for k in pairs(val) do error("invalid table: sparse array")
if type(k) ~= "number" then end
error("invalid table: mixed or invalid key types") -- Encode
end rope[#rope + 1] = "["
n = n + 1 for i, v in ipairs(val) do
end if i > 1 then
if n ~= #val then rope[#rope + 1] = ","
error("invalid table: sparse array") end
end encode(rope, v, stack)
-- Encode end
for i, v in ipairs(val) do rope[#rope + 1] = "]"
table.insert(res, encode(v, stack)) else
end -- Treat as an object
stack[val] = nil rope[#rope + 1] = "{"
return "[" .. table.concat(res, ",") .. "]" local first = true;
for k, v in pairs(val) do
else if type(k) ~= "string" then
-- Treat as an object error("invalid table: mixed or invalid key types")
for k, v in pairs(val) do end
if type(k) ~= "string" then if not first then
error("invalid table: mixed or invalid key types") rope[#rope + 1] = ","
end end
table.insert(res, encode(k, stack) .. ":" .. encode(v, stack)) encode(rope, k, stack)
end rope[#rope + 1] = ":"
stack[val] = nil encode(rope, v, stack)
return "{" .. table.concat(res, ",") .. "}" first = false
end end
end rope[#rope + 1] = "}"
end
stack[val] = nil
local function encode_string(val) end;
return '"' .. val:gsub('[%z\1-\31\\"]', escape_char) .. '"'
end local function encode_string(rope, val)
rope[#rope + 1] = '"'
rope[#rope + 1] = val:gsub('[%z\1-\31\\"]', escape_char)
local function encode_number(val) rope[#rope + 1] = '"'
-- Check for NaN, -inf and inf end;
if val ~= val or val <= -math.huge or val >= math.huge then
error("unexpected number value '" .. tostring(val) .. "'") local function encode_number(rope, val)
end -- Check for NaN, -inf and inf
return string.format("%.14g", val) if val ~= val or val <= -math.huge or val >= math.huge then
end error("unexpected number value '" .. tostring(val) .. "'")
end
-- See www.cs.berkeley.edu/~wkahan/ieee754status/IEEE754.PDF
local type_func_map = { -- 17 digits suffice to losslessly represent 64-bit IEEE754 floats
[ "nil" ] = encode_nil, rope[#rope + 1] = ("%.17g"):format(val)
[ "table" ] = encode_table, end;
[ "string" ] = encode_string,
[ "number" ] = encode_number, local function encode_boolean(rope, val)
[ "boolean" ] = tostring, rope[#rope + 1] = val and "true" or "false"
} end;
local type_func_map = {
encode = function(val, stack) [ "nil" ] = encode_nil,
local t = type(val) [ "table" ] = encode_table,
local f = type_func_map[t] [ "string" ] = encode_string,
if f then [ "number" ] = encode_number,
return f(val, stack) [ "boolean" ] = encode_boolean,
end };
error("unexpected type '" .. t .. "'")
end function encode(rope, val, stack)
local t = type(val)
local encoder = type_func_map[t]
function json.encode(val) if encoder then
return ( encode(val) ) return encoder(rope, val, stack)
end end
error("unexpected type '" .. t .. "'")
end;
-------------------------------------------------------------------------------
-- Decode function json.encode(val)
------------------------------------------------------------------------------- local rope = {}
encode(rope, val, {})
local parse return table_concat(rope);
end;
local function create_set(...)
local res = {} -------------------------------------------------------------------------------
for i = 1, select("#", ...) do -- Decode
res[ select(i, ...) ] = true -------------------------------------------------------------------------------
end
return res local parse
end
local function create_set(...)
local space_chars = create_set(" ", "\t", "\r", "\n") local res = {};
local delim_chars = create_set(" ", "\t", "\r", "\n", "]", "}", ",") for i = 1, select("#", ...) do
local escape_chars = create_set("\\", "/", '"', "b", "f", "n", "r", "t", "u") res[ select(i, ...) ] = true
local literals = create_set("true", "false", "null") end
return res;
local literal_map = { end
[ "true" ] = true,
[ "false" ] = false, local space_chars = create_set(" ", "\t", "\r", "\n");
[ "null" ] = nil, local delim_chars = create_set(" ", "\t", "\r", "\n", "]", "}", ",");
} local escape_chars = create_set("\\", "/", '"', "b", "f", "n", "r", "t", "u");
local literals = create_set("true", "false", "null");
local function next_char(str, idx, set, negate) local literal_map = {
for i = idx, #str do [ "true" ] = true,
if set[str:sub(i, i)] ~= negate then [ "false" ] = false,
return i [ "null" ] = nil,
end };
end
return #str + 1 local function next_char(str, idx, set, negate)
end for i = idx, #str do
if set[str:sub(i, i)] ~= negate then
return i
local function decode_error(str, idx, msg) end
local line_count = 1 end
local col_count = 1 return #str + 1;
for i = 1, idx - 1 do end;
col_count = col_count + 1
if str:sub(i, i) == "\n" then local function decode_error(str, idx, msg)
line_count = line_count + 1 local line_count = 1
col_count = 1 local col_count = 1
end for i = 1, idx - 1 do
end col_count = col_count + 1
error( string.format("%s at line %d col %d", msg, line_count, col_count) ) if str:sub(i, i) == "\n" then
end line_count = line_count + 1
col_count = 1
end
local function codepoint_to_utf8(n) end
-- http://scripts.sil.org/cms/scripts/page.php?site_id=nrsi&id=iws-appendixa error(string_format("%s at line %d col %d", msg, line_count, col_count))
local f = math.floor end;
if n <= 0x7f then
return string.char(n) local function codepoint_to_utf8(n)
elseif n <= 0x7ff then -- http://scripts.sil.org/cms/scripts/page.php?site_id=nrsi&id=iws-appendixa
return string.char(f(n / 64) + 192, n % 64 + 128) local f = math.floor;
elseif n <= 0xffff then if n <= 0x7f then
return string.char(f(n / 4096) + 224, f(n % 4096 / 64) + 128, n % 64 + 128) return string_char(n)
elseif n <= 0x10ffff then elseif n <= 0x7ff then
return string.char(f(n / 262144) + 240, f(n % 262144 / 4096) + 128, return string_char(f(n / 64) + 192, n % 64 + 128)
f(n % 4096 / 64) + 128, n % 64 + 128) elseif n <= 0xffff then
end return string_char(f(n / 4096) + 224, f(n % 4096 / 64) + 128, n % 64 + 128)
error( string.format("invalid unicode codepoint '%x'", n) ) elseif n <= 0x10ffff then
end return string_char(f(n / 262144) + 240, f(n % 262144 / 4096) + 128,
f(n % 4096 / 64) + 128, n % 64 + 128)
end
local function parse_unicode_escape(s) error( string_format("invalid unicode codepoint '%x'", n) )
local n1 = tonumber( s:sub(1, 4), 16 ) end;
local n2 = tonumber( s:sub(7, 10), 16 )
-- Surrogate pair? local function parse_unicode_escape(s)
if n2 then local n1 = tonumber(s:sub(1, 4), 16);
return codepoint_to_utf8((n1 - 0xd800) * 0x400 + (n2 - 0xdc00) + 0x10000) local n2 = tonumber(s:sub(7, 10), 16);
else -- Surrogate pair?
return codepoint_to_utf8(n1) if n2 then
end return codepoint_to_utf8((n1 - 0xd800) * 0x400 + (n2 - 0xdc00) + 0x10000);
end else
return codepoint_to_utf8(n1);
end
local function parse_string(str, i) end;
local res = ""
local j = i + 1 local function parse_string(str, i)
local k = j local res = {};
local j = i + 1;
while j <= #str do local k = j;
local x = str:byte(j)
while j <= #str do
if x < 32 then local x = str:byte(j);
decode_error(str, j, "control character in string") if x < 32 then
decode_error(str, j, "control character in string")
elseif x == 92 then -- `\`: Escape elseif x == 92 then -- `\`: Escape
res = res .. str:sub(k, j - 1) res[#res + 1] = str:sub(k, j - 1)
j = j + 1 j = j + 1
local c = str:sub(j, j) local c = str:sub(j, j)
if c == "u" then if c == "u" then
local hex = str:match("^[dD][89aAbB]%x%x\\u%x%x%x%x", j + 1) local hex = str:match("^[dD][89aAbB]%x%x\\u%x%x%x%x", j + 1)
or str:match("^%x%x%x%x", j + 1) or str:match("^%x%x%x%x", j + 1)
or decode_error(str, j - 1, "invalid unicode escape in string") or decode_error(str, j - 1, "invalid unicode escape in string")
res = res .. parse_unicode_escape(hex) res[#res + 1] = parse_unicode_escape(hex)
j = j + #hex j = j + #hex
else else
if not escape_chars[c] then if not escape_chars[c] then
decode_error(str, j - 1, "invalid escape char '" .. c .. "' in string") decode_error(str, j - 1, "invalid escape char '" .. c .. "' in string")
end end
res = res .. escape_char_map_inv[c] res[#res + 1] = escape_char_map_inv[c]
end end
k = j + 1 k = j + 1
elseif x == 34 then -- `"`: End of string
elseif x == 34 then -- `"`: End of string res[#res + 1] = str:sub(k, j - 1)
res = res .. str:sub(k, j - 1) return table_concat(res), j + 1
return res, j + 1 end
end j = j + 1
end
j = j + 1 decode_error(str, i, "expected closing quote for string")
end return table_concat(res);
end;
decode_error(str, i, "expected closing quote for string")
end local function parse_number(str, i)
local x = next_char(str, i, delim_chars);
local s = str:sub(i, x - 1);
local function parse_number(str, i) local n = tonumber(s);
local x = next_char(str, i, delim_chars) if not n then
local s = str:sub(i, x - 1) decode_error(str, i, "invalid number '" .. s .. "'")
local n = tonumber(s) end
if not n then return n, x;
decode_error(str, i, "invalid number '" .. s .. "'") end;
end
return n, x local function parse_literal(str, i)
end local x = next_char(str, i, delim_chars)
local word = str:sub(i, x - 1)
if not literals[word] then
local function parse_literal(str, i) decode_error(str, i, "invalid literal '" .. word .. "'")
local x = next_char(str, i, delim_chars) end
local word = str:sub(i, x - 1) return literal_map[word], x;
if not literals[word] then end;
decode_error(str, i, "invalid literal '" .. word .. "'")
end local function parse_array(str, i)
return literal_map[word], x local res = {};
end local n = 1;
i = i + 1
while 1 do
local function parse_array(str, i) local x;
local res = {} i = next_char(str, i, space_chars, true)
local n = 1 -- Empty / end of array?
i = i + 1 if str:sub(i, i) == "]" then
while 1 do i = i + 1
local x break;
i = next_char(str, i, space_chars, true) end
-- Empty / end of array? -- Read token
if str:sub(i, i) == "]" then x, i = parse(str, i)
i = i + 1 res[n] = x
break n = n + 1
end -- Next token
-- Read token i = next_char(str, i, space_chars, true)
x, i = parse(str, i) local chr = str:sub(i, i);
res[n] = x i = i + 1
n = n + 1 if chr == "]" then
-- Next token break;
i = next_char(str, i, space_chars, true) end
local chr = str:sub(i, i) if chr ~= "," then
i = i + 1 decode_error(str, i, "expected ']' or ','")
if chr == "]" then break end end
if chr ~= "," then decode_error(str, i, "expected ']' or ','") end end
end return res, i;
return res, i end;
end
local function parse_object(str, i)
local res = {};
local function parse_object(str, i) i = i + 1
local res = {} while 1 do
i = i + 1 local key, val;
while 1 do i = next_char(str, i, space_chars, true)
local key, val -- Empty / end of object?
i = next_char(str, i, space_chars, true) if str:sub(i, i) == "}" then
-- Empty / end of object? i = i + 1
if str:sub(i, i) == "}" then break;
i = i + 1 end
break -- Read key
end if str:sub(i, i) ~= '"' then
-- Read key decode_error(str, i, "expected string for key")
if str:sub(i, i) ~= '"' then end
decode_error(str, i, "expected string for key") key, i = parse(str, i)
end -- Read ':' delimiter
key, i = parse(str, i) i = next_char(str, i, space_chars, true)
-- Read ':' delimiter if str:sub(i, i) ~= ":" then
i = next_char(str, i, space_chars, true) decode_error(str, i, "expected ':' after key")
if str:sub(i, i) ~= ":" then end
decode_error(str, i, "expected ':' after key") i = next_char(str, i + 1, space_chars, true)
end -- Read value
i = next_char(str, i + 1, space_chars, true) val, i = parse(str, i)
-- Read value -- Set
val, i = parse(str, i) res[key] = val
-- Set -- Next token
res[key] = val i = next_char(str, i, space_chars, true)
-- Next token local chr = str:sub(i, i)
i = next_char(str, i, space_chars, true) i = i + 1
local chr = str:sub(i, i) if chr == "}" then
i = i + 1 break;
if chr == "}" then break end end
if chr ~= "," then decode_error(str, i, "expected '}' or ','") end if chr ~= "," then
end decode_error(str, i, "expected '}' or ','") end
return res, i end
end return res, i;
end;
local char_func_map = { local char_func_map = {
[ '"' ] = parse_string, [ '"' ] = parse_string,
[ "0" ] = parse_number, [ "0" ] = parse_number,
[ "1" ] = parse_number, [ "1" ] = parse_number,
[ "2" ] = parse_number, [ "2" ] = parse_number,
[ "3" ] = parse_number, [ "3" ] = parse_number,
[ "4" ] = parse_number, [ "4" ] = parse_number,
[ "5" ] = parse_number, [ "5" ] = parse_number,
[ "6" ] = parse_number, [ "6" ] = parse_number,
[ "7" ] = parse_number, [ "7" ] = parse_number,
[ "8" ] = parse_number, [ "8" ] = parse_number,
[ "9" ] = parse_number, [ "9" ] = parse_number,
[ "-" ] = parse_number, [ "-" ] = parse_number,
[ "t" ] = parse_literal, [ "t" ] = parse_literal,
[ "f" ] = parse_literal, [ "f" ] = parse_literal,
[ "n" ] = parse_literal, [ "n" ] = parse_literal,
[ "[" ] = parse_array, [ "[" ] = parse_array,
[ "{" ] = parse_object, [ "{" ] = parse_object,
} };
parse = function(str, idx)
parse = function(str, idx) local chr = str:sub(idx, idx);
local chr = str:sub(idx, idx) local f = char_func_map[chr];
local f = char_func_map[chr] if f then
if f then return f(str, idx);
return f(str, idx) end
end decode_error(str, idx, "unexpected character '" .. chr .. "'");
decode_error(str, idx, "unexpected character '" .. chr .. "'") end;
end
function json.decode(str)
if type(str) ~= "string" then
function json.decode(str) error("expected argument of type string, got " .. type(str));
if type(str) ~= "string" then end
error("expected argument of type string, got " .. type(str)) local res, idx = parse(str, next_char(str, 1, space_chars, true))
end idx = next_char(str, idx, space_chars, true)
local res, idx = parse(str, next_char(str, 1, space_chars, true)) if idx <= #str then
idx = next_char(str, idx, space_chars, true) decode_error(str, idx, "trailing garbage")
if idx <= #str then end
decode_error(str, idx, "trailing garbage") return res;
end end;
return res
end return json;
return json