Software /
code /
prosody
Annotate
util/json.lua @ 4989:573123ff2ab0
mod_admin_telnet: Always handle commands terminated by line feeds - ensures consistency even when packets are joined or split on the network
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Mon, 23 Jul 2012 14:03:00 +0100 |
parent | 4474:b08a46cf06e6 |
child | 5395:ec33d72a08b6 |
rev | line source |
---|---|
3979 | 1 |
2 local type = type; | |
4474
b08a46cf06e6
util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents:
4404
diff
changeset
|
3 local t_insert, t_concat, t_remove, t_sort = table.insert, table.concat, table.remove, table.sort; |
3979 | 4 local s_char = string.char; |
5 local tostring, tonumber = tostring, tonumber; | |
6 local pairs, ipairs = pairs, ipairs; | |
7 local next = next; | |
8 local error = error; | |
9 local newproxy, getmetatable = newproxy, getmetatable; | |
10 local print = print; | |
11 | |
12 --module("json") | |
13 local json = {}; | |
14 | |
15 local null = newproxy and newproxy(true) or {}; | |
16 if getmetatable and getmetatable(null) then | |
17 getmetatable(null).__tostring = function() return "null"; end; | |
18 end | |
19 json.null = null; | |
20 | |
21 local escapes = { | |
22 ["\""] = "\\\"", ["\\"] = "\\\\", ["\b"] = "\\b", | |
23 ["\f"] = "\\f", ["\n"] = "\\n", ["\r"] = "\\r", ["\t"] = "\\t"}; | |
24 local unescapes = { | |
25 ["\""] = "\"", ["\\"] = "\\", ["/"] = "/", | |
26 b = "\b", f = "\f", n = "\n", r = "\r", t = "\t"}; | |
27 for i=0,31 do | |
28 local ch = s_char(i); | |
29 if not escapes[ch] then escapes[ch] = ("\\u%.4X"):format(i); end | |
30 end | |
31 | |
32 local valid_types = { | |
33 number = true, | |
34 string = true, | |
35 table = true, | |
36 boolean = true | |
37 }; | |
38 local special_keys = { | |
39 __array = true; | |
40 __hash = true; | |
41 }; | |
42 | |
43 local simplesave, tablesave, arraysave, stringsave; | |
44 | |
45 function stringsave(o, buffer) | |
46 -- FIXME do proper utf-8 and binary data detection | |
47 t_insert(buffer, "\""..(o:gsub(".", escapes)).."\""); | |
48 end | |
49 | |
50 function arraysave(o, buffer) | |
51 t_insert(buffer, "["); | |
52 if next(o) then | |
53 for i,v in ipairs(o) do | |
54 simplesave(v, buffer); | |
55 t_insert(buffer, ","); | |
56 end | |
57 t_remove(buffer); | |
58 end | |
59 t_insert(buffer, "]"); | |
60 end | |
61 | |
62 function tablesave(o, buffer) | |
63 local __array = {}; | |
64 local __hash = {}; | |
65 local hash = {}; | |
66 for i,v in ipairs(o) do | |
67 __array[i] = v; | |
68 end | |
69 for k,v in pairs(o) do | |
70 local ktype, vtype = type(k), type(v); | |
71 if valid_types[vtype] or v == null then | |
72 if ktype == "string" and not special_keys[k] then | |
73 hash[k] = v; | |
74 elseif (valid_types[ktype] or k == null) and __array[k] == nil then | |
75 __hash[k] = v; | |
76 end | |
77 end | |
78 end | |
79 if next(__hash) ~= nil or next(hash) ~= nil or next(__array) == nil then | |
80 t_insert(buffer, "{"); | |
81 local mark = #buffer; | |
4474
b08a46cf06e6
util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents:
4404
diff
changeset
|
82 if buffer.ordered then |
b08a46cf06e6
util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents:
4404
diff
changeset
|
83 local keys = {}; |
b08a46cf06e6
util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents:
4404
diff
changeset
|
84 for k in pairs(hash) do |
b08a46cf06e6
util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents:
4404
diff
changeset
|
85 t_insert(keys, k); |
b08a46cf06e6
util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents:
4404
diff
changeset
|
86 end |
b08a46cf06e6
util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents:
4404
diff
changeset
|
87 t_sort(keys); |
b08a46cf06e6
util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents:
4404
diff
changeset
|
88 for _,k in ipairs(keys) do |
b08a46cf06e6
util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents:
4404
diff
changeset
|
89 stringsave(k, buffer); |
b08a46cf06e6
util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents:
4404
diff
changeset
|
90 t_insert(buffer, ":"); |
b08a46cf06e6
util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents:
4404
diff
changeset
|
91 simplesave(hash[k], buffer); |
b08a46cf06e6
util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents:
4404
diff
changeset
|
92 t_insert(buffer, ","); |
b08a46cf06e6
util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents:
4404
diff
changeset
|
93 end |
b08a46cf06e6
util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents:
4404
diff
changeset
|
94 else |
b08a46cf06e6
util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents:
4404
diff
changeset
|
95 for k,v in pairs(hash) do |
b08a46cf06e6
util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents:
4404
diff
changeset
|
96 stringsave(k, buffer); |
b08a46cf06e6
util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents:
4404
diff
changeset
|
97 t_insert(buffer, ":"); |
b08a46cf06e6
util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents:
4404
diff
changeset
|
98 simplesave(v, buffer); |
b08a46cf06e6
util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents:
4404
diff
changeset
|
99 t_insert(buffer, ","); |
b08a46cf06e6
util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents:
4404
diff
changeset
|
100 end |
3979 | 101 end |
102 if next(__hash) ~= nil then | |
103 t_insert(buffer, "\"__hash\":["); | |
104 for k,v in pairs(__hash) do | |
105 simplesave(k, buffer); | |
106 t_insert(buffer, ","); | |
107 simplesave(v, buffer); | |
108 t_insert(buffer, ","); | |
109 end | |
110 t_remove(buffer); | |
111 t_insert(buffer, "]"); | |
112 t_insert(buffer, ","); | |
113 end | |
114 if next(__array) then | |
115 t_insert(buffer, "\"__array\":"); | |
116 arraysave(__array, buffer); | |
117 t_insert(buffer, ","); | |
118 end | |
119 if mark ~= #buffer then t_remove(buffer); end | |
120 t_insert(buffer, "}"); | |
121 else | |
122 arraysave(__array, buffer); | |
123 end | |
124 end | |
125 | |
126 function simplesave(o, buffer) | |
127 local t = type(o); | |
128 if t == "number" then | |
129 t_insert(buffer, tostring(o)); | |
130 elseif t == "string" then | |
131 stringsave(o, buffer); | |
132 elseif t == "table" then | |
133 tablesave(o, buffer); | |
134 elseif t == "boolean" then | |
135 t_insert(buffer, (o and "true" or "false")); | |
136 else | |
137 t_insert(buffer, "null"); | |
138 end | |
139 end | |
140 | |
141 function json.encode(obj) | |
142 local t = {}; | |
143 simplesave(obj, t); | |
144 return t_concat(t); | |
145 end | |
4474
b08a46cf06e6
util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents:
4404
diff
changeset
|
146 function json.encode_ordered(obj) |
b08a46cf06e6
util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents:
4404
diff
changeset
|
147 local t = { ordered = true }; |
b08a46cf06e6
util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents:
4404
diff
changeset
|
148 simplesave(obj, t); |
b08a46cf06e6
util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents:
4404
diff
changeset
|
149 return t_concat(t); |
b08a46cf06e6
util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents:
4404
diff
changeset
|
150 end |
3979 | 151 |
152 ----------------------------------- | |
153 | |
154 | |
155 function json.decode(json) | |
4284
20979f124ad9
util.json: Fixed handling of truncated JSON.
Waqas Hussain <waqas20@gmail.com>
parents:
4147
diff
changeset
|
156 json = json.." "; -- appending a space ensures valid json wouldn't touch EOF |
3979 | 157 local pos = 1; |
158 local current = {}; | |
159 local stack = {}; | |
160 local ch, peek; | |
161 local function next() | |
162 ch = json:sub(pos, pos); | |
4284
20979f124ad9
util.json: Fixed handling of truncated JSON.
Waqas Hussain <waqas20@gmail.com>
parents:
4147
diff
changeset
|
163 if ch == "" then error("Unexpected EOF"); end |
3979 | 164 pos = pos+1; |
165 peek = json:sub(pos, pos); | |
166 return ch; | |
167 end | |
168 | |
169 local function skipwhitespace() | |
170 while ch and (ch == "\r" or ch == "\n" or ch == "\t" or ch == " ") do | |
171 next(); | |
172 end | |
173 end | |
174 local function skiplinecomment() | |
175 repeat next(); until not(ch) or ch == "\r" or ch == "\n"; | |
176 skipwhitespace(); | |
177 end | |
178 local function skipstarcomment() | |
179 next(); next(); -- skip '/', '*' | |
180 while peek and ch ~= "*" and peek ~= "/" do next(); end | |
181 if not peek then error("eof in star comment") end | |
182 next(); next(); -- skip '*', '/' | |
183 skipwhitespace(); | |
184 end | |
185 local function skipstuff() | |
186 while true do | |
187 skipwhitespace(); | |
188 if ch == "/" and peek == "*" then | |
189 skipstarcomment(); | |
4404
5356664ef9d4
util.json: Fix for single-line comments (thanks Norbert Kiesel)
Matthew Wild <mwild1@gmail.com>
parents:
4284
diff
changeset
|
190 elseif ch == "/" and peek == "/" then |
3979 | 191 skiplinecomment(); |
192 else | |
193 return; | |
194 end | |
195 end | |
196 end | |
197 | |
198 local readvalue; | |
199 local function readarray() | |
200 local t = {}; | |
201 next(); -- skip '[' | |
202 skipstuff(); | |
203 if ch == "]" then next(); return t; end | |
204 t_insert(t, readvalue()); | |
205 while true do | |
206 skipstuff(); | |
207 if ch == "]" then next(); return t; end | |
208 if not ch then error("eof while reading array"); | |
209 elseif ch == "," then next(); | |
210 elseif ch then error("unexpected character in array, comma expected"); end | |
211 if not ch then error("eof while reading array"); end | |
212 t_insert(t, readvalue()); | |
213 end | |
214 end | |
215 | |
216 local function checkandskip(c) | |
217 local x = ch or "eof"; | |
218 if x ~= c then error("unexpected "..x..", '"..c.."' expected"); end | |
219 next(); | |
220 end | |
221 local function readliteral(lit, val) | |
222 for c in lit:gmatch(".") do | |
223 checkandskip(c); | |
224 end | |
225 return val; | |
226 end | |
227 local function readstring() | |
228 local s = ""; | |
229 checkandskip("\""); | |
230 while ch do | |
231 while ch and ch ~= "\\" and ch ~= "\"" do | |
232 s = s..ch; next(); | |
233 end | |
234 if ch == "\\" then | |
235 next(); | |
236 if unescapes[ch] then | |
237 s = s..unescapes[ch]; | |
238 next(); | |
239 elseif ch == "u" then | |
240 local seq = ""; | |
241 for i=1,4 do | |
242 next(); | |
243 if not ch then error("unexpected eof in string"); end | |
244 if not ch:match("[0-9a-fA-F]") then error("invalid unicode escape sequence in string"); end | |
245 seq = seq..ch; | |
246 end | |
247 s = s..s.char(tonumber(seq, 16)); -- FIXME do proper utf-8 | |
248 next(); | |
249 else error("invalid escape sequence in string"); end | |
250 end | |
251 if ch == "\"" then | |
252 next(); | |
253 return s; | |
254 end | |
255 end | |
256 error("eof while reading string"); | |
257 end | |
258 local function readnumber() | |
259 local s = ""; | |
260 if ch == "-" then | |
261 s = s..ch; next(); | |
262 if not ch:match("[0-9]") then error("number format error"); end | |
263 end | |
264 if ch == "0" then | |
265 s = s..ch; next(); | |
266 if ch:match("[0-9]") then error("number format error"); end | |
267 else | |
268 while ch and ch:match("[0-9]") do | |
269 s = s..ch; next(); | |
270 end | |
271 end | |
272 if ch == "." then | |
273 s = s..ch; next(); | |
274 if not ch:match("[0-9]") then error("number format error"); end | |
275 while ch and ch:match("[0-9]") do | |
276 s = s..ch; next(); | |
277 end | |
278 if ch == "e" or ch == "E" then | |
279 s = s..ch; next(); | |
280 if ch == "+" or ch == "-" then | |
281 s = s..ch; next(); | |
282 if not ch:match("[0-9]") then error("number format error"); end | |
283 while ch and ch:match("[0-9]") do | |
284 s = s..ch; next(); | |
285 end | |
286 end | |
287 end | |
288 end | |
289 return tonumber(s); | |
290 end | |
291 local function readmember(t) | |
4147
7f119ebcf55f
util.json: Fixed a parse error caused by unexpected whitespace.
Waqas Hussain <waqas20@gmail.com>
parents:
3979
diff
changeset
|
292 skipstuff(); |
3979 | 293 local k = readstring(); |
4147
7f119ebcf55f
util.json: Fixed a parse error caused by unexpected whitespace.
Waqas Hussain <waqas20@gmail.com>
parents:
3979
diff
changeset
|
294 skipstuff(); |
3979 | 295 checkandskip(":"); |
296 t[k] = readvalue(); | |
297 end | |
298 local function fixobject(obj) | |
299 local __array = obj.__array; | |
300 if __array then | |
301 obj.__array = nil; | |
302 for i,v in ipairs(__array) do | |
303 t_insert(obj, v); | |
304 end | |
305 end | |
306 local __hash = obj.__hash; | |
307 if __hash then | |
308 obj.__hash = nil; | |
309 local k; | |
310 for i,v in ipairs(__hash) do | |
311 if k ~= nil then | |
312 obj[k] = v; k = nil; | |
313 else | |
314 k = v; | |
315 end | |
316 end | |
317 end | |
318 return obj; | |
319 end | |
320 local function readobject() | |
321 local t = {}; | |
322 next(); -- skip '{' | |
323 skipstuff(); | |
324 if ch == "}" then next(); return t; end | |
325 if not ch then error("eof while reading object"); end | |
326 readmember(t); | |
327 while true do | |
328 skipstuff(); | |
329 if ch == "}" then next(); return fixobject(t); end | |
330 if not ch then error("eof while reading object"); | |
331 elseif ch == "," then next(); | |
332 elseif ch then error("unexpected character in object, comma expected"); end | |
333 if not ch then error("eof while reading object"); end | |
334 readmember(t); | |
335 end | |
336 end | |
337 | |
338 function readvalue() | |
339 skipstuff(); | |
340 while ch do | |
341 if ch == "{" then | |
342 return readobject(); | |
343 elseif ch == "[" then | |
344 return readarray(); | |
345 elseif ch == "\"" then | |
346 return readstring(); | |
347 elseif ch:match("[%-0-9%.]") then | |
348 return readnumber(); | |
349 elseif ch == "n" then | |
350 return readliteral("null", null); | |
351 elseif ch == "t" then | |
352 return readliteral("true", true); | |
353 elseif ch == "f" then | |
354 return readliteral("false", false); | |
355 else | |
356 error("invalid character at value start: "..ch); | |
357 end | |
358 end | |
359 error("eof while reading value"); | |
360 end | |
361 next(); | |
362 return readvalue(); | |
363 end | |
364 | |
365 function json.test(object) | |
366 local encoded = json.encode(object); | |
367 local decoded = json.decode(encoded); | |
368 local recoded = json.encode(decoded); | |
369 if encoded ~= recoded then | |
370 print("FAILED"); | |
371 print("encoded:", encoded); | |
372 print("recoded:", recoded); | |
373 else | |
374 print(encoded); | |
375 end | |
376 return encoded == recoded; | |
377 end | |
378 | |
379 return json; |