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;
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 if buffer.ordered then
84 for k in pairs(hash) do
88 for _,k in ipairs(keys) do
89 stringsave(k, buffer);
90 t_insert(buffer, ":");
91 simplesave(hash[k], buffer);
92 t_insert(buffer, ",");
95 for k,v in pairs(hash) do
96 stringsave(k, buffer);
97 t_insert(buffer, ":");
98 simplesave(v, buffer);
99 t_insert(buffer, ",");
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, ",");
111 t_insert(buffer, "]");
112 t_insert(buffer, ",");
114 if next(__array) then
115 t_insert(buffer, "\"__array\":");
116 arraysave(__array, buffer);
117 t_insert(buffer, ",");
119 if mark ~= #buffer then t_remove(buffer); end
120 t_insert(buffer, "}");
122 arraysave(__array, buffer);
126 function simplesave(o, buffer)
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"));
137 t_insert(buffer, "null");
141 function json.encode(obj)
146 function json.encode_ordered(obj)
147 local t = { ordered = true };
152 -----------------------------------
155 function json.decode(json)
156 json = json.." "; -- appending a space ensures valid json wouldn't touch EOF
161 local function next()
162 ch = json:sub(pos, pos);
163 if ch == "" then error("Unexpected EOF"); end
165 peek = json:sub(pos, pos);
169 local function skipwhitespace()
170 while ch and (ch == "\r" or ch == "\n" or ch == "\t" or ch == " ") do
174 local function skiplinecomment()
175 repeat next(); until not(ch) or ch == "\r" or ch == "\n";
178 local function skipstarcomment()
179 next(); next(); -- skip '/', '*'
180 while peek and ch ~= "*" and peek ~= "/" do next(); end
181 if not peek then error("eof in star comment") end
182 next(); next(); -- skip '*', '/'
185 local function skipstuff()
188 if ch == "/" and peek == "*" then
190 elseif ch == "/" and peek == "/" then
199 local function readarray()
203 if ch == "]" then next(); return t; end
204 t_insert(t, readvalue());
207 if ch == "]" then next(); return t; end
208 if not ch then error("eof while reading array");
209 elseif ch == "," then next();
210 elseif ch then error("unexpected character in array, comma expected"); end
211 if not ch then error("eof while reading array"); end
212 t_insert(t, readvalue());
216 local function checkandskip(c)
217 local x = ch or "eof";
218 if x ~= c then error("unexpected "..x..", '"..c.."' expected"); end
221 local function readliteral(lit, val)
222 for c in lit:gmatch(".") do
227 local function readstring()
231 while ch and ch ~= "\\" and ch ~= "\"" do
236 if unescapes[ch] then
237 s = s..unescapes[ch];
239 elseif ch == "u" then
243 if not ch then error("unexpected eof in string"); end
244 if not ch:match("[0-9a-fA-F]") then error("invalid unicode escape sequence in string"); end
247 s = s..s.char(tonumber(seq, 16)); -- FIXME do proper utf-8
249 else error("invalid escape sequence in string"); end
256 error("eof while reading string");
258 local function readnumber()
262 if not ch:match("[0-9]") then error("number format error"); end
266 if ch:match("[0-9]") then error("number format error"); end
268 while ch and ch:match("[0-9]") do
274 if not ch:match("[0-9]") then error("number format error"); end
275 while ch and ch:match("[0-9]") do
278 if ch == "e" or ch == "E" then
280 if ch == "+" or ch == "-" then
282 if not ch:match("[0-9]") then error("number format error"); end
283 while ch and ch:match("[0-9]") do
291 local function readmember(t)
293 local k = readstring();
298 local function fixobject(obj)
299 local __array = obj.__array;
302 for i,v in ipairs(__array) do
306 local __hash = obj.__hash;
310 for i,v in ipairs(__hash) do
320 local function readobject()
324 if ch == "}" then next(); return t; end
325 if not ch then error("eof while reading object"); end
329 if ch == "}" then next(); return fixobject(t); end
330 if not ch then error("eof while reading object");
331 elseif ch == "," then next();
332 elseif ch then error("unexpected character in object, comma expected"); end
333 if not ch then error("eof while reading object"); end
343 elseif ch == "[" then
345 elseif ch == "\"" then
347 elseif ch:match("[%-0-9%.]") then
349 elseif ch == "n" then
350 return readliteral("null", null);
351 elseif ch == "t" then
352 return readliteral("true", true);
353 elseif ch == "f" then
354 return readliteral("false", false);
356 error("invalid character at value start: "..ch);
359 error("eof while reading value");
365 function json.test(object)
366 local encoded = json.encode(object);
367 local decoded = json.decode(encoded);
368 local recoded = json.encode(decoded);
369 if encoded ~= recoded then
371 print("encoded:", encoded);
372 print("recoded:", recoded);
376 return encoded == recoded;