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;
15 local getmetatable, setmetatable = getmetatable, setmetatable;
18 local has_array, array = pcall(require, "util.array");
19 local array_mt = has_array and getmetatable(array()) or {};
24 local null = setmetatable({}, { __tostring = function() return "null"; end; });
28 ["\""] = "\\\"", ["\\"] = "\\\\", ["\b"] = "\\b",
29 ["\f"] = "\\f", ["\n"] = "\\n", ["\r"] = "\\r", ["\t"] = "\\t"};
31 ["\""] = "\"", ["\\"] = "\\", ["/"] = "/",
32 b = "\b", f = "\f", n = "\n", r = "\r", t = "\t"};
35 if not escapes[ch] then escapes[ch] = ("\\u%.4X"):format(i); end
38 local function codepoint_to_utf8(code)
39 if code < 0x80 then return s_char(code); end
40 local bits0_6 = code % 64;
42 local bits6_5 = (code - bits0_6) / 64;
43 return s_char(0x80 + 0x40 + bits6_5, 0x80 + bits0_6);
45 local bits0_12 = code % 4096;
46 local bits6_6 = (bits0_12 - bits0_6) / 64;
47 local bits12_4 = (code - bits0_12) / 4096;
48 return s_char(0x80 + 0x40 + 0x20 + bits12_4, 0x80 + bits6_6, 0x80 + bits0_6);
57 local special_keys = {
62 local simplesave, tablesave, arraysave, stringsave;
64 function stringsave(o, buffer)
65 -- FIXME do proper utf-8 and binary data detection
66 t_insert(buffer, "\""..(o:gsub(".", escapes)).."\"");
69 function arraysave(o, buffer)
70 t_insert(buffer, "[");
72 for _, v in ipairs(o) do
73 simplesave(v, buffer);
74 t_insert(buffer, ",");
78 t_insert(buffer, "]");
81 function tablesave(o, buffer)
85 for i,v in ipairs(o) do
88 for k,v in pairs(o) do
89 local ktype, vtype = type(k), type(v);
90 if valid_types[vtype] or v == null then
91 if ktype == "string" and not special_keys[k] then
93 elseif (valid_types[ktype] or k == null) and __array[k] == nil then
98 if next(__hash) ~= nil or next(hash) ~= nil or next(__array) == nil then
99 t_insert(buffer, "{");
100 local mark = #buffer;
101 if buffer.ordered then
103 for k in pairs(hash) do
107 for _,k in ipairs(keys) do
108 stringsave(k, buffer);
109 t_insert(buffer, ":");
110 simplesave(hash[k], buffer);
111 t_insert(buffer, ",");
114 for k,v in pairs(hash) do
115 stringsave(k, buffer);
116 t_insert(buffer, ":");
117 simplesave(v, buffer);
118 t_insert(buffer, ",");
121 if next(__hash) ~= nil then
122 t_insert(buffer, "\"__hash\":[");
123 for k,v in pairs(__hash) do
124 simplesave(k, buffer);
125 t_insert(buffer, ",");
126 simplesave(v, buffer);
127 t_insert(buffer, ",");
130 t_insert(buffer, "]");
131 t_insert(buffer, ",");
133 if next(__array) then
134 t_insert(buffer, "\"__array\":");
135 arraysave(__array, buffer);
136 t_insert(buffer, ",");
138 if mark ~= #buffer then t_remove(buffer); end
139 t_insert(buffer, "}");
141 arraysave(__array, buffer);
145 function simplesave(o, buffer)
148 t_insert(buffer, "null");
149 elseif t == "number" then
150 t_insert(buffer, tostring(o));
151 elseif t == "string" then
152 stringsave(o, buffer);
153 elseif t == "table" then
154 local mt = getmetatable(o);
155 if mt == array_mt then
156 arraysave(o, buffer);
158 tablesave(o, buffer);
160 elseif t == "boolean" then
161 t_insert(buffer, (o and "true" or "false"));
163 t_insert(buffer, "null");
167 function module.encode(obj)
172 function module.encode_ordered(obj)
173 local t = { ordered = true };
177 function module.encode_array(obj)
183 -----------------------------------
186 local function _skip_whitespace(json, index)
187 return json:find("[^ \t\r\n]", index) or index; -- no need to check \r\n, we converted those to \t
189 local function _fixobject(obj)
190 local __array = obj.__array;
193 for _, v in ipairs(__array) do
197 local __hash = obj.__hash;
201 for _, v in ipairs(__hash) do
211 local _readvalue, _readstring;
212 local function _readobject(json, index)
216 index = _skip_whitespace(json, index + 1);
217 if json:byte(index) ~= 0x22 then -- "\""
218 if json:byte(index) == 0x7d then return o, index + 1; end -- "}"
219 return nil, "key expected";
221 key, index = _readstring(json, index);
222 if key == nil then return nil, index; end
223 index = _skip_whitespace(json, index);
224 if json:byte(index) ~= 0x3a then return nil, "colon expected"; end -- ":"
225 val, index = _readvalue(json, index + 1);
226 if val == nil then return nil, index; end
228 index = _skip_whitespace(json, index);
229 local b = json:byte(index);
230 if b == 0x7d then return _fixobject(o), index + 1; end -- "}"
231 if b ~= 0x2c then return nil, "object eof"; end -- ","
234 local function _readarray(json, index)
236 local oindex = index;
239 val, index = _readvalue(json, index + 1);
241 if json:byte(oindex + 1) == 0x5d then return setmetatable(a, array_mt), oindex + 2; end -- "]"
245 index = _skip_whitespace(json, index);
246 local b = json:byte(index);
247 if b == 0x5d then return setmetatable(a, array_mt), index + 1; end -- "]"
248 if b ~= 0x2c then return nil, "array eof"; end -- ","
251 local _unescape_error;
252 local function _unescape_surrogate_func(x)
253 local lead, trail = tonumber(x:sub(3, 6), 16), tonumber(x:sub(9, 12), 16);
254 local codepoint = lead * 0x400 + trail - 0x35FDC00;
255 local a = codepoint % 64;
256 codepoint = (codepoint - a) / 64;
257 local b = codepoint % 64;
258 codepoint = (codepoint - b) / 64;
259 local c = codepoint % 64;
260 codepoint = (codepoint - c) / 64;
261 return s_char(0xF0 + codepoint, 0x80 + c, 0x80 + b, 0x80 + a);
263 local function _unescape_func(x)
264 x = x:match("%x%x%x%x", 3);
266 --if x >= 0xD800 and x <= 0xDFFF then _unescape_error = true; end -- bad surrogate pair
267 return codepoint_to_utf8(tonumber(x, 16));
269 _unescape_error = true;
271 function _readstring(json, index)
273 local endindex = json:find("\"", index, true);
275 local s = json:sub(index, endindex - 1);
276 --if s:find("[%z-\31]") then return nil, "control char in string"; end
277 -- FIXME handle control characters
278 _unescape_error = nil;
279 --s = s:gsub("\\u[dD][89abAB]%x%x\\u[dD][cdefCDEF]%x%x", _unescape_surrogate_func);
280 -- FIXME handle escapes beyond BMP
281 s = s:gsub("\\u.?.?.?.?", _unescape_func);
282 if _unescape_error then return nil, "invalid escape"; end
283 return s, endindex + 1;
285 return nil, "string eof";
287 local function _readnumber(json, index)
288 local m = json:match("[0-9%.%-eE%+]+", index); -- FIXME do strict checking
289 return tonumber(m), index + #m;
291 local function _readnull(json, index)
292 local a, b, c = json:byte(index + 1, index + 3);
293 if a == 0x75 and b == 0x6c and c == 0x6c then
294 return null, index + 4;
296 return nil, "null parse failed";
298 local function _readtrue(json, index)
299 local a, b, c = json:byte(index + 1, index + 3);
300 if a == 0x72 and b == 0x75 and c == 0x65 then
301 return true, index + 4;
303 return nil, "true parse failed";
305 local function _readfalse(json, index)
306 local a, b, c, d = json:byte(index + 1, index + 4);
307 if a == 0x61 and b == 0x6c and c == 0x73 and d == 0x65 then
308 return false, index + 5;
310 return nil, "false parse failed";
312 function _readvalue(json, index)
313 index = _skip_whitespace(json, index);
314 local b = json:byte(index);
315 -- TODO try table lookup instead of if-else?
316 if b == 0x7B then -- "{"
317 return _readobject(json, index);
318 elseif b == 0x5B then -- "["
319 return _readarray(json, index);
320 elseif b == 0x22 then -- "\""
321 return _readstring(json, index);
322 elseif b ~= nil and b >= 0x30 and b <= 0x39 or b == 0x2d then -- "0"-"9" or "-"
323 return _readnumber(json, index);
324 elseif b == 0x6e then -- "n"
325 return _readnull(json, index);
326 elseif b == 0x74 then -- "t"
327 return _readtrue(json, index);
328 elseif b == 0x66 then -- "f"
329 return _readfalse(json, index);
331 return nil, "value expected";
334 local first_escape = {
335 ["\\\""] = "\\u0022";
336 ["\\\\"] = "\\u005c";
337 ["\\/" ] = "\\u002f";
338 ["\\b" ] = "\\u0008";
339 ["\\f" ] = "\\u000C";
340 ["\\n" ] = "\\u000A";
341 ["\\r" ] = "\\u000D";
342 ["\\t" ] = "\\u0009";
346 function module.decode(json)
347 json = json:gsub("\\.", first_escape) -- get rid of all escapes except \uXXXX, making string parsing much simpler
348 --:gsub("[\r\n]", "\t"); -- \r\n\t are equivalent, we care about none of them, and none of them can be in strings
350 -- TODO do encoding verification
352 local val, index = _readvalue(json, 1);
353 if val == nil then return val, index; end
354 if json:find("[^ \t\r\n]", index) then return nil, "garbage at eof"; end
359 function module.test(object)
360 local encoded = module.encode(object);
361 local decoded = module.decode(encoded);
362 local recoded = module.encode(decoded);
363 if encoded ~= recoded then
365 print("encoded:", encoded);
366 print("recoded:", recoded);
370 return encoded == recoded;