Annotate

util/json.lua @ 4617:d8e56e6ac8d4

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