forked from findstr/silly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.lua
57 lines (42 loc) · 1.12 KB
/
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
local json = {}
function json.decode(str)
local j = string.gsub(str, '\"([^\"]-)\":','%1=')
j = "local t =" .. j .."; return t"
t = load(j)()
return t;
end
local encode_tbl, encode_array
function encode_array(arr)
local sz = ""
for _, v in ipairs(arr) do
assert(type(v) == "table")
sz = sz .. encode_tbl(v) .. ','
end
return sz
end
function encode_tbl(tbl)
local l = tbl or {}
local sz
sz = '{'
for k, v in pairs(tbl) do
if (type(v) == "table") then
sz = sz .. k .. ':' .. '[' .. encode_array(v) .. '],'
else
sz = sz ..'"' .. k .. '"' .. ":" .. '"' .. v .. '"' .. ','
end
end
sz = sz .. '}'
return sz
end
function json.encode(tbl)
local sz = encode_tbl(tbl)
return (sz .. '\r\n\r')
end
------------test--------
--[[
local tbl = {room={{cmd="auth1", uid = "dafas"},
{cmd="auth2", sid = "123"}}}
local sz = json.encode(tbl)
print(sz)
]]--
return json