2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
10 local t_insert, t_concat, t_remove, t_sort = table.insert, table.concat, table.remove, table.sort;
11 local s_char = string.char;
12 local tostring, tonumber = tostring, tonumber;
13 local pairs, ipairs = pairs, ipairs;
16 local newproxy, getmetatable, setmetatable = newproxy, getmetatable, setmetatable;
19 local has_array, array = pcall(require, "util.array");
20 local array_mt = has_array and getmetatable(array()) or {};
25 local null = newproxy and newproxy(true) or {};
26 if getmetatable and getmetatable(null) then
27 getmetatable(null).__tostring = function() return "null"; end;
32 ["\""] = "\\\"", ["\\"] = "\\\\", ["\b"] = "\\b",
33 ["\f"] = "\\f", ["\n"] = "\\n", ["\r"] = "\\r", ["\t"] = "\\t"};
35 ["\""] = "\"", ["\\"] = "\\", ["/"] = "/",
36 b = "\b", f = "\f", n = "\n", r = "\r", t = "\t"};
39 if not escapes[ch] then escapes[ch] = ("\\u%.4X"):format(i); end
42 local function codepoint_to_utf8(code)
43 if code < 0x80 then return s_char(code); end
44 local bits0_6 = code % 64;
46 local bits6_5 = (code - bits0_6) / 64;
47 return s_char(0x80 + 0x40 + bits6_5, 0x80 + bits0_6);
49 local bits0_12 = code % 4096;
50 local bits6_6 = (bits0_12 - bits0_6) / 64;
51 local bits12_4 = (code - bits0_12) / 4096;
52 return s_char(0x80 + 0x40 + 0x20 + bits12_4, 0x80 + bits6_6, 0x80 + bits0_6);
61 local special_keys = {
66 local simplesave, tablesave, arraysave, stringsave;
68 function stringsave(o, buffer)
69 -- FIXME do proper utf-8 and binary data detection
70 t_insert(buffer, "\""..(o:gsub(".", escapes)).."\"");
73 function arraysave(o, buffer)
74 t_insert(buffer, "[");
76 for i,v in ipairs(o) do
77 simplesave(v, buffer);
78 t_insert(buffer, ",");
82 t_insert(buffer, "]");
85 function tablesave(o, buffer)
89 for i,v in ipairs(o) do
92 for k,v in pairs(o) do
93 local ktype, vtype = type(k), type(v);
94 if valid_types[vtype] or v == null then
95 if ktype == "string" and not special_keys[k] then
97 elseif (valid_types[ktype] or k == null) and __array[k] == nil then
102 if next(__hash) ~= nil or next(hash) ~= nil or next(__array) == nil then
103 t_insert(buffer, "{");
104 local mark = #buffer;
105 if buffer.ordered then
107 for k in pairs(hash) do
111 for _,k in ipairs(keys) do
112 stringsave(k, buffer);
113 t_insert(buffer, ":");
114 simplesave(hash[k], buffer);
115 t_insert(buffer, ",");
118 for k,v in pairs(hash) do
119 stringsave(k, buffer);
120 t_insert(buffer, ":");
121 simplesave(v, buffer);
122 t_insert(buffer, ",");
125 if next(__hash) ~= nil then
126 t_insert(buffer, "\"__hash\":[");
127 for k,v in pairs(__hash) do
128 simplesave(k, buffer);
129 t_insert(buffer, ",");
130 simplesave(v, buffer);
131 t_insert(buffer, ",");
134 t_insert(buffer, "]");
135 t_insert(buffer, ",");
137 if next(__array) then
138 t_insert(buffer, "\"__array\":");
139 arraysave(__array, buffer);
140 t_insert(buffer, ",");
142 if mark ~= #buffer then t_remove(buffer); end
143 t_insert(buffer, "}");
145 arraysave(__array, buffer);
149 function simplesave(o, buffer)
151 if t == "number" then
152 t_insert(buffer, tostring(o));
153 elseif t == "string" then
154 stringsave(o, buffer);
155 elseif t == "table" then
156 local mt = getmetatable(o);
157 if mt == array_mt then
158 arraysave(o, buffer);
160 tablesave(o, buffer);
162 elseif t == "boolean" then
163 t_insert(buffer, (o and "true" or "false"));
165 t_insert(buffer, "null");
169 function json.encode(obj)
174 function json.encode_ordered(obj)
175 local t = { ordered = true };
179 function json.encode_array(obj)
185 -----------------------------------
188 function json.decode(json)
189 json = json.." "; -- appending a space ensures valid json wouldn't touch EOF
194 local function next()
195 ch = json:sub(pos, pos);
196 if ch == "" then error("Unexpected EOF"); end
198 peek = json:sub(pos, pos);
202 local function skipwhitespace()
203 while ch and (ch == "\r" or ch == "\n" or ch == "\t" or ch == " ") do
207 local function skiplinecomment()
208 repeat next(); until not(ch) or ch == "\r" or ch == "\n";
211 local function skipstarcomment()
212 next(); next(); -- skip '/', '*'
213 while peek and ch ~= "*" and peek ~= "/" do next(); end
214 if not peek then error("eof in star comment") end
215 next(); next(); -- skip '*', '/'
218 local function skipstuff()
221 if ch == "/" and peek == "*" then
223 elseif ch == "/" and peek == "/" then
232 local function readarray()
233 local t = setmetatable({}, array_mt);
236 if ch == "]" then next(); return t; end
237 t_insert(t, readvalue());
240 if ch == "]" then next(); return t; end
241 if not ch then error("eof while reading array");
242 elseif ch == "," then next();
243 elseif ch then error("unexpected character in array, comma expected"); end
244 if not ch then error("eof while reading array"); end
245 t_insert(t, readvalue());
249 local function checkandskip(c)
250 local x = ch or "eof";
251 if x ~= c then error("unexpected "..x..", '"..c.."' expected"); end
254 local function readliteral(lit, val)
255 for c in lit:gmatch(".") do
260 local function readstring()
264 while ch and ch ~= "\\" and ch ~= "\"" do
265 t_insert(s, ch); next();
269 if unescapes[ch] then
270 t_insert(s, unescapes[ch]);
272 elseif ch == "u" then
276 if not ch then error("unexpected eof in string"); end
277 if not ch:match("[0-9a-fA-F]") then error("invalid unicode escape sequence in string"); end
280 t_insert(s, codepoint_to_utf8(tonumber(seq, 16)));
282 else error("invalid escape sequence in string"); end
289 error("eof while reading string");
291 local function readnumber()
295 if not ch:match("[0-9]") then error("number format error"); end
299 if ch:match("[0-9]") then error("number format error"); end
301 while ch and ch:match("[0-9]") do
307 if not ch:match("[0-9]") then error("number format error"); end
308 while ch and ch:match("[0-9]") do
311 if ch == "e" or ch == "E" then
313 if ch == "+" or ch == "-" then
315 if not ch:match("[0-9]") then error("number format error"); end
316 while ch and ch:match("[0-9]") do
324 local function readmember(t)
326 local k = readstring();
331 local function fixobject(obj)
332 local __array = obj.__array;
335 for i,v in ipairs(__array) do
339 local __hash = obj.__hash;
343 for i,v in ipairs(__hash) do
353 local function readobject()
357 if ch == "}" then next(); return t; end
358 if not ch then error("eof while reading object"); end
362 if ch == "}" then next(); return fixobject(t); end
363 if not ch then error("eof while reading object");
364 elseif ch == "," then next();
365 elseif ch then error("unexpected character in object, comma expected"); end
366 if not ch then error("eof while reading object"); end
376 elseif ch == "[" then
378 elseif ch == "\"" then
380 elseif ch:match("[%-0-9%.]") then
382 elseif ch == "n" then
383 return readliteral("null", null);
384 elseif ch == "t" then
385 return readliteral("true", true);
386 elseif ch == "f" then
387 return readliteral("false", false);
389 error("invalid character at value start: "..ch);
392 error("eof while reading value");
398 function json.test(object)
399 local encoded = json.encode(object);
400 local decoded = json.decode(encoded);
401 local recoded = json.encode(decoded);
402 if encoded ~= recoded then
404 print("encoded:", encoded);
405 print("recoded:", recoded);
409 return encoded == recoded;