# HG changeset patch # User Matthew Wild # Date 1457632397 0 # Node ID 925f848c706d590af09a1b0327b31450cf532230 # Parent a9ef93bc81d903b34eee4b7b532af7de33fb7efc util.json: Variable renaming to avoid shadowing [luacheck] diff -r a9ef93bc81d9 -r 925f848c706d util/json.lua --- a/util/json.lua Thu Mar 10 17:52:57 2016 +0000 +++ b/util/json.lua Thu Mar 10 17:53:17 2016 +0000 @@ -19,10 +19,10 @@ local array_mt = has_array and getmetatable(array()) or {}; --module("json") -local json = {}; +local module = {}; local null = setmetatable({}, { __tostring = function() return "null"; end; }); -json.null = null; +module.null = null; local escapes = { ["\""] = "\\\"", ["\\"] = "\\\\", ["\b"] = "\\b", @@ -69,7 +69,7 @@ function arraysave(o, buffer) t_insert(buffer, "["); if next(o) then - for i,v in ipairs(o) do + for _, v in ipairs(o) do simplesave(v, buffer); t_insert(buffer, ","); end @@ -164,17 +164,17 @@ end end -function json.encode(obj) +function module.encode(obj) local t = {}; simplesave(obj, t); return t_concat(t); end -function json.encode_ordered(obj) +function module.encode_ordered(obj) local t = { ordered = true }; simplesave(obj, t); return t_concat(t); end -function json.encode_array(obj) +function module.encode_array(obj) local t = {}; arraysave(obj, t); return t_concat(t); @@ -190,7 +190,7 @@ local __array = obj.__array; if __array then obj.__array = nil; - for i,v in ipairs(__array) do + for _, v in ipairs(__array) do t_insert(obj, v); end end @@ -198,7 +198,7 @@ if __hash then obj.__hash = nil; local k; - for i,v in ipairs(__hash) do + for _, v in ipairs(__hash) do if k ~= nil then obj[k] = v; k = nil; else @@ -343,7 +343,7 @@ ["\\u" ] = "\\u"; }; -function json.decode(json) +function module.decode(json) json = json:gsub("\\.", first_escape) -- get rid of all escapes except \uXXXX, making string parsing much simpler --:gsub("[\r\n]", "\t"); -- \r\n\t are equivalent, we care about none of them, and none of them can be in strings @@ -356,10 +356,10 @@ return val; end -function json.test(object) - local encoded = json.encode(object); - local decoded = json.decode(encoded); - local recoded = json.encode(decoded); +function module.test(object) + local encoded = module.encode(object); + local decoded = module.decode(encoded); + local recoded = module.encode(decoded); if encoded ~= recoded then print("FAILED"); print("encoded:", encoded); @@ -370,4 +370,4 @@ return encoded == recoded; end -return json; +return module;