Software /
code /
prosody
Comparison
util/json.lua @ 5516:9733836629f9
util.json: Make encode(decode("[]"))=="[]".
author | Waqas Hussain <waqas20@gmail.com> |
---|---|
date | Tue, 23 Apr 2013 14:41:52 -0400 |
parent | 5436:a4ba5819bf50 |
child | 5517:9d7349bbe4d2 |
comparison
equal
deleted
inserted
replaced
5514:1091d7c3b4d2 | 5516:9733836629f9 |
---|---|
15 local pairs, ipairs = pairs, ipairs; | 15 local pairs, ipairs = pairs, ipairs; |
16 local next = next; | 16 local next = next; |
17 local error = error; | 17 local error = error; |
18 local newproxy, getmetatable = newproxy, getmetatable; | 18 local newproxy, getmetatable = newproxy, getmetatable; |
19 local print = print; | 19 local print = print; |
20 | |
21 local has_array, array = pcall(require, "util.array"); | |
22 local array_mt = hasarray and getmetatable(array()) or {}; | |
20 | 23 |
21 --module("json") | 24 --module("json") |
22 local json = {}; | 25 local json = {}; |
23 | 26 |
24 local null = newproxy and newproxy(true) or {}; | 27 local null = newproxy and newproxy(true) or {}; |
163 if t == "number" then | 166 if t == "number" then |
164 t_insert(buffer, tostring(o)); | 167 t_insert(buffer, tostring(o)); |
165 elseif t == "string" then | 168 elseif t == "string" then |
166 stringsave(o, buffer); | 169 stringsave(o, buffer); |
167 elseif t == "table" then | 170 elseif t == "table" then |
168 tablesave(o, buffer); | 171 local mt = getmetatable(o); |
172 if mt == array_mt then | |
173 arraysave(o, buffer); | |
174 else | |
175 tablesave(o, buffer); | |
176 end | |
169 elseif t == "boolean" then | 177 elseif t == "boolean" then |
170 t_insert(buffer, (o and "true" or "false")); | 178 t_insert(buffer, (o and "true" or "false")); |
171 else | 179 else |
172 t_insert(buffer, "null"); | 180 t_insert(buffer, "null"); |
173 end | 181 end |
235 end | 243 end |
236 end | 244 end |
237 | 245 |
238 local readvalue; | 246 local readvalue; |
239 local function readarray() | 247 local function readarray() |
240 local t = {}; | 248 local t = setmetatable({}, array_mt); |
241 next(); -- skip '[' | 249 next(); -- skip '[' |
242 skipstuff(); | 250 skipstuff(); |
243 if ch == "]" then next(); return t; end | 251 if ch == "]" then next(); return t; end |
244 t_insert(t, readvalue()); | 252 t_insert(t, readvalue()); |
245 while true do | 253 while true do |