File: json.lua

package info (click to toggle)
ntopng 5.2.1%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 121,832 kB
  • sloc: javascript: 143,431; cpp: 71,175; ansic: 11,108; sh: 4,687; makefile: 911; python: 587; sql: 512; pascal: 234; perl: 118; ruby: 52; exp: 4
file content (187 lines) | stat: -rw-r--r-- 4,552 bytes parent folder | download
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
--
-- (C) 2013-22 - ntop.org
--

--- Simple class for JSON parsing

dirs = ntop.getDirs()
package.path = dirs.installdir .. "/scripts/lua/modules/?.lua;" .. package.path

local parseElement

local function skipWhitespaces(content, pos)
  local _,nend = content:find("^[ \n\r\t]+", pos)
  if (nend ~= nil) then
     return nend+1
  else
     return pos
  end
end

local function parseNumber(content, pos)
  -- Integer part
  local integer_part = content:match('^-?[1-9]%d*', pos)
                       or content:match("^-?0", pos)

  if not integer_part then
    return nil
  end

  -- Decimal part
  local lstart = pos + integer_part:len()
  local decimal_part = content:match('^%.%d+', i) or ""
  lstart = lstart + decimal_part:len()

  -- Exponential
  local exponent_part = content:match('^[eE][-+]?%d+', i) or ""
  lstart = lstart + exponent_part:len()
  local full_number_text = integer_part .. decimal_part .. exponent_part
  local cnumber = tonumber(full_number_text)

  if not cnumber then
    return nil
  end

  return cnumber, lstart
end

local function parseString(content, pos)
  if (content:sub(pos, pos) ~= '"') then
    return ""
  end
  local i = pos + 1
  local clen = content:len()
  local val = ""

  while (i <= clen) do
    local c = content:sub(i, i)
    if (c == '"') then
      return val, i+1
    end
    val = val..c
    i = i + 1
  end

  return ""
end

local function parseObject(content, pos)
  if (content:sub(pos, pos) ~= '{') then
    return {}, pos
  end
  local lstart = skipWhitespaces(content, pos+1)
  local val = {}

  -- Handle empty array
  if (content:sub(lstart, lstart) == '}') then
    return val, lstart+1
  end

  local clen = content:len()
  while (lstart <= clen) do
    local key, nstart = parseString(content, lstart)
    lstart = skipWhitespaces(content, nstart)
    if (content == nil or lstart == nil or
        content:sub(lstart, lstart) ~= ':') then
      return {}, lstart
    end
    lstart = skipWhitespaces(content, lstart+1)
    local nval, nstart = parseElement(content, lstart)
    val[key] = nval
    -- We have key and value, must have a } or a ,
    lstart = skipWhitespaces(content, nstart)
    if (content == nil or lstart == nil) then
      return {}, clen
    end
    if (content:sub(lstart, lstart) == '}') then
      return val, lstart+1
    end
    if (content:sub(lstart, lstart) ~= ',') then
      return {}, lstart
    end
    lstart = skipWhitespaces(content, lstart+1) -- skip comma
  end

  return {}, lstart
end

local function parseArray(content, pos)
  if (content:sub(pos, pos) ~= '[') then
    return {}, pos
  end

  local lstart = skipWhitespaces(content, pos+1)
  local val = {}
  -- Handle empty array
  if (content:sub(lstart, lstart) == ']') then
    return val, lstart+1
  end
  local idx = 1 -- keep 1 to n convention
  local clen = content:len()

  while (lstart <= clen) do
    local nval, nstart = parseElement(content, lstart)
    val[idx] = nval
    idx = idx + 1
    lstart = skipWhitespaces(content, nstart)
    -- Need a ] or a , now
    if (content == nil or lstart == nil) then
      return {}, clen
    end
    if (content:sub(lstart, lstart) == ']') then
      return val, lstart+1
    end
    if (content:sub(lstart, lstart) ~= ',') then
      return {}, lstart
    end
    lstart = skipWhitespaces(content, lstart+1)
  end
  return {}, lstart
end

parseElement = function(content, pos)
  pos = skipWhitespaces(content, pos)

  if (pos > content:len()) then
    return {}, pos
  end

  if (content:find('^"', pos)) then
    return parseString(content, pos)
  elseif (content:find('^[-0123456789 ]', pos)) then
    return parseNumber(content, pos)
  elseif (content:find('^%{', pos)) then
    return parseObject(content, pos)
  elseif (content:find('^%[', pos)) then
    return parseArray(content, pos)
  else
    return {}, pos
  end
end

-- Exposed methods:
-- printTable: prints an indented version of a JSON parsing table
-- parseJSON: creates a table from a JSON

function printTable(tbl, indent)
  if (tbl == nil) then return end
  if not indent then indent = 0 end
  for k, v in pairs(tbl) do
    formatting = string.rep("  ", indent) .. k .. ": "
    if type(v) == "table" then
      io.write(formatting)
      printTable(v, indent+1)
    elseif type(v) == 'boolean' then
      io.write(formatting .. tostring(v).."\n")
    else
      io.write(formatting .. v.."\n")
    end
  end
end

function parseJSON(content)
  if (content == nil) then return {} end
  local table = parseElement(content, 1)
  --printTable(table)
  return table
end