Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bee7ee3431 | ||
|
|
eb6e343c53 | ||
|
|
19cc024df6 | ||
|
|
e1dbe93f7c |
2
LICENSE
2
LICENSE
@@ -1,4 +1,4 @@
|
|||||||
Copyright (c) 2015 rxi
|
Copyright (c) 2018 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
|
||||||
|
|||||||
30
json.lua
30
json.lua
@@ -1,13 +1,28 @@
|
|||||||
--
|
--
|
||||||
-- json.lua
|
-- json.lua
|
||||||
--
|
--
|
||||||
-- Copyright (c) 2015 rxi
|
-- Copyright (c) 2018 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.1" }
|
||||||
|
|
||||||
-------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------
|
||||||
-- Encode
|
-- Encode
|
||||||
@@ -373,7 +388,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
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -91,23 +91,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 +125,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)
|
||||||
@@ -234,5 +236,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