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)
141 local function next()
142 ch = json:sub(pos, pos);
144 peek = json:sub(pos, pos);
148 local function skipwhitespace()
149 while ch and (ch == "\r" or ch == "\n" or ch == "\t" or ch == " ") do
153 local function skiplinecomment()
154 repeat next(); until not(ch) or ch == "\r" or ch == "\n";
157 local function skipstarcomment()
158 next(); next(); -- skip '/', '*'
159 while peek and ch ~= "*" and peek ~= "/" do next(); end
160 if not peek then error("eof in star comment") end
161 next(); next(); -- skip '*', '/'
164 local function skipstuff()
167 if ch == "/" and peek == "*" then
169 elseif ch == "/" and peek == "*" then
178 local function readarray()
182 if ch == "]" then next(); return t; end
183 t_insert(t, readvalue());
186 if ch == "]" then next(); return t; end
187 if not ch then error("eof while reading array");
188 elseif ch == "," then next();
189 elseif ch then error("unexpected character in array, comma expected"); end
190 if not ch then error("eof while reading array"); end
191 t_insert(t, readvalue());
195 local function checkandskip(c)
196 local x = ch or "eof";
197 if x ~= c then error("unexpected "..x..", '"..c.."' expected"); end
200 local function readliteral(lit, val)
201 for c in lit:gmatch(".") do
206 local function readstring()
210 while ch and ch ~= "\\" and ch ~= "\"" do
215 if unescapes[ch] then
216 s = s..unescapes[ch];
218 elseif ch == "u" then
222 if not ch then error("unexpected eof in string"); end
223 if not ch:match("[0-9a-fA-F]") then error("invalid unicode escape sequence in string"); end
226 s = s..s.char(tonumber(seq, 16)); -- FIXME do proper utf-8
228 else error("invalid escape sequence in string"); end
235 error("eof while reading string");
237 local function readnumber()
241 if not ch:match("[0-9]") then error("number format error"); end
245 if ch:match("[0-9]") then error("number format error"); end
247 while ch and ch:match("[0-9]") do
253 if not ch:match("[0-9]") then error("number format error"); end
254 while ch and ch:match("[0-9]") do
257 if ch == "e" or ch == "E" then
259 if ch == "+" or ch == "-" then
261 if not ch:match("[0-9]") then error("number format error"); end
262 while ch and ch:match("[0-9]") do
270 local function readmember(t)
271 local k = readstring();
275 local function fixobject(obj)
276 local __array = obj.__array;
279 for i,v in ipairs(__array) do
283 local __hash = obj.__hash;
287 for i,v in ipairs(__hash) do
297 local function readobject()
301 if ch == "}" then next(); return t; end
302 if not ch then error("eof while reading object"); end
306 if ch == "}" then next(); return fixobject(t); end
307 if not ch then error("eof while reading object");
308 elseif ch == "," then next();
309 elseif ch then error("unexpected character in object, comma expected"); end
310 if not ch then error("eof while reading object"); end
320 elseif ch == "[" then
322 elseif ch == "\"" then
324 elseif ch:match("[%-0-9%.]") then
326 elseif ch == "n" then
327 return readliteral("null", null);
328 elseif ch == "t" then
329 return readliteral("true", true);
330 elseif ch == "f" then
331 return readliteral("false", false);
333 error("invalid character at value start: "..ch);
336 error("eof while reading value");
342 function json.test(object)
343 local encoded = json.encode(object);
344 local decoded = json.decode(encoded);
345 local recoded = json.encode(decoded);
346 if encoded ~= recoded then
348 print("encoded:", encoded);
349 print("recoded:", recoded);
353 return encoded == recoded;