Annotate

util/json.lua @ 4284:20979f124ad9

util.json: Fixed handling of truncated JSON.
author Waqas Hussain <waqas20@gmail.com>
date Thu, 02 Jun 2011 05:36:15 +0500
parent 4147:7f119ebcf55f
child 4404:5356664ef9d4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4047
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
1
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
2 local type = type;
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
3 local t_insert, t_concat, t_remove = table.insert, table.concat, table.remove;
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
4 local s_char = string.char;
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
5 local tostring, tonumber = tostring, tonumber;
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
6 local pairs, ipairs = pairs, ipairs;
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
7 local next = next;
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
8 local error = error;
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
9 local newproxy, getmetatable = newproxy, getmetatable;
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
10 local print = print;
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
11
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
12 --module("json")
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
13 local json = {};
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
14
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
15 local null = newproxy and newproxy(true) or {};
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
16 if getmetatable and getmetatable(null) then
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
17 getmetatable(null).__tostring = function() return "null"; end;
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
18 end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
19 json.null = null;
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
20
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
21 local escapes = {
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
22 ["\""] = "\\\"", ["\\"] = "\\\\", ["\b"] = "\\b",
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
23 ["\f"] = "\\f", ["\n"] = "\\n", ["\r"] = "\\r", ["\t"] = "\\t"};
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
24 local unescapes = {
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
25 ["\""] = "\"", ["\\"] = "\\", ["/"] = "/",
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
26 b = "\b", f = "\f", n = "\n", r = "\r", t = "\t"};
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
27 for i=0,31 do
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
28 local ch = s_char(i);
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
29 if not escapes[ch] then escapes[ch] = ("\\u%.4X"):format(i); end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
30 end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
31
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
32 local valid_types = {
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
33 number = true,
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
34 string = true,
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
35 table = true,
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
36 boolean = true
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
37 };
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
38 local special_keys = {
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
39 __array = true;
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
40 __hash = true;
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
41 };
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
42
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
43 local simplesave, tablesave, arraysave, stringsave;
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
44
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
45 function stringsave(o, buffer)
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
46 -- FIXME do proper utf-8 and binary data detection
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
47 t_insert(buffer, "\""..(o:gsub(".", escapes)).."\"");
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
48 end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
49
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
50 function arraysave(o, buffer)
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
51 t_insert(buffer, "[");
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
52 if next(o) then
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
53 for i,v in ipairs(o) do
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
54 simplesave(v, buffer);
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
55 t_insert(buffer, ",");
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
56 end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
57 t_remove(buffer);
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
58 end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
59 t_insert(buffer, "]");
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
60 end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
61
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
62 function tablesave(o, buffer)
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
63 local __array = {};
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
64 local __hash = {};
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
65 local hash = {};
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
66 for i,v in ipairs(o) do
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
67 __array[i] = v;
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
68 end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
69 for k,v in pairs(o) do
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
70 local ktype, vtype = type(k), type(v);
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
71 if valid_types[vtype] or v == null then
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
72 if ktype == "string" and not special_keys[k] then
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
73 hash[k] = v;
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
74 elseif (valid_types[ktype] or k == null) and __array[k] == nil then
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
75 __hash[k] = v;
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
76 end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
77 end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
78 end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
79 if next(__hash) ~= nil or next(hash) ~= nil or next(__array) == nil then
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
80 t_insert(buffer, "{");
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
81 local mark = #buffer;
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
82 for k,v in pairs(hash) do
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
83 stringsave(k, buffer);
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
84 t_insert(buffer, ":");
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
85 simplesave(v, buffer);
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
86 t_insert(buffer, ",");
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
87 end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
88 if next(__hash) ~= nil then
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
89 t_insert(buffer, "\"__hash\":[");
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
90 for k,v in pairs(__hash) do
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
91 simplesave(k, buffer);
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
92 t_insert(buffer, ",");
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
93 simplesave(v, buffer);
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
94 t_insert(buffer, ",");
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
95 end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
96 t_remove(buffer);
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
97 t_insert(buffer, "]");
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
98 t_insert(buffer, ",");
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
99 end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
100 if next(__array) then
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
101 t_insert(buffer, "\"__array\":");
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
102 arraysave(__array, buffer);
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
103 t_insert(buffer, ",");
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
104 end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
105 if mark ~= #buffer then t_remove(buffer); end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
106 t_insert(buffer, "}");
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
107 else
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
108 arraysave(__array, buffer);
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
109 end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
110 end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
111
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
112 function simplesave(o, buffer)
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
113 local t = type(o);
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
114 if t == "number" then
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
115 t_insert(buffer, tostring(o));
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
116 elseif t == "string" then
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
117 stringsave(o, buffer);
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
118 elseif t == "table" then
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
119 tablesave(o, buffer);
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
120 elseif t == "boolean" then
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
121 t_insert(buffer, (o and "true" or "false"));
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
122 else
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
123 t_insert(buffer, "null");
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
124 end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
125 end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
126
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
127 function json.encode(obj)
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
128 local t = {};
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
129 simplesave(obj, t);
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
130 return t_concat(t);
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
131 end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
132
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
133 -----------------------------------
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
134
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
135
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
136 function json.decode(json)
4284
20979f124ad9 util.json: Fixed handling of truncated JSON.
Waqas Hussain <waqas20@gmail.com>
parents: 4147
diff changeset
137 json = json.." "; -- appending a space ensures valid json wouldn't touch EOF
4047
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
138 local pos = 1;
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
139 local current = {};
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
140 local stack = {};
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
141 local ch, peek;
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
142 local function next()
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
143 ch = json:sub(pos, pos);
4284
20979f124ad9 util.json: Fixed handling of truncated JSON.
Waqas Hussain <waqas20@gmail.com>
parents: 4147
diff changeset
144 if ch == "" then error("Unexpected EOF"); end
4047
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
145 pos = pos+1;
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
146 peek = json:sub(pos, pos);
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
147 return ch;
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
148 end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
149
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
150 local function skipwhitespace()
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
151 while ch and (ch == "\r" or ch == "\n" or ch == "\t" or ch == " ") do
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
152 next();
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
153 end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
154 end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
155 local function skiplinecomment()
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
156 repeat next(); until not(ch) or ch == "\r" or ch == "\n";
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
157 skipwhitespace();
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
158 end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
159 local function skipstarcomment()
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
160 next(); next(); -- skip '/', '*'
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
161 while peek and ch ~= "*" and peek ~= "/" do next(); end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
162 if not peek then error("eof in star comment") end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
163 next(); next(); -- skip '*', '/'
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
164 skipwhitespace();
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
165 end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
166 local function skipstuff()
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
167 while true do
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
168 skipwhitespace();
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
169 if ch == "/" and peek == "*" then
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
170 skipstarcomment();
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
171 elseif ch == "/" and peek == "*" then
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
172 skiplinecomment();
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
173 else
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
174 return;
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
175 end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
176 end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
177 end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
178
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
179 local readvalue;
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
180 local function readarray()
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
181 local t = {};
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
182 next(); -- skip '['
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
183 skipstuff();
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
184 if ch == "]" then next(); return t; end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
185 t_insert(t, readvalue());
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
186 while true do
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
187 skipstuff();
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
188 if ch == "]" then next(); return t; end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
189 if not ch then error("eof while reading array");
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
190 elseif ch == "," then next();
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
191 elseif ch then error("unexpected character in array, comma expected"); end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
192 if not ch then error("eof while reading array"); end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
193 t_insert(t, readvalue());
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
194 end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
195 end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
196
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
197 local function checkandskip(c)
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
198 local x = ch or "eof";
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
199 if x ~= c then error("unexpected "..x..", '"..c.."' expected"); end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
200 next();
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
201 end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
202 local function readliteral(lit, val)
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
203 for c in lit:gmatch(".") do
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
204 checkandskip(c);
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
205 end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
206 return val;
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
207 end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
208 local function readstring()
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
209 local s = "";
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
210 checkandskip("\"");
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
211 while ch do
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
212 while ch and ch ~= "\\" and ch ~= "\"" do
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
213 s = s..ch; next();
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
214 end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
215 if ch == "\\" then
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
216 next();
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
217 if unescapes[ch] then
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
218 s = s..unescapes[ch];
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
219 next();
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
220 elseif ch == "u" then
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
221 local seq = "";
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
222 for i=1,4 do
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
223 next();
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
224 if not ch then error("unexpected eof in string"); end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
225 if not ch:match("[0-9a-fA-F]") then error("invalid unicode escape sequence in string"); end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
226 seq = seq..ch;
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
227 end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
228 s = s..s.char(tonumber(seq, 16)); -- FIXME do proper utf-8
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
229 next();
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
230 else error("invalid escape sequence in string"); end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
231 end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
232 if ch == "\"" then
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
233 next();
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
234 return s;
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
235 end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
236 end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
237 error("eof while reading string");
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
238 end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
239 local function readnumber()
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
240 local s = "";
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
241 if ch == "-" then
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
242 s = s..ch; next();
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
243 if not ch:match("[0-9]") then error("number format error"); end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
244 end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
245 if ch == "0" then
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
246 s = s..ch; next();
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
247 if ch:match("[0-9]") then error("number format error"); end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
248 else
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
249 while ch and ch:match("[0-9]") do
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
250 s = s..ch; next();
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
251 end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
252 end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
253 if ch == "." then
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
254 s = s..ch; next();
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
255 if not ch:match("[0-9]") then error("number format error"); end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
256 while ch and ch:match("[0-9]") do
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
257 s = s..ch; next();
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
258 end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
259 if ch == "e" or ch == "E" then
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
260 s = s..ch; next();
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
261 if ch == "+" or ch == "-" then
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
262 s = s..ch; next();
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
263 if not ch:match("[0-9]") then error("number format error"); end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
264 while ch and ch:match("[0-9]") do
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
265 s = s..ch; next();
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
266 end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
267 end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
268 end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
269 end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
270 return tonumber(s);
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
271 end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
272 local function readmember(t)
4147
7f119ebcf55f util.json: Fixed a parse error caused by unexpected whitespace.
Waqas Hussain <waqas20@gmail.com>
parents: 4047
diff changeset
273 skipstuff();
4047
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
274 local k = readstring();
4147
7f119ebcf55f util.json: Fixed a parse error caused by unexpected whitespace.
Waqas Hussain <waqas20@gmail.com>
parents: 4047
diff changeset
275 skipstuff();
4047
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
276 checkandskip(":");
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
277 t[k] = readvalue();
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
278 end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
279 local function fixobject(obj)
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
280 local __array = obj.__array;
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
281 if __array then
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
282 obj.__array = nil;
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
283 for i,v in ipairs(__array) do
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
284 t_insert(obj, v);
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
285 end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
286 end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
287 local __hash = obj.__hash;
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
288 if __hash then
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
289 obj.__hash = nil;
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
290 local k;
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
291 for i,v in ipairs(__hash) do
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
292 if k ~= nil then
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
293 obj[k] = v; k = nil;
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
294 else
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
295 k = v;
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
296 end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
297 end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
298 end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
299 return obj;
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
300 end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
301 local function readobject()
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
302 local t = {};
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
303 next(); -- skip '{'
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
304 skipstuff();
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
305 if ch == "}" then next(); return t; end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
306 if not ch then error("eof while reading object"); end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
307 readmember(t);
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
308 while true do
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
309 skipstuff();
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
310 if ch == "}" then next(); return fixobject(t); end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
311 if not ch then error("eof while reading object");
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
312 elseif ch == "," then next();
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
313 elseif ch then error("unexpected character in object, comma expected"); end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
314 if not ch then error("eof while reading object"); end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
315 readmember(t);
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
316 end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
317 end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
318
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
319 function readvalue()
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
320 skipstuff();
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
321 while ch do
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
322 if ch == "{" then
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
323 return readobject();
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
324 elseif ch == "[" then
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
325 return readarray();
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
326 elseif ch == "\"" then
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
327 return readstring();
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
328 elseif ch:match("[%-0-9%.]") then
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
329 return readnumber();
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
330 elseif ch == "n" then
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
331 return readliteral("null", null);
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
332 elseif ch == "t" then
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
333 return readliteral("true", true);
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
334 elseif ch == "f" then
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
335 return readliteral("false", false);
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
336 else
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
337 error("invalid character at value start: "..ch);
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
338 end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
339 end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
340 error("eof while reading value");
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
341 end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
342 next();
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
343 return readvalue();
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
344 end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
345
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
346 function json.test(object)
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
347 local encoded = json.encode(object);
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
348 local decoded = json.decode(encoded);
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
349 local recoded = json.encode(decoded);
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
350 if encoded ~= recoded then
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
351 print("FAILED");
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
352 print("encoded:", encoded);
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
353 print("recoded:", recoded);
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
354 else
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
355 print(encoded);
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
356 end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
357 return encoded == recoded;
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
358 end
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
359
1a89fd3cc954 util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
360 return json;