3 local t_insert, t_concat, t_remove = table.insert, table.concat, table.remove;
4 local s_char = string.char;
5 local tostring, tonumber = tostring, tonumber;
6 local pairs, ipairs = pairs, ipairs;
9 local newproxy, getmetatable = newproxy, getmetatable;
15 local null = newproxy and newproxy(true) or {};
16 if getmetatable and getmetatable(null) then
17 getmetatable(null).__tostring = function() return "null"; end;
22 ["\""] = "\\\"", ["\\"] = "\\\\", ["\b"] = "\\b",
23 ["\f"] = "\\f", ["\n"] = "\\n", ["\r"] = "\\r", ["\t"] = "\\t"};
25 ["\""] = "\"", ["\\"] = "\\", ["/"] = "/",
26 b = "\b", f = "\f", n = "\n", r = "\r", t = "\t"};
29 if not escapes[ch] then escapes[ch] = ("\\u%.4X"):format(i); end
38 local special_keys = {
43 local simplesave, tablesave, arraysave, stringsave;
45 function stringsave(o, buffer)
46 -- FIXME do proper utf-8 and binary data detection
47 t_insert(buffer, "\""..(o:gsub(".", escapes)).."\"");
50 function arraysave(o, buffer)
51 t_insert(buffer, "[");
53 for i,v in ipairs(o) do
54 simplesave(v, buffer);
55 t_insert(buffer, ",");
59 t_insert(buffer, "]");
62 function tablesave(o, buffer)
66 for i,v in ipairs(o) do
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
74 elseif (valid_types[ktype] or k == null) and __array[k] == nil then
79 if next(__hash) ~= nil or next(hash) ~= nil or next(__array) == nil then
80 t_insert(buffer, "{");
82 for k,v in pairs(hash) do
83 stringsave(k, buffer);
84 t_insert(buffer, ":");
85 simplesave(v, buffer);
86 t_insert(buffer, ",");
88 if next(__hash) ~= nil then
89 t_insert(buffer, "\"__hash\":[");
90 for k,v in pairs(__hash) do
91 simplesave(k, buffer);
92 t_insert(buffer, ",");
93 simplesave(v, buffer);
94 t_insert(buffer, ",");
97 t_insert(buffer, "]");
98 t_insert(buffer, ",");
100 if next(__array) then
101 t_insert(buffer, "\"__array\":");
102 arraysave(__array, buffer);
103 t_insert(buffer, ",");
105 if mark ~= #buffer then t_remove(buffer); end
106 t_insert(buffer, "}");
108 arraysave(__array, buffer);
112 function simplesave(o, buffer)
114 if t == "number" then
115 t_insert(buffer, tostring(o));
116 elseif t == "string" then
117 stringsave(o, buffer);
118 elseif t == "table" then
119 tablesave(o, buffer);
120 elseif t == "boolean" then
121 t_insert(buffer, (o and "true" or "false"));
123 t_insert(buffer, "null");
127 function json.encode(obj)
133 -----------------------------------
136 function json.decode(json)
137 json = json.." "; -- appending a space ensures valid json wouldn't touch EOF
142 local function next()
143 ch = json:sub(pos, pos);
144 if ch == "" then error("Unexpected EOF"); end
146 peek = json:sub(pos, pos);
150 local function skipwhitespace()
151 while ch and (ch == "\r" or ch == "\n" or ch == "\t" or ch == " ") do
155 local function skiplinecomment()
156 repeat next(); until not(ch) or ch == "\r" or ch == "\n";
159 local function skipstarcomment()
160 next(); next(); -- skip '/', '*'
161 while peek and ch ~= "*" and peek ~= "/" do next(); end
162 if not peek then error("eof in star comment") end
163 next(); next(); -- skip '*', '/'
166 local function skipstuff()
169 if ch == "/" and peek == "*" then
171 elseif ch == "/" and peek == "*" then
180 local function readarray()
184 if ch == "]" then next(); return t; end
185 t_insert(t, readvalue());
188 if ch == "]" then next(); return t; end
189 if not ch then error("eof while reading array");
190 elseif ch == "," then next();
191 elseif ch then error("unexpected character in array, comma expected"); end
192 if not ch then error("eof while reading array"); end
193 t_insert(t, readvalue());
197 local function checkandskip(c)
198 local x = ch or "eof";
199 if x ~= c then error("unexpected "..x..", '"..c.."' expected"); end
202 local function readliteral(lit, val)
203 for c in lit:gmatch(".") do
208 local function readstring()
212 while ch and ch ~= "\\" and ch ~= "\"" do
217 if unescapes[ch] then
218 s = s..unescapes[ch];
220 elseif ch == "u" then
224 if not ch then error("unexpected eof in string"); end
225 if not ch:match("[0-9a-fA-F]") then error("invalid unicode escape sequence in string"); end
228 s = s..s.char(tonumber(seq, 16)); -- FIXME do proper utf-8
230 else error("invalid escape sequence in string"); end
237 error("eof while reading string");
239 local function readnumber()
243 if not ch:match("[0-9]") then error("number format error"); end
247 if ch:match("[0-9]") then error("number format error"); end
249 while ch and ch:match("[0-9]") do
255 if not ch:match("[0-9]") then error("number format error"); end
256 while ch and ch:match("[0-9]") do
259 if ch == "e" or ch == "E" then
261 if ch == "+" or ch == "-" then
263 if not ch:match("[0-9]") then error("number format error"); end
264 while ch and ch:match("[0-9]") do
272 local function readmember(t)
274 local k = readstring();
279 local function fixobject(obj)
280 local __array = obj.__array;
283 for i,v in ipairs(__array) do
287 local __hash = obj.__hash;
291 for i,v in ipairs(__hash) do
301 local function readobject()
305 if ch == "}" then next(); return t; end
306 if not ch then error("eof while reading object"); end
310 if ch == "}" then next(); return fixobject(t); end
311 if not ch then error("eof while reading object");
312 elseif ch == "," then next();
313 elseif ch then error("unexpected character in object, comma expected"); end
314 if not ch then error("eof while reading object"); end
324 elseif ch == "[" then
326 elseif ch == "\"" then
328 elseif ch:match("[%-0-9%.]") then
330 elseif ch == "n" then
331 return readliteral("null", null);
332 elseif ch == "t" then
333 return readliteral("true", true);
334 elseif ch == "f" then
335 return readliteral("false", false);
337 error("invalid character at value start: "..ch);
340 error("eof while reading value");
346 function json.test(object)
347 local encoded = json.encode(object);
348 local decoded = json.decode(encoded);
349 local recoded = json.encode(decoded);
350 if encoded ~= recoded then
352 print("encoded:", encoded);
353 print("recoded:", recoded);
357 return encoded == recoded;