Compare commits
16 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dbf4b2dd2e | ||
|
|
ee6abdecb2 | ||
|
|
11077824d7 | ||
|
|
6d080a1a66 | ||
|
|
2e76cfb067 | ||
|
|
8aa60078ca | ||
|
|
d1e3b0f5d0 | ||
|
|
69b714ad2b | ||
|
|
f049daf06c | ||
|
|
fd58f29876 | ||
|
|
d3f417d4d4 | ||
|
|
e1abe1c45c | ||
|
|
bee7ee3431 | ||
|
|
eb6e343c53 | ||
|
|
19cc024df6 | ||
|
|
e1dbe93f7c |
1
.github/FUNDING.yml
vendored
Normal file
1
.github/FUNDING.yml
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
github: rxi
|
||||||
2
LICENSE
2
LICENSE
@@ -1,4 +1,4 @@
|
|||||||
Copyright (c) 2015 rxi
|
Copyright (c) 2020 rxi
|
||||||
|
|
||||||
|
|
||||||
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
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ A lightweight JSON library for Lua
|
|||||||
* Implemented in pure Lua: works with 5.1, 5.2, 5.3 and JIT
|
* Implemented in pure Lua: works with 5.1, 5.2, 5.3 and JIT
|
||||||
* Fast: generally outperforms other pure Lua JSON implementations
|
* Fast: generally outperforms other pure Lua JSON implementations
|
||||||
([benchmark scripts](bench/))
|
([benchmark scripts](bench/))
|
||||||
* Tiny: around 290sloc, 9kb
|
* Tiny: around 280sloc, 9kb
|
||||||
* Proper error messages, *eg:* `expected '}' or ',' at line 203 col 30`
|
* Proper error messages, *eg:* `expected '}' or ',' at line 203 col 30`
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
116
json.lua
116
json.lua
@@ -1,13 +1,28 @@
|
|||||||
--
|
--
|
||||||
-- json.lua
|
-- json.lua
|
||||||
--
|
--
|
||||||
-- Copyright (c) 2015 rxi
|
-- Copyright (c) 2020 rxi
|
||||||
--
|
--
|
||||||
-- This library is free software; you can redistribute it and/or modify it
|
-- Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||||
-- under the terms of the MIT license. See LICENSE for details.
|
-- this software and associated documentation files (the "Software"), to deal in
|
||||||
|
-- the Software without restriction, including without limitation the rights to
|
||||||
|
-- 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
|
||||||
|
-- so, subject to the following conditions:
|
||||||
|
--
|
||||||
|
-- The above copyright notice and this permission notice shall be included in all
|
||||||
|
-- copies or substantial portions of the Software.
|
||||||
|
--
|
||||||
|
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
-- 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
|
||||||
|
-- SOFTWARE.
|
||||||
--
|
--
|
||||||
|
|
||||||
local json = { _version = "0.1.0" }
|
local json = { _version = "0.1.2" }
|
||||||
|
|
||||||
-------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------
|
||||||
-- Encode
|
-- Encode
|
||||||
@@ -16,23 +31,23 @@ local json = { _version = "0.1.0" }
|
|||||||
local encode
|
local encode
|
||||||
|
|
||||||
local escape_char_map = {
|
local escape_char_map = {
|
||||||
[ "\\" ] = "\\\\",
|
[ "\\" ] = "\\",
|
||||||
[ "\"" ] = "\\\"",
|
[ "\"" ] = "\"",
|
||||||
[ "\b" ] = "\\b",
|
[ "\b" ] = "b",
|
||||||
[ "\f" ] = "\\f",
|
[ "\f" ] = "f",
|
||||||
[ "\n" ] = "\\n",
|
[ "\n" ] = "n",
|
||||||
[ "\r" ] = "\\r",
|
[ "\r" ] = "r",
|
||||||
[ "\t" ] = "\\t",
|
[ "\t" ] = "t",
|
||||||
}
|
}
|
||||||
|
|
||||||
local escape_char_map_inv = { [ "\\/" ] = "/" }
|
local escape_char_map_inv = { [ "/" ] = "/" }
|
||||||
for k, v in pairs(escape_char_map) do
|
for k, v in pairs(escape_char_map) do
|
||||||
escape_char_map_inv[v] = k
|
escape_char_map_inv[v] = k
|
||||||
end
|
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
|
||||||
|
|
||||||
|
|
||||||
@@ -50,7 +65,7 @@ local function encode_table(val, stack)
|
|||||||
|
|
||||||
stack[val] = true
|
stack[val] = true
|
||||||
|
|
||||||
if val[1] ~= nil or next(val) == nil then
|
if rawget(val, 1) ~= nil or next(val) == nil then
|
||||||
-- Treat as array -- check keys are valid and it is not sparse
|
-- Treat as array -- check keys are valid and it is not sparse
|
||||||
local n = 0
|
local n = 0
|
||||||
for k in pairs(val) do
|
for k in pairs(val) do
|
||||||
@@ -189,9 +204,9 @@ end
|
|||||||
|
|
||||||
|
|
||||||
local function parse_unicode_escape(s)
|
local function parse_unicode_escape(s)
|
||||||
local n1 = tonumber( s:sub(3, 6), 16 )
|
local n1 = tonumber( s:sub(1, 4), 16 )
|
||||||
local n2 = tonumber( s:sub(9, 12), 16 )
|
local n2 = tonumber( s:sub(7, 10), 16 )
|
||||||
-- Surrogate pair?
|
-- Surrogate pair?
|
||||||
if n2 then
|
if n2 then
|
||||||
return codepoint_to_utf8((n1 - 0xd800) * 0x400 + (n2 - 0xdc00) + 0x10000)
|
return codepoint_to_utf8((n1 - 0xd800) * 0x400 + (n2 - 0xdc00) + 0x10000)
|
||||||
else
|
else
|
||||||
@@ -201,54 +216,42 @@ end
|
|||||||
|
|
||||||
|
|
||||||
local function parse_string(str, i)
|
local function parse_string(str, i)
|
||||||
local has_unicode_escape = false
|
local res = ""
|
||||||
local has_surrogate_escape = false
|
local j = i + 1
|
||||||
local has_escape = false
|
local k = j
|
||||||
local last
|
|
||||||
for j = i + 1, #str do
|
while j <= #str do
|
||||||
local x = str:byte(j)
|
local x = str:byte(j)
|
||||||
|
|
||||||
if x < 32 then
|
if x < 32 then
|
||||||
decode_error(str, j, "control character in string")
|
decode_error(str, j, "control character in string")
|
||||||
end
|
|
||||||
|
|
||||||
if last == 92 then -- "\\" (escape char)
|
elseif x == 92 then -- `\`: Escape
|
||||||
if x == 117 then -- "u" (unicode escape sequence)
|
res = res .. str:sub(k, j - 1)
|
||||||
local hex = str:sub(j + 1, j + 5)
|
j = j + 1
|
||||||
if not hex:find("%x%x%x%x") then
|
local c = str:sub(j, j)
|
||||||
decode_error(str, j, "invalid unicode escape in string")
|
if c == "u" then
|
||||||
end
|
local hex = str:match("^[dD][89aAbB]%x%x\\u%x%x%x%x", j + 1)
|
||||||
if hex:find("^[dD][89aAbB]") then
|
or str:match("^%x%x%x%x", j + 1)
|
||||||
has_surrogate_escape = true
|
or decode_error(str, j - 1, "invalid unicode escape in string")
|
||||||
else
|
res = res .. parse_unicode_escape(hex)
|
||||||
has_unicode_escape = true
|
j = j + #hex
|
||||||
end
|
|
||||||
else
|
else
|
||||||
local c = string.char(x)
|
|
||||||
if not escape_chars[c] then
|
if not escape_chars[c] then
|
||||||
decode_error(str, j, "invalid escape char '" .. c .. "' in string")
|
decode_error(str, j - 1, "invalid escape char '" .. c .. "' in string")
|
||||||
end
|
end
|
||||||
has_escape = true
|
res = res .. escape_char_map_inv[c]
|
||||||
end
|
end
|
||||||
last = nil
|
k = j + 1
|
||||||
|
|
||||||
elseif x == 34 then -- '"' (end of string)
|
elseif x == 34 then -- `"`: End of string
|
||||||
local s = str:sub(i + 1, j - 1)
|
res = res .. str:sub(k, j - 1)
|
||||||
if has_surrogate_escape then
|
return res, j + 1
|
||||||
s = s:gsub("\\u[dD][89aAbB]..\\u....", parse_unicode_escape)
|
|
||||||
end
|
|
||||||
if has_unicode_escape then
|
|
||||||
s = s:gsub("\\u....", parse_unicode_escape)
|
|
||||||
end
|
|
||||||
if has_escape then
|
|
||||||
s = s:gsub("\\.", escape_char_map_inv)
|
|
||||||
end
|
|
||||||
return s, j + 1
|
|
||||||
|
|
||||||
else
|
|
||||||
last = x
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
j = j + 1
|
||||||
end
|
end
|
||||||
|
|
||||||
decode_error(str, i, "expected closing quote for string")
|
decode_error(str, i, "expected closing quote for string")
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -373,7 +376,12 @@ function json.decode(str)
|
|||||||
if type(str) ~= "string" then
|
if type(str) ~= "string" then
|
||||||
error("expected argument of type string, got " .. type(str))
|
error("expected argument of type string, got " .. type(str))
|
||||||
end
|
end
|
||||||
return ( parse(str, next_char(str, 1, space_chars, true)) )
|
local res, idx = parse(str, next_char(str, 1, space_chars, true))
|
||||||
|
idx = next_char(str, idx, space_chars, true)
|
||||||
|
if idx <= #str then
|
||||||
|
decode_error(str, idx, "trailing garbage")
|
||||||
|
end
|
||||||
|
return res
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -66,10 +66,16 @@ end)
|
|||||||
|
|
||||||
|
|
||||||
test("strings", function()
|
test("strings", function()
|
||||||
|
local s = ""
|
||||||
|
assert( s == json.decode( json.encode(s) ) )
|
||||||
|
local s = "\\"
|
||||||
|
assert( s == json.decode( json.encode(s) ) )
|
||||||
local s = "Hello world"
|
local s = "Hello world"
|
||||||
assert( s == json.decode( json.encode(s) ) )
|
assert( s == json.decode( json.encode(s) ) )
|
||||||
local s = "\0 \13 \27"
|
local s = "\0 \13 \27"
|
||||||
assert( s == json.decode( json.encode(s) ) )
|
assert( s == json.decode( json.encode(s) ) )
|
||||||
|
local s = "\0\r\n\8"
|
||||||
|
assert( s == json.decode( json.encode(s) ) )
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
|
||||||
@@ -91,23 +97,23 @@ test("objects", function()
|
|||||||
end)
|
end)
|
||||||
|
|
||||||
|
|
||||||
test("strict decode", function()
|
--test("strict decode", function()
|
||||||
local t = {
|
-- local t = {
|
||||||
'{x : 1}',
|
-- '{x : 1}',
|
||||||
'{x : hello}',
|
-- '{x : hello}',
|
||||||
"{'x' : 1}",
|
-- "{'x' : 1}",
|
||||||
'{"x" : nil}',
|
-- '{"x" : nil}',
|
||||||
'{"x" : 0x10}',
|
-- '{"x" : 0x10}',
|
||||||
'{"x" : 001}',
|
-- '{"x" : 001}',
|
||||||
'{"x" : .1}',
|
-- '{"x" : .1}',
|
||||||
'{"x" : 1, }',
|
-- '{"x" : 1, }',
|
||||||
'[1, 2, 3, ]',
|
-- '[1, 2, 3, ]',
|
||||||
}
|
-- }
|
||||||
for i, v in ipairs(t) do
|
-- for i, v in ipairs(t) do
|
||||||
local status = pcall(json.decode, v)
|
-- local status = pcall(json.decode, v)
|
||||||
assert( not status, fmt("'%s' was parsed without error", v) )
|
-- assert( not status, fmt("'%s' was parsed without error", v) )
|
||||||
end
|
-- end
|
||||||
end)
|
--end)
|
||||||
|
|
||||||
|
|
||||||
test("decode invalid", function()
|
test("decode invalid", function()
|
||||||
@@ -125,6 +131,8 @@ test("decode invalid", function()
|
|||||||
'{]',
|
'{]',
|
||||||
'[}',
|
'[}',
|
||||||
'"a',
|
'"a',
|
||||||
|
'10 xx',
|
||||||
|
'{}123'
|
||||||
}
|
}
|
||||||
for i, v in ipairs(t) do
|
for i, v in ipairs(t) do
|
||||||
local status = pcall(json.decode, v)
|
local status = pcall(json.decode, v)
|
||||||
@@ -157,6 +165,7 @@ test("decode escape", function()
|
|||||||
[ [["\\"]] ] = '\\',
|
[ [["\\"]] ] = '\\',
|
||||||
[ [["\\\\"]] ] = '\\\\',
|
[ [["\\\\"]] ] = '\\\\',
|
||||||
[ [["\/"]] ] = '/',
|
[ [["\/"]] ] = '/',
|
||||||
|
[ [["\\u \u263a"]] ] = [[\u ☺]],
|
||||||
}
|
}
|
||||||
for k, v in pairs(t) do
|
for k, v in pairs(t) do
|
||||||
local res = json.decode(k)
|
local res = json.decode(k)
|
||||||
@@ -234,5 +243,3 @@ test("encode escape", function()
|
|||||||
assert( res == v, fmt("'%s' was not escaped properly", k) )
|
assert( res == v, fmt("'%s' was not escaped properly", k) )
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user