Merge 0.9->trunk
[prosody.git] / util / json.lua
1
2 local type = type;
3 local t_insert, t_concat, t_remove, t_sort = table.insert, table.concat, table.remove, table.sort;
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;
82                 if buffer.ordered then
83                         local keys = {};
84                         for k in pairs(hash) do
85                                 t_insert(keys, k);
86                         end
87                         t_sort(keys);
88                         for _,k in ipairs(keys) do
89                                 stringsave(k, buffer);
90                                 t_insert(buffer, ":");
91                                 simplesave(hash[k], buffer);
92                                 t_insert(buffer, ",");
93                         end
94                 else
95                         for k,v in pairs(hash) do
96                                 stringsave(k, buffer);
97                                 t_insert(buffer, ":");
98                                 simplesave(v, buffer);
99                                 t_insert(buffer, ",");
100                         end
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
146 function json.encode_ordered(obj)
147         local t = { ordered = true };
148         simplesave(obj, t);
149         return t_concat(t);
150 end
151 function json.encode_array(obj)
152         local t = {};
153         arraysave(obj, t);
154         return t_concat(t);
155 end
156
157 -----------------------------------
158
159
160 function json.decode(json)
161         json = json.." "; -- appending a space ensures valid json wouldn't touch EOF
162         local pos = 1;
163         local current = {};
164         local stack = {};
165         local ch, peek;
166         local function next()
167                 ch = json:sub(pos, pos);
168                 if ch == "" then error("Unexpected EOF"); end
169                 pos = pos+1;
170                 peek = json:sub(pos, pos);
171                 return ch;
172         end
173         
174         local function skipwhitespace()
175                 while ch and (ch == "\r" or ch == "\n" or ch == "\t" or ch == " ") do
176                         next();
177                 end
178         end
179         local function skiplinecomment()
180                 repeat next(); until not(ch) or ch == "\r" or ch == "\n";
181                 skipwhitespace();
182         end
183         local function skipstarcomment()
184                 next(); next(); -- skip '/', '*'
185                 while peek and ch ~= "*" and peek ~= "/" do next(); end
186                 if not peek then error("eof in star comment") end
187                 next(); next(); -- skip '*', '/'
188                 skipwhitespace();
189         end
190         local function skipstuff()
191                 while true do
192                         skipwhitespace();
193                         if ch == "/" and peek == "*" then
194                                 skipstarcomment();
195                         elseif ch == "/" and peek == "/" then
196                                 skiplinecomment();
197                         else
198                                 return;
199                         end
200                 end
201         end
202         
203         local readvalue;
204         local function readarray()
205                 local t = {};
206                 next(); -- skip '['
207                 skipstuff();
208                 if ch == "]" then next(); return t; end
209                 t_insert(t, readvalue());
210                 while true do
211                         skipstuff();
212                         if ch == "]" then next(); return t; end
213                         if not ch then error("eof while reading array");
214                         elseif ch == "," then next();
215                         elseif ch then error("unexpected character in array, comma expected"); end
216                         if not ch then error("eof while reading array"); end
217                         t_insert(t, readvalue());
218                 end
219         end
220         
221         local function checkandskip(c)
222                 local x = ch or "eof";
223                 if x ~= c then error("unexpected "..x..", '"..c.."' expected"); end
224                 next();
225         end
226         local function readliteral(lit, val)
227                 for c in lit:gmatch(".") do
228                         checkandskip(c);
229                 end
230                 return val;
231         end
232         local function readstring()
233                 local s = "";
234                 checkandskip("\"");
235                 while ch do
236                         while ch and ch ~= "\\" and ch ~= "\"" do
237                                 s = s..ch; next();
238                         end
239                         if ch == "\\" then
240                                 next();
241                                 if unescapes[ch] then
242                                         s = s..unescapes[ch];
243                                         next();
244                                 elseif ch == "u" then
245                                         local seq = "";
246                                         for i=1,4 do
247                                                 next();
248                                                 if not ch then error("unexpected eof in string"); end
249                                                 if not ch:match("[0-9a-fA-F]") then error("invalid unicode escape sequence in string"); end
250                                                 seq = seq..ch;
251                                         end
252                                         s = s..s.char(tonumber(seq, 16)); -- FIXME do proper utf-8
253                                         next();
254                                 else error("invalid escape sequence in string"); end
255                         end
256                         if ch == "\"" then
257                                 next();
258                                 return s;
259                         end
260                 end
261                 error("eof while reading string");
262         end
263         local function readnumber()
264                 local s = "";
265                 if ch == "-" then
266                         s = s..ch; next();
267                         if not ch:match("[0-9]") then error("number format error"); end
268                 end
269                 if ch == "0" then
270                         s = s..ch; next();
271                         if ch:match("[0-9]") then error("number format error"); end
272                 else
273                         while ch and ch:match("[0-9]") do
274                                 s = s..ch; next();
275                         end
276                 end
277                 if ch == "." then
278                         s = s..ch; next();
279                         if not ch:match("[0-9]") then error("number format error"); end
280                         while ch and ch:match("[0-9]") do
281                                 s = s..ch; next();
282                         end
283                         if ch == "e" or ch == "E" then
284                                 s = s..ch; next();
285                                 if ch == "+" or ch == "-" then
286                                         s = s..ch; next();
287                                         if not ch:match("[0-9]") then error("number format error"); end
288                                         while ch and ch:match("[0-9]") do
289                                                 s = s..ch; next();
290                                         end
291                                 end
292                         end
293                 end
294                 return tonumber(s);
295         end
296         local function readmember(t)
297                 skipstuff();
298                 local k = readstring();
299                 skipstuff();
300                 checkandskip(":");
301                 t[k] = readvalue();
302         end
303         local function fixobject(obj)
304                 local __array = obj.__array;
305                 if __array then
306                         obj.__array = nil;
307                         for i,v in ipairs(__array) do
308                                 t_insert(obj, v);
309                         end
310                 end
311                 local __hash = obj.__hash;
312                 if __hash then
313                         obj.__hash = nil;
314                         local k;
315                         for i,v in ipairs(__hash) do
316                                 if k ~= nil then
317                                         obj[k] = v; k = nil;
318                                 else
319                                         k = v;
320                                 end
321                         end
322                 end
323                 return obj;
324         end
325         local function readobject()
326                 local t = {};
327                 next(); -- skip '{'
328                 skipstuff();
329                 if ch == "}" then next(); return t; end
330                 if not ch then error("eof while reading object"); end
331                 readmember(t);
332                 while true do
333                         skipstuff();
334                         if ch == "}" then next(); return fixobject(t); end
335                         if not ch then error("eof while reading object");
336                         elseif ch == "," then next();
337                         elseif ch then error("unexpected character in object, comma expected"); end
338                         if not ch then error("eof while reading object"); end
339                         readmember(t);
340                 end
341         end
342         
343         function readvalue()
344                 skipstuff();
345                 while ch do
346                         if ch == "{" then
347                                 return readobject();
348                         elseif ch == "[" then
349                                 return readarray();
350                         elseif ch == "\"" then
351                                 return readstring();
352                         elseif ch:match("[%-0-9%.]") then
353                                 return readnumber();
354                         elseif ch == "n" then
355                                 return readliteral("null", null);
356                         elseif ch == "t" then
357                                 return readliteral("true", true);
358                         elseif ch == "f" then
359                                 return readliteral("false", false);
360                         else
361                                 error("invalid character at value start: "..ch);
362                         end
363                 end
364                 error("eof while reading value");
365         end
366         next();
367         return readvalue();
368 end
369
370 function json.test(object)
371         local encoded = json.encode(object);
372         local decoded = json.decode(encoded);
373         local recoded = json.encode(decoded);
374         if encoded ~= recoded then
375                 print("FAILED");
376                 print("encoded:", encoded);
377                 print("recoded:", recoded);
378         else
379                 print(encoded);
380         end
381         return encoded == recoded;
382 end
383
384 return json;