Software / code / prosody
Comparison
util/json.lua @ 7261:925f848c706d
util.json: Variable renaming to avoid shadowing [luacheck]
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Thu, 10 Mar 2016 17:53:17 +0000 |
| parent | 7259:d8300985f2bb |
| child | 8382:e5d00bf4a4d5 |
comparison
equal
deleted
inserted
replaced
| 7260:a9ef93bc81d9 | 7261:925f848c706d |
|---|---|
| 17 | 17 |
| 18 local has_array, array = pcall(require, "util.array"); | 18 local has_array, array = pcall(require, "util.array"); |
| 19 local array_mt = has_array and getmetatable(array()) or {}; | 19 local array_mt = has_array and getmetatable(array()) or {}; |
| 20 | 20 |
| 21 --module("json") | 21 --module("json") |
| 22 local json = {}; | 22 local module = {}; |
| 23 | 23 |
| 24 local null = setmetatable({}, { __tostring = function() return "null"; end; }); | 24 local null = setmetatable({}, { __tostring = function() return "null"; end; }); |
| 25 json.null = null; | 25 module.null = null; |
| 26 | 26 |
| 27 local escapes = { | 27 local escapes = { |
| 28 ["\""] = "\\\"", ["\\"] = "\\\\", ["\b"] = "\\b", | 28 ["\""] = "\\\"", ["\\"] = "\\\\", ["\b"] = "\\b", |
| 29 ["\f"] = "\\f", ["\n"] = "\\n", ["\r"] = "\\r", ["\t"] = "\\t"}; | 29 ["\f"] = "\\f", ["\n"] = "\\n", ["\r"] = "\\r", ["\t"] = "\\t"}; |
| 30 local unescapes = { | 30 local unescapes = { |
| 67 end | 67 end |
| 68 | 68 |
| 69 function arraysave(o, buffer) | 69 function arraysave(o, buffer) |
| 70 t_insert(buffer, "["); | 70 t_insert(buffer, "["); |
| 71 if next(o) then | 71 if next(o) then |
| 72 for i,v in ipairs(o) do | 72 for _, v in ipairs(o) do |
| 73 simplesave(v, buffer); | 73 simplesave(v, buffer); |
| 74 t_insert(buffer, ","); | 74 t_insert(buffer, ","); |
| 75 end | 75 end |
| 76 t_remove(buffer); | 76 t_remove(buffer); |
| 77 end | 77 end |
| 162 else | 162 else |
| 163 t_insert(buffer, "null"); | 163 t_insert(buffer, "null"); |
| 164 end | 164 end |
| 165 end | 165 end |
| 166 | 166 |
| 167 function json.encode(obj) | 167 function module.encode(obj) |
| 168 local t = {}; | 168 local t = {}; |
| 169 simplesave(obj, t); | 169 simplesave(obj, t); |
| 170 return t_concat(t); | 170 return t_concat(t); |
| 171 end | 171 end |
| 172 function json.encode_ordered(obj) | 172 function module.encode_ordered(obj) |
| 173 local t = { ordered = true }; | 173 local t = { ordered = true }; |
| 174 simplesave(obj, t); | 174 simplesave(obj, t); |
| 175 return t_concat(t); | 175 return t_concat(t); |
| 176 end | 176 end |
| 177 function json.encode_array(obj) | 177 function module.encode_array(obj) |
| 178 local t = {}; | 178 local t = {}; |
| 179 arraysave(obj, t); | 179 arraysave(obj, t); |
| 180 return t_concat(t); | 180 return t_concat(t); |
| 181 end | 181 end |
| 182 | 182 |
| 188 end | 188 end |
| 189 local function _fixobject(obj) | 189 local function _fixobject(obj) |
| 190 local __array = obj.__array; | 190 local __array = obj.__array; |
| 191 if __array then | 191 if __array then |
| 192 obj.__array = nil; | 192 obj.__array = nil; |
| 193 for i,v in ipairs(__array) do | 193 for _, v in ipairs(__array) do |
| 194 t_insert(obj, v); | 194 t_insert(obj, v); |
| 195 end | 195 end |
| 196 end | 196 end |
| 197 local __hash = obj.__hash; | 197 local __hash = obj.__hash; |
| 198 if __hash then | 198 if __hash then |
| 199 obj.__hash = nil; | 199 obj.__hash = nil; |
| 200 local k; | 200 local k; |
| 201 for i,v in ipairs(__hash) do | 201 for _, v in ipairs(__hash) do |
| 202 if k ~= nil then | 202 if k ~= nil then |
| 203 obj[k] = v; k = nil; | 203 obj[k] = v; k = nil; |
| 204 else | 204 else |
| 205 k = v; | 205 k = v; |
| 206 end | 206 end |
| 341 ["\\r" ] = "\\u000D"; | 341 ["\\r" ] = "\\u000D"; |
| 342 ["\\t" ] = "\\u0009"; | 342 ["\\t" ] = "\\u0009"; |
| 343 ["\\u" ] = "\\u"; | 343 ["\\u" ] = "\\u"; |
| 344 }; | 344 }; |
| 345 | 345 |
| 346 function json.decode(json) | 346 function module.decode(json) |
| 347 json = json:gsub("\\.", first_escape) -- get rid of all escapes except \uXXXX, making string parsing much simpler | 347 json = json:gsub("\\.", first_escape) -- get rid of all escapes except \uXXXX, making string parsing much simpler |
| 348 --:gsub("[\r\n]", "\t"); -- \r\n\t are equivalent, we care about none of them, and none of them can be in strings | 348 --:gsub("[\r\n]", "\t"); -- \r\n\t are equivalent, we care about none of them, and none of them can be in strings |
| 349 | 349 |
| 350 -- TODO do encoding verification | 350 -- TODO do encoding verification |
| 351 | 351 |
| 354 if json:find("[^ \t\r\n]", index) then return nil, "garbage at eof"; end | 354 if json:find("[^ \t\r\n]", index) then return nil, "garbage at eof"; end |
| 355 | 355 |
| 356 return val; | 356 return val; |
| 357 end | 357 end |
| 358 | 358 |
| 359 function json.test(object) | 359 function module.test(object) |
| 360 local encoded = json.encode(object); | 360 local encoded = module.encode(object); |
| 361 local decoded = json.decode(encoded); | 361 local decoded = module.decode(encoded); |
| 362 local recoded = json.encode(decoded); | 362 local recoded = module.encode(decoded); |
| 363 if encoded ~= recoded then | 363 if encoded ~= recoded then |
| 364 print("FAILED"); | 364 print("FAILED"); |
| 365 print("encoded:", encoded); | 365 print("encoded:", encoded); |
| 366 print("recoded:", recoded); | 366 print("recoded:", recoded); |
| 367 else | 367 else |
| 368 print(encoded); | 368 print(encoded); |
| 369 end | 369 end |
| 370 return encoded == recoded; | 370 return encoded == recoded; |
| 371 end | 371 end |
| 372 | 372 |
| 373 return json; | 373 return module; |