Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d1e3b0f5d0 | ||
|
|
69b714ad2b | ||
|
|
f049daf06c | ||
|
|
fd58f29876 | ||
|
|
d3f417d4d4 | ||
|
|
e1abe1c45c | ||
|
|
bee7ee3431 | ||
|
|
eb6e343c53 | ||
|
|
19cc024df6 | ||
|
|
e1dbe93f7c |
2
LICENSE
2
LICENSE
@@ -1,4 +1,4 @@
|
||||
Copyright (c) 2015 rxi
|
||||
Copyright (c) 2019 rxi
|
||||
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
|
||||
48
json.lua
48
json.lua
@@ -1,13 +1,28 @@
|
||||
--
|
||||
-- json.lua
|
||||
--
|
||||
-- Copyright (c) 2015 rxi
|
||||
-- Copyright (c) 2019 rxi
|
||||
--
|
||||
-- This library is free software; you can redistribute it and/or modify it
|
||||
-- under the terms of the MIT license. See LICENSE for details.
|
||||
-- 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
|
||||
-- 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
|
||||
@@ -38,7 +53,7 @@ end
|
||||
|
||||
local function encode_nil(val)
|
||||
return "null"
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
local function encode_table(val, stack)
|
||||
@@ -50,7 +65,7 @@ local function encode_table(val, stack)
|
||||
|
||||
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
|
||||
local n = 0
|
||||
for k in pairs(val) do
|
||||
@@ -127,7 +142,7 @@ end
|
||||
|
||||
local parse
|
||||
|
||||
local function create_set(...)
|
||||
local function create_set(...)
|
||||
local res = {}
|
||||
for i = 1, select("#", ...) do
|
||||
res[ select(i, ...) ] = true
|
||||
@@ -234,17 +249,17 @@ local function parse_string(str, i)
|
||||
|
||||
elseif x == 34 then -- '"' (end of string)
|
||||
local s = str:sub(i + 1, j - 1)
|
||||
if has_surrogate_escape then
|
||||
if has_surrogate_escape then
|
||||
s = s:gsub("\\u[dD][89aAbB]..\\u....", parse_unicode_escape)
|
||||
end
|
||||
if has_unicode_escape then
|
||||
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
|
||||
@@ -282,7 +297,7 @@ local function parse_array(str, i)
|
||||
local x
|
||||
i = next_char(str, i, space_chars, true)
|
||||
-- Empty / end of array?
|
||||
if str:sub(i, i) == "]" then
|
||||
if str:sub(i, i) == "]" then
|
||||
i = i + 1
|
||||
break
|
||||
end
|
||||
@@ -290,7 +305,7 @@ local function parse_array(str, i)
|
||||
x, i = parse(str, i)
|
||||
res[n] = x
|
||||
n = n + 1
|
||||
-- Next token
|
||||
-- Next token
|
||||
i = next_char(str, i, space_chars, true)
|
||||
local chr = str:sub(i, i)
|
||||
i = i + 1
|
||||
@@ -308,7 +323,7 @@ local function parse_object(str, i)
|
||||
local key, val
|
||||
i = next_char(str, i, space_chars, true)
|
||||
-- Empty / end of object?
|
||||
if str:sub(i, i) == "}" then
|
||||
if str:sub(i, i) == "}" then
|
||||
i = i + 1
|
||||
break
|
||||
end
|
||||
@@ -373,7 +388,12 @@ function json.decode(str)
|
||||
if type(str) ~= "string" then
|
||||
error("expected argument of type string, got " .. type(str))
|
||||
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
|
||||
|
||||
|
||||
|
||||
@@ -57,7 +57,7 @@ end)
|
||||
|
||||
test("literals", function()
|
||||
assert( json.decode("true") == true )
|
||||
assert( json.encode(true) == "true" )
|
||||
assert( json.encode(true) == "true" )
|
||||
assert( json.decode("false") == false )
|
||||
assert( json.encode(false) == "false" )
|
||||
assert( json.decode("null") == nil )
|
||||
@@ -91,23 +91,23 @@ test("objects", function()
|
||||
end)
|
||||
|
||||
|
||||
test("strict decode", function()
|
||||
local t = {
|
||||
'{x : 1}',
|
||||
'{x : hello}',
|
||||
"{'x' : 1}",
|
||||
'{"x" : nil}',
|
||||
'{"x" : 0x10}',
|
||||
'{"x" : 001}',
|
||||
'{"x" : .1}',
|
||||
'{"x" : 1, }',
|
||||
'[1, 2, 3, ]',
|
||||
}
|
||||
for i, v in ipairs(t) do
|
||||
local status = pcall(json.decode, v)
|
||||
assert( not status, fmt("'%s' was parsed without error", v) )
|
||||
end
|
||||
end)
|
||||
--test("strict decode", function()
|
||||
-- local t = {
|
||||
-- '{x : 1}',
|
||||
-- '{x : hello}',
|
||||
-- "{'x' : 1}",
|
||||
-- '{"x" : nil}',
|
||||
-- '{"x" : 0x10}',
|
||||
-- '{"x" : 001}',
|
||||
-- '{"x" : .1}',
|
||||
-- '{"x" : 1, }',
|
||||
-- '[1, 2, 3, ]',
|
||||
-- }
|
||||
-- for i, v in ipairs(t) do
|
||||
-- local status = pcall(json.decode, v)
|
||||
-- assert( not status, fmt("'%s' was parsed without error", v) )
|
||||
-- end
|
||||
--end)
|
||||
|
||||
|
||||
test("decode invalid", function()
|
||||
@@ -125,6 +125,8 @@ test("decode invalid", function()
|
||||
'{]',
|
||||
'[}',
|
||||
'"a',
|
||||
'10 xx',
|
||||
'{}123'
|
||||
}
|
||||
for i, v in ipairs(t) do
|
||||
local status = pcall(json.decode, v)
|
||||
@@ -234,5 +236,3 @@ test("encode escape", function()
|
||||
assert( res == v, fmt("'%s' was not escaped properly", k) )
|
||||
end
|
||||
end)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user