forked from arcapos/luajson
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.lua
46 lines (37 loc) · 933 Bytes
/
test.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
-- import the json module
local json = require('json')
local foo = json.decode([[
{"name" : "lol", "age" : -1.5e+06, "foo" : ["bar", true, null]}
]])
foo.x = 'x'
-- foo[1] = 'hossa'
-- foo[2] = 'rossa'
print(foo.age) -- -1500000
print(foo.name) -- lol
print(foo.foo[1]) -- bar
print(foo.foo[2]) -- true
print(foo.foo[3]) -- null
print(foo.foo[3] == json.null) -- true
foo.foox = "omg :D"
foo.theNull = json.null
foo.itIs = true
foo.itIsNot = false
foo.isNil = nil
foo.a = 'a'
foo.subtable = {
a = 'one',
b = 'two'
}
local a = {
test = 'hello',
test2 = 'world'
}
a.myself = a
local str = json.encode(foo)
print(str)
local foo2 = json.decode(str)
print(json.encode(foo2)) -- {"name":"lol",age:-1500000,"foo":"omg :D"}
print(json.encode(nil))
assert(pcall(json.encode, function () print('foo') end) == false)
assert(pcall(json.encode) == false)
assert(pcall(json.encode, {[false]=1}) == false)