Software /
code /
prosody
Annotate
util/json.lua @ 7235:ee1f7e1e548c
util.json: Fix encoding of json.null (bug introduced in bf1f09a5bcf7)
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Fri, 04 Mar 2016 22:27:04 +0000 (2016-03-04) |
parent | 6785:bf1f09a5bcf7 |
child | 7259:d8300985f2bb |
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 -- 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
|
6 -- 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
|
7 -- |
3979 | 8 |
9 local type = type; | |
4474
b08a46cf06e6
util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents:
4404
diff
changeset
|
10 local t_insert, t_concat, t_remove, t_sort = table.insert, table.concat, table.remove, table.sort; |
3979 | 11 local s_char = string.char; |
12 local tostring, tonumber = tostring, tonumber; | |
13 local pairs, ipairs = pairs, ipairs; | |
14 local next = next; | |
15 local error = error; | |
6785
bf1f09a5bcf7
util.json: Remove use of newproxy
Kim Alvefur <zash@zash.se>
parents:
5776
diff
changeset
|
16 local getmetatable, setmetatable = getmetatable, setmetatable; |
3979 | 17 local print = print; |
18 | |
5516
9733836629f9
util.json: Make encode(decode("[]"))=="[]".
Waqas Hussain <waqas20@gmail.com>
parents:
5436
diff
changeset
|
19 local has_array, array = pcall(require, "util.array"); |
5562
a6b8fb827e2a
util.json: Fix variable name typo which broke util.json when util.array was missing.
Waqas Hussain <waqas20@gmail.com>
parents:
5561
diff
changeset
|
20 local array_mt = has_array and getmetatable(array()) or {}; |
5516
9733836629f9
util.json: Make encode(decode("[]"))=="[]".
Waqas Hussain <waqas20@gmail.com>
parents:
5436
diff
changeset
|
21 |
3979 | 22 --module("json") |
23 local json = {}; | |
24 | |
6785
bf1f09a5bcf7
util.json: Remove use of newproxy
Kim Alvefur <zash@zash.se>
parents:
5776
diff
changeset
|
25 local null = setmetatable({}, { __tostring = function() return "null"; end; }); |
3979 | 26 json.null = null; |
27 | |
28 local escapes = { | |
29 ["\""] = "\\\"", ["\\"] = "\\\\", ["\b"] = "\\b", | |
30 ["\f"] = "\\f", ["\n"] = "\\n", ["\r"] = "\\r", ["\t"] = "\\t"}; | |
31 local unescapes = { | |
32 ["\""] = "\"", ["\\"] = "\\", ["/"] = "/", | |
33 b = "\b", f = "\f", n = "\n", r = "\r", t = "\t"}; | |
34 for i=0,31 do | |
35 local ch = s_char(i); | |
36 if not escapes[ch] then escapes[ch] = ("\\u%.4X"):format(i); end | |
37 end | |
38 | |
5517
9d7349bbe4d2
util.json: New, improved, fixed codepoint to UTF-8 conversion.
Waqas Hussain <waqas20@gmail.com>
parents:
5516
diff
changeset
|
39 local function codepoint_to_utf8(code) |
9d7349bbe4d2
util.json: New, improved, fixed codepoint to UTF-8 conversion.
Waqas Hussain <waqas20@gmail.com>
parents:
5516
diff
changeset
|
40 if code < 0x80 then return s_char(code); end |
9d7349bbe4d2
util.json: New, improved, fixed codepoint to UTF-8 conversion.
Waqas Hussain <waqas20@gmail.com>
parents:
5516
diff
changeset
|
41 local bits0_6 = code % 64; |
9d7349bbe4d2
util.json: New, improved, fixed codepoint to UTF-8 conversion.
Waqas Hussain <waqas20@gmail.com>
parents:
5516
diff
changeset
|
42 if code < 0x800 then |
9d7349bbe4d2
util.json: New, improved, fixed codepoint to UTF-8 conversion.
Waqas Hussain <waqas20@gmail.com>
parents:
5516
diff
changeset
|
43 local bits6_5 = (code - bits0_6) / 64; |
9d7349bbe4d2
util.json: New, improved, fixed codepoint to UTF-8 conversion.
Waqas Hussain <waqas20@gmail.com>
parents:
5516
diff
changeset
|
44 return s_char(0x80 + 0x40 + bits6_5, 0x80 + bits0_6); |
5436
a4ba5819bf50
util.json: Convert \uXXXX to UTF-8 when decoding
Matthew Wild <mwild1@gmail.com>
parents:
5395
diff
changeset
|
45 end |
5517
9d7349bbe4d2
util.json: New, improved, fixed codepoint to UTF-8 conversion.
Waqas Hussain <waqas20@gmail.com>
parents:
5516
diff
changeset
|
46 local bits0_12 = code % 4096; |
9d7349bbe4d2
util.json: New, improved, fixed codepoint to UTF-8 conversion.
Waqas Hussain <waqas20@gmail.com>
parents:
5516
diff
changeset
|
47 local bits6_6 = (bits0_12 - bits0_6) / 64; |
9d7349bbe4d2
util.json: New, improved, fixed codepoint to UTF-8 conversion.
Waqas Hussain <waqas20@gmail.com>
parents:
5516
diff
changeset
|
48 local bits12_4 = (code - bits0_12) / 4096; |
9d7349bbe4d2
util.json: New, improved, fixed codepoint to UTF-8 conversion.
Waqas Hussain <waqas20@gmail.com>
parents:
5516
diff
changeset
|
49 return s_char(0x80 + 0x40 + 0x20 + bits12_4, 0x80 + bits6_6, 0x80 + bits0_6); |
5436
a4ba5819bf50
util.json: Convert \uXXXX to UTF-8 when decoding
Matthew Wild <mwild1@gmail.com>
parents:
5395
diff
changeset
|
50 end |
a4ba5819bf50
util.json: Convert \uXXXX to UTF-8 when decoding
Matthew Wild <mwild1@gmail.com>
parents:
5395
diff
changeset
|
51 |
3979 | 52 local valid_types = { |
53 number = true, | |
54 string = true, | |
55 table = true, | |
56 boolean = true | |
57 }; | |
58 local special_keys = { | |
59 __array = true; | |
60 __hash = true; | |
61 }; | |
62 | |
63 local simplesave, tablesave, arraysave, stringsave; | |
64 | |
65 function stringsave(o, buffer) | |
66 -- FIXME do proper utf-8 and binary data detection | |
67 t_insert(buffer, "\""..(o:gsub(".", escapes)).."\""); | |
68 end | |
69 | |
70 function arraysave(o, buffer) | |
71 t_insert(buffer, "["); | |
72 if next(o) then | |
73 for i,v in ipairs(o) do | |
74 simplesave(v, buffer); | |
75 t_insert(buffer, ","); | |
76 end | |
77 t_remove(buffer); | |
78 end | |
79 t_insert(buffer, "]"); | |
80 end | |
81 | |
82 function tablesave(o, buffer) | |
83 local __array = {}; | |
84 local __hash = {}; | |
85 local hash = {}; | |
86 for i,v in ipairs(o) do | |
87 __array[i] = v; | |
88 end | |
89 for k,v in pairs(o) do | |
90 local ktype, vtype = type(k), type(v); | |
91 if valid_types[vtype] or v == null then | |
92 if ktype == "string" and not special_keys[k] then | |
93 hash[k] = v; | |
94 elseif (valid_types[ktype] or k == null) and __array[k] == nil then | |
95 __hash[k] = v; | |
96 end | |
97 end | |
98 end | |
99 if next(__hash) ~= nil or next(hash) ~= nil or next(__array) == nil then | |
100 t_insert(buffer, "{"); | |
101 local mark = #buffer; | |
4474
b08a46cf06e6
util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents:
4404
diff
changeset
|
102 if buffer.ordered then |
b08a46cf06e6
util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents:
4404
diff
changeset
|
103 local keys = {}; |
b08a46cf06e6
util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents:
4404
diff
changeset
|
104 for k in pairs(hash) do |
b08a46cf06e6
util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents:
4404
diff
changeset
|
105 t_insert(keys, k); |
b08a46cf06e6
util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents:
4404
diff
changeset
|
106 end |
b08a46cf06e6
util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents:
4404
diff
changeset
|
107 t_sort(keys); |
b08a46cf06e6
util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents:
4404
diff
changeset
|
108 for _,k in ipairs(keys) do |
b08a46cf06e6
util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents:
4404
diff
changeset
|
109 stringsave(k, buffer); |
b08a46cf06e6
util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents:
4404
diff
changeset
|
110 t_insert(buffer, ":"); |
b08a46cf06e6
util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents:
4404
diff
changeset
|
111 simplesave(hash[k], buffer); |
b08a46cf06e6
util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents:
4404
diff
changeset
|
112 t_insert(buffer, ","); |
b08a46cf06e6
util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents:
4404
diff
changeset
|
113 end |
b08a46cf06e6
util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents:
4404
diff
changeset
|
114 else |
b08a46cf06e6
util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents:
4404
diff
changeset
|
115 for k,v in pairs(hash) do |
b08a46cf06e6
util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents:
4404
diff
changeset
|
116 stringsave(k, buffer); |
b08a46cf06e6
util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents:
4404
diff
changeset
|
117 t_insert(buffer, ":"); |
b08a46cf06e6
util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents:
4404
diff
changeset
|
118 simplesave(v, buffer); |
b08a46cf06e6
util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents:
4404
diff
changeset
|
119 t_insert(buffer, ","); |
b08a46cf06e6
util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents:
4404
diff
changeset
|
120 end |
3979 | 121 end |
122 if next(__hash) ~= nil then | |
123 t_insert(buffer, "\"__hash\":["); | |
124 for k,v in pairs(__hash) do | |
125 simplesave(k, buffer); | |
126 t_insert(buffer, ","); | |
127 simplesave(v, buffer); | |
128 t_insert(buffer, ","); | |
129 end | |
130 t_remove(buffer); | |
131 t_insert(buffer, "]"); | |
132 t_insert(buffer, ","); | |
133 end | |
134 if next(__array) then | |
135 t_insert(buffer, "\"__array\":"); | |
136 arraysave(__array, buffer); | |
137 t_insert(buffer, ","); | |
138 end | |
139 if mark ~= #buffer then t_remove(buffer); end | |
140 t_insert(buffer, "}"); | |
141 else | |
142 arraysave(__array, buffer); | |
143 end | |
144 end | |
145 | |
146 function simplesave(o, buffer) | |
147 local t = type(o); | |
7235
ee1f7e1e548c
util.json: Fix encoding of json.null (bug introduced in bf1f09a5bcf7)
Matthew Wild <mwild1@gmail.com>
parents:
6785
diff
changeset
|
148 if o == null then |
ee1f7e1e548c
util.json: Fix encoding of json.null (bug introduced in bf1f09a5bcf7)
Matthew Wild <mwild1@gmail.com>
parents:
6785
diff
changeset
|
149 t_insert(buffer, "null"); |
ee1f7e1e548c
util.json: Fix encoding of json.null (bug introduced in bf1f09a5bcf7)
Matthew Wild <mwild1@gmail.com>
parents:
6785
diff
changeset
|
150 elseif t == "number" then |
3979 | 151 t_insert(buffer, tostring(o)); |
152 elseif t == "string" then | |
153 stringsave(o, buffer); | |
154 elseif t == "table" then | |
5516
9733836629f9
util.json: Make encode(decode("[]"))=="[]".
Waqas Hussain <waqas20@gmail.com>
parents:
5436
diff
changeset
|
155 local mt = getmetatable(o); |
9733836629f9
util.json: Make encode(decode("[]"))=="[]".
Waqas Hussain <waqas20@gmail.com>
parents:
5436
diff
changeset
|
156 if mt == array_mt then |
9733836629f9
util.json: Make encode(decode("[]"))=="[]".
Waqas Hussain <waqas20@gmail.com>
parents:
5436
diff
changeset
|
157 arraysave(o, buffer); |
9733836629f9
util.json: Make encode(decode("[]"))=="[]".
Waqas Hussain <waqas20@gmail.com>
parents:
5436
diff
changeset
|
158 else |
9733836629f9
util.json: Make encode(decode("[]"))=="[]".
Waqas Hussain <waqas20@gmail.com>
parents:
5436
diff
changeset
|
159 tablesave(o, buffer); |
9733836629f9
util.json: Make encode(decode("[]"))=="[]".
Waqas Hussain <waqas20@gmail.com>
parents:
5436
diff
changeset
|
160 end |
3979 | 161 elseif t == "boolean" then |
162 t_insert(buffer, (o and "true" or "false")); | |
163 else | |
164 t_insert(buffer, "null"); | |
165 end | |
166 end | |
167 | |
168 function json.encode(obj) | |
169 local t = {}; | |
170 simplesave(obj, t); | |
171 return t_concat(t); | |
172 end | |
4474
b08a46cf06e6
util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents:
4404
diff
changeset
|
173 function json.encode_ordered(obj) |
b08a46cf06e6
util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents:
4404
diff
changeset
|
174 local t = { ordered = true }; |
b08a46cf06e6
util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents:
4404
diff
changeset
|
175 simplesave(obj, t); |
b08a46cf06e6
util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents:
4404
diff
changeset
|
176 return t_concat(t); |
b08a46cf06e6
util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents:
4404
diff
changeset
|
177 end |
5395
ec33d72a08b6
util.json: Add json.encode_array() (thanks B)
Matthew Wild <mwild1@gmail.com>
parents:
4474
diff
changeset
|
178 function json.encode_array(obj) |
ec33d72a08b6
util.json: Add json.encode_array() (thanks B)
Matthew Wild <mwild1@gmail.com>
parents:
4474
diff
changeset
|
179 local t = {}; |
ec33d72a08b6
util.json: Add json.encode_array() (thanks B)
Matthew Wild <mwild1@gmail.com>
parents:
4474
diff
changeset
|
180 arraysave(obj, t); |
ec33d72a08b6
util.json: Add json.encode_array() (thanks B)
Matthew Wild <mwild1@gmail.com>
parents:
4474
diff
changeset
|
181 return t_concat(t); |
ec33d72a08b6
util.json: Add json.encode_array() (thanks B)
Matthew Wild <mwild1@gmail.com>
parents:
4474
diff
changeset
|
182 end |
3979 | 183 |
184 ----------------------------------- | |
185 | |
186 | |
5565
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
187 local function _skip_whitespace(json, index) |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
188 return json:find("[^ \t\r\n]", index) or index; -- no need to check \r\n, we converted those to \t |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
189 end |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
190 local function _fixobject(obj) |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
191 local __array = obj.__array; |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
192 if __array then |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
193 obj.__array = nil; |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
194 for i,v in ipairs(__array) do |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
195 t_insert(obj, v); |
3979 | 196 end |
197 end | |
5565
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
198 local __hash = obj.__hash; |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
199 if __hash then |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
200 obj.__hash = nil; |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
201 local k; |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
202 for i,v in ipairs(__hash) do |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
203 if k ~= nil then |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
204 obj[k] = v; k = nil; |
3979 | 205 else |
5565
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
206 k = v; |
3979 | 207 end |
208 end | |
209 end | |
5565
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
210 return obj; |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
211 end |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
212 local _readvalue, _readstring; |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
213 local function _readobject(json, index) |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
214 local o = {}; |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
215 while true do |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
216 local key, val; |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
217 index = _skip_whitespace(json, index + 1); |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
218 if json:byte(index) ~= 0x22 then -- "\"" |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
219 if json:byte(index) == 0x7d then return o, index + 1; end -- "}" |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
220 return nil, "key expected"; |
3979 | 221 end |
5565
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
222 key, index = _readstring(json, index); |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
223 if key == nil then return nil, index; end |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
224 index = _skip_whitespace(json, index); |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
225 if json:byte(index) ~= 0x3a then return nil, "colon expected"; end -- ":" |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
226 val, index = _readvalue(json, index + 1); |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
227 if val == nil then return nil, index; end |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
228 o[key] = val; |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
229 index = _skip_whitespace(json, index); |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
230 local b = json:byte(index); |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
231 if b == 0x7d then return _fixobject(o), index + 1; end -- "}" |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
232 if b ~= 0x2c then return nil, "object eof"; end -- "," |
3979 | 233 end |
5565
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
234 end |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
235 local function _readarray(json, index) |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
236 local a = {}; |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
237 local oindex = index; |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
238 while true do |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
239 local val; |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
240 val, index = _readvalue(json, index + 1); |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
241 if val == nil then |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
242 if json:byte(oindex + 1) == 0x5d then return setmetatable(a, array_mt), oindex + 2; end -- "]" |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
243 return val, index; |
3979 | 244 end |
5565
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
245 t_insert(a, val); |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
246 index = _skip_whitespace(json, index); |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
247 local b = json:byte(index); |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
248 if b == 0x5d then return setmetatable(a, array_mt), index + 1; end -- "]" |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
249 if b ~= 0x2c then return nil, "array eof"; end -- "," |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
250 end |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
251 end |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
252 local _unescape_error; |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
253 local function _unescape_surrogate_func(x) |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
254 local lead, trail = tonumber(x:sub(3, 6), 16), tonumber(x:sub(9, 12), 16); |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
255 local codepoint = lead * 0x400 + trail - 0x35FDC00; |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
256 local a = codepoint % 64; |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
257 codepoint = (codepoint - a) / 64; |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
258 local b = codepoint % 64; |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
259 codepoint = (codepoint - b) / 64; |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
260 local c = codepoint % 64; |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
261 codepoint = (codepoint - c) / 64; |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
262 return s_char(0xF0 + codepoint, 0x80 + c, 0x80 + b, 0x80 + a); |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
263 end |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
264 local function _unescape_func(x) |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
265 x = x:match("%x%x%x%x", 3); |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
266 if x then |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
267 --if x >= 0xD800 and x <= 0xDFFF then _unescape_error = true; end -- bad surrogate pair |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
268 return codepoint_to_utf8(tonumber(x, 16)); |
3979 | 269 end |
5565
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
270 _unescape_error = true; |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
271 end |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
272 function _readstring(json, index) |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
273 index = index + 1; |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
274 local endindex = json:find("\"", index, true); |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
275 if endindex then |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
276 local s = json:sub(index, endindex - 1); |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
277 --if s:find("[%z-\31]") then return nil, "control char in string"; end |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
278 -- FIXME handle control characters |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
279 _unescape_error = nil; |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
280 --s = s:gsub("\\u[dD][89abAB]%x%x\\u[dD][cdefCDEF]%x%x", _unescape_surrogate_func); |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
281 -- FIXME handle escapes beyond BMP |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
282 s = s:gsub("\\u.?.?.?.?", _unescape_func); |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
283 if _unescape_error then return nil, "invalid escape"; end |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
284 return s, endindex + 1; |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
285 end |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
286 return nil, "string eof"; |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
287 end |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
288 local function _readnumber(json, index) |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
289 local m = json:match("[0-9%.%-eE%+]+", index); -- FIXME do strict checking |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
290 return tonumber(m), index + #m; |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
291 end |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
292 local function _readnull(json, index) |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
293 local a, b, c = json:byte(index + 1, index + 3); |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
294 if a == 0x75 and b == 0x6c and c == 0x6c then |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
295 return null, index + 4; |
3979 | 296 end |
5565
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
297 return nil, "null parse failed"; |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
298 end |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
299 local function _readtrue(json, index) |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
300 local a, b, c = json:byte(index + 1, index + 3); |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
301 if a == 0x72 and b == 0x75 and c == 0x65 then |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
302 return true, index + 4; |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
303 end |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
304 return nil, "true parse failed"; |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
305 end |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
306 local function _readfalse(json, index) |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
307 local a, b, c, d = json:byte(index + 1, index + 4); |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
308 if a == 0x61 and b == 0x6c and c == 0x73 and d == 0x65 then |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
309 return false, index + 5; |
3979 | 310 end |
5565
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
311 return nil, "false parse failed"; |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
312 end |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
313 function _readvalue(json, index) |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
314 index = _skip_whitespace(json, index); |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
315 local b = json:byte(index); |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
316 -- TODO try table lookup instead of if-else? |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
317 if b == 0x7B then -- "{" |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
318 return _readobject(json, index); |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
319 elseif b == 0x5B then -- "[" |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
320 return _readarray(json, index); |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
321 elseif b == 0x22 then -- "\"" |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
322 return _readstring(json, index); |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
323 elseif b ~= nil and b >= 0x30 and b <= 0x39 or b == 0x2d then -- "0"-"9" or "-" |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
324 return _readnumber(json, index); |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
325 elseif b == 0x6e then -- "n" |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
326 return _readnull(json, index); |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
327 elseif b == 0x74 then -- "t" |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
328 return _readtrue(json, index); |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
329 elseif b == 0x66 then -- "f" |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
330 return _readfalse(json, index); |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
331 else |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
332 return nil, "value expected"; |
3979 | 333 end |
5565
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
334 end |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
335 local first_escape = { |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
336 ["\\\""] = "\\u0022"; |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
337 ["\\\\"] = "\\u005c"; |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
338 ["\\/" ] = "\\u002f"; |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
339 ["\\b" ] = "\\u0008"; |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
340 ["\\f" ] = "\\u000C"; |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
341 ["\\n" ] = "\\u000A"; |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
342 ["\\r" ] = "\\u000D"; |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
343 ["\\t" ] = "\\u0009"; |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
344 ["\\u" ] = "\\u"; |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
345 }; |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
346 |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
347 function json.decode(json) |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
348 json = json:gsub("\\.", first_escape) -- get rid of all escapes except \uXXXX, making string parsing much simpler |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
349 --:gsub("[\r\n]", "\t"); -- \r\n\t are equivalent, we care about none of them, and none of them can be in strings |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5565
diff
changeset
|
350 |
5565
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
351 -- TODO do encoding verification |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5565
diff
changeset
|
352 |
5565
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
353 local val, index = _readvalue(json, 1); |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
354 if val == nil then return val, index; end |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
355 if json:find("[^ \t\r\n]", index) then return nil, "garbage at eof"; end |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
356 |
6dd806829226
util.json: New, faster, stricter, more compliant JSON decoder. Now returns nil,err instead of throwing errors on invalid input.
Waqas Hussain <waqas20@gmail.com>
parents:
5563
diff
changeset
|
357 return val; |
3979 | 358 end |
359 | |
360 function json.test(object) | |
361 local encoded = json.encode(object); | |
362 local decoded = json.decode(encoded); | |
363 local recoded = json.encode(decoded); | |
364 if encoded ~= recoded then | |
365 print("FAILED"); | |
366 print("encoded:", encoded); | |
367 print("recoded:", recoded); | |
368 else | |
369 print(encoded); | |
370 end | |
371 return encoded == recoded; | |
372 end | |
373 | |
374 return json; |