-
Notifications
You must be signed in to change notification settings - Fork 5
/
test_json.lua
32 lines (28 loc) · 926 Bytes
/
test_json.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
local json = require( "acid.json" )
local cjson = require( "cjson" )
function test.enc(t)
local cases = {
{nil , nil, 'null'},
{{}, nil, '{}'},
{{}, {is_array=true}, '[]'},
{{1, "2"}, nil, cjson.encode({1, "2"})},
{{a=1, b='2', c={c1=1}}, nil, cjson.encode({a=1, b='2', c={c1=1}})},
}
for _, case in ipairs(cases) do
local j, opt, exp = unpack(case)
t:eq(json.enc(j, opt), exp)
end
end
function test.dec(t)
local cases = {
{nil, nil, nil},
{'{"a":1, "b":2}', nil, {a=1, b=2}},
{'{"a":1, "b":2, "c":null}', nil, {a=1, b=2}},
{'{"a":1, "b":2, "c":null}', {use_nil=false}, {a=1, b=2, c=cjson.null}},
{'3', {use_nil=true}, 3},
}
for _, case in ipairs(cases) do
local j, opt, exp = unpack(case)
t:eqdict(json.dec(j, opt), exp)
end
end