Annotate

util/json.lua @ 5458:84162b81c863

net.http, util.http: Move definitions of urlencode/decode and formencode/decode to util.http (possible to use them without unnecessary network-related dependencies)
author Matthew Wild <mwild1@gmail.com>
date Thu, 11 Apr 2013 17:32:59 +0100
parent 5436:a4ba5819bf50
child 5516:9733836629f9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5436
a4ba5819bf50 util.json: Convert \uXXXX to UTF-8 when decoding
Matthew Wild <mwild1@gmail.com>
parents: 5395
diff changeset
1 -- Prosody IM
a4ba5819bf50 util.json: Convert \uXXXX to UTF-8 when decoding
Matthew Wild <mwild1@gmail.com>
parents: 5395
diff changeset
2 -- Copyright (C) 2008-2010 Matthew Wild
a4ba5819bf50 util.json: Convert \uXXXX to UTF-8 when decoding
Matthew Wild <mwild1@gmail.com>
parents: 5395
diff changeset
3 -- Copyright (C) 2008-2010 Waqas Hussain
a4ba5819bf50 util.json: Convert \uXXXX to UTF-8 when decoding
Matthew Wild <mwild1@gmail.com>
parents: 5395
diff changeset
4 --
a4ba5819bf50 util.json: Convert \uXXXX to UTF-8 when decoding
Matthew Wild <mwild1@gmail.com>
parents: 5395
diff changeset
5 -- utf8char copyright (C) 2007 Rici Lake
a4ba5819bf50 util.json: Convert \uXXXX to UTF-8 when decoding
Matthew Wild <mwild1@gmail.com>
parents: 5395
diff changeset
6 --
a4ba5819bf50 util.json: Convert \uXXXX to UTF-8 when decoding
Matthew Wild <mwild1@gmail.com>
parents: 5395
diff changeset
7 -- This project is MIT/X11 licensed. Please see the
a4ba5819bf50 util.json: Convert \uXXXX to UTF-8 when decoding
Matthew Wild <mwild1@gmail.com>
parents: 5395
diff changeset
8 -- COPYING file in the source package for more information.
a4ba5819bf50 util.json: Convert \uXXXX to UTF-8 when decoding
Matthew Wild <mwild1@gmail.com>
parents: 5395
diff changeset
9 --
3979
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
10
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
11 local type = type;
4474
b08a46cf06e6 util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents: 4404
diff changeset
12 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
13 local s_char = string.char;
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
14 local tostring, tonumber = tostring, tonumber;
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
15 local pairs, ipairs = pairs, ipairs;
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
16 local next = next;
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
17 local error = error;
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
18 local newproxy, getmetatable = newproxy, getmetatable;
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
19 local print = print;
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 --module("json")
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
22 local json = {};
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
23
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
24 local null = newproxy and newproxy(true) or {};
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
25 if getmetatable and getmetatable(null) then
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
26 getmetatable(null).__tostring = function() return "null"; end;
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
27 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
28 json.null = null;
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
29
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
30 local escapes = {
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
31 ["\""] = "\\\"", ["\\"] = "\\\\", ["\b"] = "\\b",
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
32 ["\f"] = "\\f", ["\n"] = "\\n", ["\r"] = "\\r", ["\t"] = "\\t"};
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
33 local unescapes = {
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
34 ["\""] = "\"", ["\\"] = "\\", ["/"] = "/",
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
35 b = "\b", f = "\f", n = "\n", r = "\r", t = "\t"};
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
36 for i=0,31 do
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
37 local ch = s_char(i);
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
38 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
39 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
40
5436
a4ba5819bf50 util.json: Convert \uXXXX to UTF-8 when decoding
Matthew Wild <mwild1@gmail.com>
parents: 5395
diff changeset
41 local function utf8char(i)
a4ba5819bf50 util.json: Convert \uXXXX to UTF-8 when decoding
Matthew Wild <mwild1@gmail.com>
parents: 5395
diff changeset
42 if i >= 0 then
a4ba5819bf50 util.json: Convert \uXXXX to UTF-8 when decoding
Matthew Wild <mwild1@gmail.com>
parents: 5395
diff changeset
43 i = i - i%1
a4ba5819bf50 util.json: Convert \uXXXX to UTF-8 when decoding
Matthew Wild <mwild1@gmail.com>
parents: 5395
diff changeset
44 if i < 128 then
a4ba5819bf50 util.json: Convert \uXXXX to UTF-8 when decoding
Matthew Wild <mwild1@gmail.com>
parents: 5395
diff changeset
45 return s_char(i)
a4ba5819bf50 util.json: Convert \uXXXX to UTF-8 when decoding
Matthew Wild <mwild1@gmail.com>
parents: 5395
diff changeset
46 else
a4ba5819bf50 util.json: Convert \uXXXX to UTF-8 when decoding
Matthew Wild <mwild1@gmail.com>
parents: 5395
diff changeset
47 local c1 = i % 64
a4ba5819bf50 util.json: Convert \uXXXX to UTF-8 when decoding
Matthew Wild <mwild1@gmail.com>
parents: 5395
diff changeset
48 i = (i - c1) / 64
a4ba5819bf50 util.json: Convert \uXXXX to UTF-8 when decoding
Matthew Wild <mwild1@gmail.com>
parents: 5395
diff changeset
49 if i < 32 then
a4ba5819bf50 util.json: Convert \uXXXX to UTF-8 when decoding
Matthew Wild <mwild1@gmail.com>
parents: 5395
diff changeset
50 return s_char(0xC0+i, 0x80+c1)
a4ba5819bf50 util.json: Convert \uXXXX to UTF-8 when decoding
Matthew Wild <mwild1@gmail.com>
parents: 5395
diff changeset
51 else
a4ba5819bf50 util.json: Convert \uXXXX to UTF-8 when decoding
Matthew Wild <mwild1@gmail.com>
parents: 5395
diff changeset
52 local c2 = i % 64
a4ba5819bf50 util.json: Convert \uXXXX to UTF-8 when decoding
Matthew Wild <mwild1@gmail.com>
parents: 5395
diff changeset
53 i = (i - c2) / 64
a4ba5819bf50 util.json: Convert \uXXXX to UTF-8 when decoding
Matthew Wild <mwild1@gmail.com>
parents: 5395
diff changeset
54 if i < 16 and (i ~= 13 or c2 < 32) then
a4ba5819bf50 util.json: Convert \uXXXX to UTF-8 when decoding
Matthew Wild <mwild1@gmail.com>
parents: 5395
diff changeset
55 return s_char(0xE0+i, 0x80+c2, 0x80+c1)
a4ba5819bf50 util.json: Convert \uXXXX to UTF-8 when decoding
Matthew Wild <mwild1@gmail.com>
parents: 5395
diff changeset
56 elseif i >= 16 and i < 0x110 then
a4ba5819bf50 util.json: Convert \uXXXX to UTF-8 when decoding
Matthew Wild <mwild1@gmail.com>
parents: 5395
diff changeset
57 local c3 = i % 64
a4ba5819bf50 util.json: Convert \uXXXX to UTF-8 when decoding
Matthew Wild <mwild1@gmail.com>
parents: 5395
diff changeset
58 i = (i - c3) / 64
a4ba5819bf50 util.json: Convert \uXXXX to UTF-8 when decoding
Matthew Wild <mwild1@gmail.com>
parents: 5395
diff changeset
59 return s_char(0xF0+i, 0x80+c3, 0x80+c2, 0x80+c1)
a4ba5819bf50 util.json: Convert \uXXXX to UTF-8 when decoding
Matthew Wild <mwild1@gmail.com>
parents: 5395
diff changeset
60 end
a4ba5819bf50 util.json: Convert \uXXXX to UTF-8 when decoding
Matthew Wild <mwild1@gmail.com>
parents: 5395
diff changeset
61 end
a4ba5819bf50 util.json: Convert \uXXXX to UTF-8 when decoding
Matthew Wild <mwild1@gmail.com>
parents: 5395
diff changeset
62 end
a4ba5819bf50 util.json: Convert \uXXXX to UTF-8 when decoding
Matthew Wild <mwild1@gmail.com>
parents: 5395
diff changeset
63 end
a4ba5819bf50 util.json: Convert \uXXXX to UTF-8 when decoding
Matthew Wild <mwild1@gmail.com>
parents: 5395
diff changeset
64 end
a4ba5819bf50 util.json: Convert \uXXXX to UTF-8 when decoding
Matthew Wild <mwild1@gmail.com>
parents: 5395
diff changeset
65
a4ba5819bf50 util.json: Convert \uXXXX to UTF-8 when decoding
Matthew Wild <mwild1@gmail.com>
parents: 5395
diff changeset
66
3979
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
67 local valid_types = {
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
68 number = true,
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
69 string = true,
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
70 table = true,
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
71 boolean = true
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
72 };
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
73 local special_keys = {
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
74 __array = true;
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
75 __hash = true;
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
76 };
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
77
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
78 local simplesave, tablesave, arraysave, stringsave;
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
79
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
80 function stringsave(o, buffer)
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
81 -- FIXME do proper utf-8 and binary data detection
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
82 t_insert(buffer, "\""..(o:gsub(".", escapes)).."\"");
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
83 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
84
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
85 function arraysave(o, buffer)
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
86 t_insert(buffer, "[");
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
87 if next(o) then
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
88 for i,v in ipairs(o) do
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
89 simplesave(v, buffer);
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
90 t_insert(buffer, ",");
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
91 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
92 t_remove(buffer);
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
93 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
94 t_insert(buffer, "]");
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
95 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
96
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
97 function tablesave(o, buffer)
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
98 local __array = {};
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
99 local __hash = {};
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
100 local hash = {};
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
101 for i,v in ipairs(o) do
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
102 __array[i] = v;
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
103 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
104 for k,v in pairs(o) do
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
105 local ktype, vtype = type(k), type(v);
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
106 if valid_types[vtype] or v == null then
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
107 if ktype == "string" and not special_keys[k] then
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
108 hash[k] = v;
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
109 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
110 __hash[k] = v;
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
111 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
112 end
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(__hash) ~= nil or next(hash) ~= nil or next(__array) == nil then
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
115 t_insert(buffer, "{");
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
116 local mark = #buffer;
4474
b08a46cf06e6 util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents: 4404
diff changeset
117 if buffer.ordered then
b08a46cf06e6 util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents: 4404
diff changeset
118 local keys = {};
b08a46cf06e6 util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents: 4404
diff changeset
119 for k in pairs(hash) do
b08a46cf06e6 util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents: 4404
diff changeset
120 t_insert(keys, k);
b08a46cf06e6 util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents: 4404
diff changeset
121 end
b08a46cf06e6 util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents: 4404
diff changeset
122 t_sort(keys);
b08a46cf06e6 util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents: 4404
diff changeset
123 for _,k in ipairs(keys) do
b08a46cf06e6 util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents: 4404
diff changeset
124 stringsave(k, buffer);
b08a46cf06e6 util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents: 4404
diff changeset
125 t_insert(buffer, ":");
b08a46cf06e6 util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents: 4404
diff changeset
126 simplesave(hash[k], buffer);
b08a46cf06e6 util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents: 4404
diff changeset
127 t_insert(buffer, ",");
b08a46cf06e6 util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents: 4404
diff changeset
128 end
b08a46cf06e6 util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents: 4404
diff changeset
129 else
b08a46cf06e6 util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents: 4404
diff changeset
130 for k,v in pairs(hash) do
b08a46cf06e6 util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents: 4404
diff changeset
131 stringsave(k, buffer);
b08a46cf06e6 util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents: 4404
diff changeset
132 t_insert(buffer, ":");
b08a46cf06e6 util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents: 4404
diff changeset
133 simplesave(v, buffer);
b08a46cf06e6 util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents: 4404
diff changeset
134 t_insert(buffer, ",");
b08a46cf06e6 util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents: 4404
diff changeset
135 end
3979
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
136 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
137 if next(__hash) ~= nil then
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
138 t_insert(buffer, "\"__hash\":[");
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
139 for k,v in pairs(__hash) do
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
140 simplesave(k, buffer);
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
141 t_insert(buffer, ",");
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
142 simplesave(v, buffer);
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
143 t_insert(buffer, ",");
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
144 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
145 t_remove(buffer);
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
146 t_insert(buffer, "]");
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
147 t_insert(buffer, ",");
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
148 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
149 if next(__array) then
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
150 t_insert(buffer, "\"__array\":");
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
151 arraysave(__array, buffer);
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
152 t_insert(buffer, ",");
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
153 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
154 if mark ~= #buffer then t_remove(buffer); end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
155 t_insert(buffer, "}");
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
156 else
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
157 arraysave(__array, buffer);
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
158 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
159 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
160
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
161 function simplesave(o, buffer)
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
162 local t = type(o);
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
163 if t == "number" then
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
164 t_insert(buffer, tostring(o));
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
165 elseif t == "string" then
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
166 stringsave(o, buffer);
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
167 elseif t == "table" then
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
168 tablesave(o, buffer);
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
169 elseif t == "boolean" then
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
170 t_insert(buffer, (o and "true" or "false"));
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
171 else
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
172 t_insert(buffer, "null");
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 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
175
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
176 function json.encode(obj)
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
177 local t = {};
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
178 simplesave(obj, t);
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
179 return t_concat(t);
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
180 end
4474
b08a46cf06e6 util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents: 4404
diff changeset
181 function json.encode_ordered(obj)
b08a46cf06e6 util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents: 4404
diff changeset
182 local t = { ordered = true };
b08a46cf06e6 util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents: 4404
diff changeset
183 simplesave(obj, t);
b08a46cf06e6 util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents: 4404
diff changeset
184 return t_concat(t);
b08a46cf06e6 util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents: 4404
diff changeset
185 end
5395
ec33d72a08b6 util.json: Add json.encode_array() (thanks B)
Matthew Wild <mwild1@gmail.com>
parents: 4474
diff changeset
186 function json.encode_array(obj)
ec33d72a08b6 util.json: Add json.encode_array() (thanks B)
Matthew Wild <mwild1@gmail.com>
parents: 4474
diff changeset
187 local t = {};
ec33d72a08b6 util.json: Add json.encode_array() (thanks B)
Matthew Wild <mwild1@gmail.com>
parents: 4474
diff changeset
188 arraysave(obj, t);
ec33d72a08b6 util.json: Add json.encode_array() (thanks B)
Matthew Wild <mwild1@gmail.com>
parents: 4474
diff changeset
189 return t_concat(t);
ec33d72a08b6 util.json: Add json.encode_array() (thanks B)
Matthew Wild <mwild1@gmail.com>
parents: 4474
diff changeset
190 end
3979
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
191
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
192 -----------------------------------
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
193
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
194
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
195 function json.decode(json)
4284
20979f124ad9 util.json: Fixed handling of truncated JSON.
Waqas Hussain <waqas20@gmail.com>
parents: 4147
diff changeset
196 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
197 local pos = 1;
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
198 local current = {};
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
199 local stack = {};
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
200 local ch, peek;
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
201 local function next()
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
202 ch = json:sub(pos, pos);
4284
20979f124ad9 util.json: Fixed handling of truncated JSON.
Waqas Hussain <waqas20@gmail.com>
parents: 4147
diff changeset
203 if ch == "" then error("Unexpected EOF"); end
3979
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
204 pos = pos+1;
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
205 peek = json:sub(pos, pos);
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
206 return ch;
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
207 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
208
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
209 local function skipwhitespace()
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
210 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
211 next();
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
212 end
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 local function skiplinecomment()
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
215 repeat next(); until not(ch) or ch == "\r" or ch == "\n";
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
216 skipwhitespace();
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
217 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
218 local function skipstarcomment()
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
219 next(); next(); -- skip '/', '*'
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
220 while peek and ch ~= "*" and peek ~= "/" do next(); end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
221 if not peek then error("eof in star comment") end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
222 next(); next(); -- skip '*', '/'
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
223 skipwhitespace();
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 local function skipstuff()
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
226 while true do
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
227 skipwhitespace();
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
228 if ch == "/" and peek == "*" then
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
229 skipstarcomment();
4404
5356664ef9d4 util.json: Fix for single-line comments (thanks Norbert Kiesel)
Matthew Wild <mwild1@gmail.com>
parents: 4284
diff changeset
230 elseif ch == "/" and peek == "/" then
3979
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
231 skiplinecomment();
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
232 else
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
233 return;
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
234 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
235 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
236 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
237
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
238 local readvalue;
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
239 local function readarray()
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
240 local t = {};
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
241 next(); -- skip '['
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
242 skipstuff();
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
243 if ch == "]" then next(); return t; end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
244 t_insert(t, readvalue());
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
245 while true do
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
246 skipstuff();
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
247 if ch == "]" then next(); return t; end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
248 if not ch then error("eof while reading array");
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
249 elseif ch == "," then next();
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
250 elseif ch then error("unexpected character in array, comma expected"); end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
251 if not ch then error("eof while reading array"); end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
252 t_insert(t, readvalue());
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
253 end
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
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
256 local function checkandskip(c)
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
257 local x = ch or "eof";
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
258 if x ~= c then error("unexpected "..x..", '"..c.."' expected"); end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
259 next();
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
260 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
261 local function readliteral(lit, val)
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
262 for c in lit:gmatch(".") do
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
263 checkandskip(c);
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
264 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
265 return val;
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
266 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
267 local function readstring()
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
268 local s = "";
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
269 checkandskip("\"");
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
270 while ch do
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
271 while ch and ch ~= "\\" and ch ~= "\"" do
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
272 s = s..ch; next();
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
273 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
274 if ch == "\\" then
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
275 next();
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
276 if unescapes[ch] then
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
277 s = s..unescapes[ch];
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
278 next();
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
279 elseif ch == "u" then
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
280 local seq = "";
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
281 for i=1,4 do
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
282 next();
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
283 if not ch then error("unexpected eof in string"); end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
284 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
285 seq = seq..ch;
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
286 end
5436
a4ba5819bf50 util.json: Convert \uXXXX to UTF-8 when decoding
Matthew Wild <mwild1@gmail.com>
parents: 5395
diff changeset
287 s = s..utf8char(tonumber(seq, 16));
3979
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
288 next();
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
289 else error("invalid escape sequence in string"); end
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 if ch == "\"" then
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
292 next();
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
293 return s;
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
294 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
295 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
296 error("eof while reading string");
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 readnumber()
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
299 local s = "";
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
300 if ch == "-" then
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
301 s = s..ch; next();
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
302 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
303 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
304 if ch == "0" then
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
305 s = s..ch; next();
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
306 if ch:match("[0-9]") then error("number format error"); end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
307 else
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
308 while ch and ch:match("[0-9]") do
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
309 s = s..ch; next();
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
310 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
311 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
312 if ch == "." then
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
313 s = s..ch; next();
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
314 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
315 while ch and ch:match("[0-9]") do
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
316 s = s..ch; next();
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 if ch == "e" or ch == "E" then
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
319 s = s..ch; next();
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
320 if ch == "+" or ch == "-" then
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
321 s = s..ch; next();
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
322 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
323 while ch and ch:match("[0-9]") do
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
324 s = s..ch; next();
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
325 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
326 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
327 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
328 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
329 return tonumber(s);
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
330 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
331 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
332 skipstuff();
3979
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
333 local k = readstring();
4147
7f119ebcf55f util.json: Fixed a parse error caused by unexpected whitespace.
Waqas Hussain <waqas20@gmail.com>
parents: 3979
diff changeset
334 skipstuff();
3979
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
335 checkandskip(":");
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
336 t[k] = readvalue();
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
337 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
338 local function fixobject(obj)
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
339 local __array = obj.__array;
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
340 if __array then
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
341 obj.__array = nil;
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
342 for i,v in ipairs(__array) do
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
343 t_insert(obj, v);
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
344 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
345 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
346 local __hash = obj.__hash;
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
347 if __hash then
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
348 obj.__hash = nil;
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
349 local k;
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
350 for i,v in ipairs(__hash) do
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
351 if k ~= nil then
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
352 obj[k] = v; k = nil;
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
353 else
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
354 k = v;
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
355 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
356 end
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 return obj;
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
359 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
360 local function readobject()
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
361 local t = {};
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
362 next(); -- skip '{'
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
363 skipstuff();
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
364 if ch == "}" then next(); return t; end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
365 if not ch then error("eof while reading object"); end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
366 readmember(t);
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
367 while true do
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
368 skipstuff();
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
369 if ch == "}" then next(); return fixobject(t); end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
370 if not ch then error("eof while reading object");
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
371 elseif ch == "," then next();
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
372 elseif ch then error("unexpected character in object, comma expected"); end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
373 if not ch then error("eof while reading object"); end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
374 readmember(t);
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 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
377
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
378 function readvalue()
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
379 skipstuff();
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
380 while ch do
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
381 if ch == "{" then
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
382 return readobject();
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
383 elseif ch == "[" then
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
384 return readarray();
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
385 elseif ch == "\"" then
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
386 return readstring();
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
387 elseif ch:match("[%-0-9%.]") then
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
388 return readnumber();
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
389 elseif ch == "n" then
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
390 return readliteral("null", null);
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
391 elseif ch == "t" then
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
392 return readliteral("true", true);
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
393 elseif ch == "f" then
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
394 return readliteral("false", false);
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
395 else
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
396 error("invalid character at value start: "..ch);
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
397 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
398 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
399 error("eof while reading value");
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
400 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
401 next();
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
402 return readvalue();
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
403 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
404
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
405 function json.test(object)
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
406 local encoded = json.encode(object);
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
407 local decoded = json.decode(encoded);
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
408 local recoded = json.encode(decoded);
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
409 if encoded ~= recoded then
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
410 print("FAILED");
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
411 print("encoded:", encoded);
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
412 print("recoded:", recoded);
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
413 else
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
414 print(encoded);
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
415 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
416 return encoded == recoded;
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
417 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
418
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
419 return json;