2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
5 -- utf8char copyright (C) 2007 Rici Lake
7 -- This project is MIT/X11 licensed. Please see the
8 -- COPYING file in the source package for more information.
12 local t_insert, t_concat, t_remove, t_sort = table.insert, table.concat, table.remove, table.sort;
13 local s_char = string.char;
14 local tostring, tonumber = tostring, tonumber;
15 local pairs, ipairs = pairs, ipairs;
18 local newproxy, getmetatable = newproxy, getmetatable;
24 local null = newproxy and newproxy(true) or {};
25 if getmetatable and getmetatable(null) then
26 getmetatable(null).__tostring = function() return "null"; end;
31 ["\""] = "\\\"", ["\\"] = "\\\\", ["\b"] = "\\b",
32 ["\f"] = "\\f", ["\n"] = "\\n", ["\r"] = "\\r", ["\t"] = "\\t"};
34 ["\""] = "\"", ["\\"] = "\\", ["/"] = "/",
35 b = "\b", f = "\f", n = "\n", r = "\r", t = "\t"};
38 if not escapes[ch] then escapes[ch] = ("\\u%.4X"):format(i); end
41 local function utf8char(i)
50 return s_char(0xC0+i, 0x80+c1)
54 if i < 16 and (i ~= 13 or c2 < 32) then
55 return s_char(0xE0+i, 0x80+c2, 0x80+c1)
56 elseif i >= 16 and i < 0x110 then
59 return s_char(0xF0+i, 0x80+c3, 0x80+c2, 0x80+c1)
73 local special_keys = {
78 local simplesave, tablesave, arraysave, stringsave;
80 function stringsave(o, buffer)
81 -- FIXME do proper utf-8 and binary data detection
82 t_insert(buffer, "\""..(o:gsub(".", escapes)).."\"");
85 function arraysave(o, buffer)
86 t_insert(buffer, "[");
88 for i,v in ipairs(o) do
89 simplesave(v, buffer);
90 t_insert(buffer, ",");
94 t_insert(buffer, "]");
97 function tablesave(o, buffer)
101 for i,v in ipairs(o) do
104 for k,v in pairs(o) do
105 local ktype, vtype = type(k), type(v);
106 if valid_types[vtype] or v == null then
107 if ktype == "string" and not special_keys[k] then
109 elseif (valid_types[ktype] or k == null) and __array[k] == nil then
114 if next(__hash) ~= nil or next(hash) ~= nil or next(__array) == nil then
115 t_insert(buffer, "{");
116 local mark = #buffer;
117 if buffer.ordered then
119 for k in pairs(hash) do
123 for _,k in ipairs(keys) do
124 stringsave(k, buffer);
125 t_insert(buffer, ":");
126 simplesave(hash[k], buffer);
127 t_insert(buffer, ",");
130 for k,v in pairs(hash) do
131 stringsave(k, buffer);
132 t_insert(buffer, ":");
133 simplesave(v, buffer);
134 t_insert(buffer, ",");
137 if next(__hash) ~= nil then
138 t_insert(buffer, "\"__hash\":[");
139 for k,v in pairs(__hash) do
140 simplesave(k, buffer);
141 t_insert(buffer, ",");
142 simplesave(v, buffer);
143 t_insert(buffer, ",");
146 t_insert(buffer, "]");
147 t_insert(buffer, ",");
149 if next(__array) then
150 t_insert(buffer, "\"__array\":");
151 arraysave(__array, buffer);
152 t_insert(buffer, ",");
154 if mark ~= #buffer then t_remove(buffer); end
155 t_insert(buffer, "}");
157 arraysave(__array, buffer);
161 function simplesave(o, buffer)
163 if t == "number" then
164 t_insert(buffer, tostring(o));
165 elseif t == "string" then
166 stringsave(o, buffer);
167 elseif t == "table" then
168 tablesave(o, buffer);
169 elseif t == "boolean" then
170 t_insert(buffer, (o and "true" or "false"));
172 t_insert(buffer, "null");
176 function json.encode(obj)
181 function json.encode_ordered(obj)
182 local t = { ordered = true };
186 function json.encode_array(obj)
192 -----------------------------------
195 function json.decode(json)
196 json = json.." "; -- appending a space ensures valid json wouldn't touch EOF
201 local function next()
202 ch = json:sub(pos, pos);
203 if ch == "" then error("Unexpected EOF"); end
205 peek = json:sub(pos, pos);
209 local function skipwhitespace()
210 while ch and (ch == "\r" or ch == "\n" or ch == "\t" or ch == " ") do
214 local function skiplinecomment()
215 repeat next(); until not(ch) or ch == "\r" or ch == "\n";
218 local function skipstarcomment()
219 next(); next(); -- skip '/', '*'
220 while peek and ch ~= "*" and peek ~= "/" do next(); end
221 if not peek then error("eof in star comment") end
222 next(); next(); -- skip '*', '/'
225 local function skipstuff()
228 if ch == "/" and peek == "*" then
230 elseif ch == "/" and peek == "/" then
239 local function readarray()
243 if ch == "]" then next(); return t; end
244 t_insert(t, readvalue());
247 if ch == "]" then next(); return t; end
248 if not ch then error("eof while reading array");
249 elseif ch == "," then next();
250 elseif ch then error("unexpected character in array, comma expected"); end
251 if not ch then error("eof while reading array"); end
252 t_insert(t, readvalue());
256 local function checkandskip(c)
257 local x = ch or "eof";
258 if x ~= c then error("unexpected "..x..", '"..c.."' expected"); end
261 local function readliteral(lit, val)
262 for c in lit:gmatch(".") do
267 local function readstring()
271 while ch and ch ~= "\\" and ch ~= "\"" do
276 if unescapes[ch] then
277 s = s..unescapes[ch];
279 elseif ch == "u" then
283 if not ch then error("unexpected eof in string"); end
284 if not ch:match("[0-9a-fA-F]") then error("invalid unicode escape sequence in string"); end
287 s = s..utf8char(tonumber(seq, 16));
289 else error("invalid escape sequence in string"); end
296 error("eof while reading string");
298 local function readnumber()
302 if not ch:match("[0-9]") then error("number format error"); end
306 if ch:match("[0-9]") then error("number format error"); end
308 while ch and ch:match("[0-9]") do
314 if not ch:match("[0-9]") then error("number format error"); end
315 while ch and ch:match("[0-9]") do
318 if ch == "e" or ch == "E" then
320 if ch == "+" or ch == "-" then
322 if not ch:match("[0-9]") then error("number format error"); end
323 while ch and ch:match("[0-9]") do
331 local function readmember(t)
333 local k = readstring();
338 local function fixobject(obj)
339 local __array = obj.__array;
342 for i,v in ipairs(__array) do
346 local __hash = obj.__hash;
350 for i,v in ipairs(__hash) do
360 local function readobject()
364 if ch == "}" then next(); return t; end
365 if not ch then error("eof while reading object"); end
369 if ch == "}" then next(); return fixobject(t); end
370 if not ch then error("eof while reading object");
371 elseif ch == "," then next();
372 elseif ch then error("unexpected character in object, comma expected"); end
373 if not ch then error("eof while reading object"); end
383 elseif ch == "[" then
385 elseif ch == "\"" then
387 elseif ch:match("[%-0-9%.]") then
389 elseif ch == "n" then
390 return readliteral("null", null);
391 elseif ch == "t" then
392 return readliteral("true", true);
393 elseif ch == "f" then
394 return readliteral("false", false);
396 error("invalid character at value start: "..ch);
399 error("eof while reading value");
405 function json.test(object)
406 local encoded = json.encode(object);
407 local decoded = json.decode(encoded);
408 local recoded = json.encode(decoded);
409 if encoded ~= recoded then
411 print("encoded:", encoded);
412 print("recoded:", recoded);
416 return encoded == recoded;