forked from tarantool/tarantool-dissector
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjson.lua
135 lines (124 loc) · 3.67 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
---
-- Library methods for handling JSON data. It handles JSON encoding and
-- decoding according to RFC 4627.
--
-- There is a straightforward mapping between JSON and Lua data types. One
-- exception is JSON <code>NULL</code>, which is not the same as Lua
-- <code>nil</code>. (A better match for Lua <code>nil</code> is JavaScript
-- <code>undefined</code>.) <code>NULL</code> values in JSON are represented by
-- the special value <code>json.NULL</code>.
--
-- @author Martin Holst Swende
-- @author David Fifield
-- @author Patrick Donnelly
-- @copyright Same as Nmap--See https://nmap.org/book/man-legal.html
-- Version 0.4
-- Created 01/25/2010 - v0.1 - created by Martin Holst Swende <[email protected]>
-- Heavily modified 02/22/2010 - v0.3. Rewrote the parser into an OO-form, to not have to handle
-- all kinds of state with parameters and return values.
-- Modified 02/27/2010 - v0.4 Added unicode handling (written by David Fifield). Renamed toJson
-- and fromJson into generate() and parse(), implemented more proper numeric parsing and added some more error checking.
local json = {}
-- See section 2.5 for escapes.
-- For convenience, ESCAPE_TABLE maps to escape sequences complete with
-- backslash, and REVERSE_ESCAPE_TABLE maps from single escape characters
-- (no backslash).
local ESCAPE_TABLE = {}
--do
local escapes = {
["\x22"] = "\"",
["\x5C"] = "\\",
["\x2F"] = "/",
["\x08"] = "b",
["\x0C"] = "f",
["\x0A"] = "n",
["\x0D"] = "r",
["\x09"] = "t",
}
for k, v in pairs(escapes) do
ESCAPE_TABLE[k] = "\\" .. v
end
--end
---Escapes a string
---@param str string
---@return string where the special chars have been escaped
local function escape(str)
return "\"" .. string.gsub(str, ".", ESCAPE_TABLE) .. "\""
end
local function is_array(t)
local i = 0
for _ in pairs(t) do
i = i + 1
if t[i] == nil then return false end
end
return true
end
--- Checks what JSON type a variable will be treated as when generating JSON
---@param var any a variable to inspect
---@return string containing the JSON type. Valid values are "array", "object", "number", "string", "boolean", and "null"
local function typeof(var)
local t = type(var)
if var == NULL then
return "null"
elseif t == "table" then
--local mtval = rawget(getmetatable(var) or {}, "json")
--if mtval == "array" or (mtval ~= "object" and #var > 0) then
if is_array(var) then
return "array"
else
return "object"
end
else
return t
end
error("Unknown data type in typeof")
end
function dump(o)
if type(o) == 'table' then
local s = '{ '
for k,v in pairs(o) do
if type(k) ~= 'number' then k = '"'..k..'"' end
s = s .. '['..k..'] = ' .. dump(v) .. ','
end
return s .. '} '
else
return tostring(o)
end
end
---Creates json data from an object
---@param obj table containing data
---@return string containing valid json
function json.encode(obj)
-- NULL-check must be performed before
-- checking type == table, since the NULL-object
-- is a table
if obj == NULL then
return "null"
elseif obj == false then
return "false"
elseif obj == true then
return "true"
elseif type(obj) == "number" then
return tostring(obj)
elseif type(obj) == "string" then
return escape(obj)
elseif type(obj) == "table" then
local elems, jtype
elems = {}
jtype = typeof(obj)
if jtype == "array" then
for _, v in ipairs(obj) do
elems[#elems + 1] = json.encode(v)
end
return "[" .. table.concat(elems, ", ") .. "]"
elseif jtype == "object" then
for k, v in pairs(obj) do
elems[#elems + 1] = escape(k) .. ": " .. json.encode(v)
end
return "{" .. table.concat(elems, ", ") .. "}"
end
end
error("Unknown data type in generate")
end
--print(generate({paf='molodets'}))
return json