/usr/share/lua/5.1/json/util.lua is in lua-json 1.3.3-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | --[[
Licensed according to the included 'LICENSE' document
Author: Thomas Harning Jr <harningt@gmail.com>
]]
local type = type
local print = print
local tostring = tostring
local pairs = pairs
local getmetatable, setmetatable = getmetatable, setmetatable
local select = select
local _ENV = nil
local function foreach(tab, func)
for k, v in pairs(tab) do
func(k,v)
end
end
local function printValue(tab, name)
local parsed = {}
local function doPrint(key, value, space)
space = space or ''
if type(value) == 'table' then
if parsed[value] then
print(space .. key .. '= <' .. parsed[value] .. '>')
else
parsed[value] = key
print(space .. key .. '= {')
space = space .. ' '
foreach(value, function(key, value) doPrint(key, value, space) end)
end
else
if type(value) == 'string' then
value = '[[' .. tostring(value) .. ']]'
end
print(space .. key .. '=' .. tostring(value))
end
end
doPrint(name, tab)
end
local function clone(t)
local ret = {}
for k,v in pairs(t) do
ret[k] = v
end
return ret
end
local function inner_merge(t, remaining, from, ...)
if remaining == 0 then
return t
end
if from then
for k,v in pairs(from) do
t[k] = v
end
end
return inner_merge(t, remaining - 1, ...)
end
--[[*
Shallow-merges tables in order onto the first table.
@param t table to merge entries onto
@param ... sequence of 0 or more tables to merge onto 't'
@returns table 't' from input
]]
local function merge(t, ...)
return inner_merge(t, select('#', ...), ...)
end
-- Function to insert nulls into the JSON stream
local function null()
return null
end
-- Marker for 'undefined' values
local function undefined()
return undefined
end
local ArrayMT = {}
--[[
Return's true if the metatable marks it as an array..
Or false if it has no array component at all
Otherwise nil to get the normal detection component working
]]
local function IsArray(value)
if type(value) ~= 'table' then return false end
local meta = getmetatable(value)
local ret = meta == ArrayMT or (meta ~= nil and meta.__is_luajson_array)
if not ret then
if #value == 0 then return false end
else
return ret
end
end
local function InitArray(array)
setmetatable(array, ArrayMT)
return array
end
local CallMT = {}
local function isCall(value)
return CallMT == getmetatable(value)
end
local function buildCall(name, ...)
local callData = {
name = name,
parameters = {n = select('#', ...), ...}
}
return setmetatable(callData, CallMT)
end
local function decodeCall(callData)
if not isCall(callData) then return nil end
return callData.name, callData.parameters
end
local function doOptionMerge(options, nested, name, defaultOptions, modeOptions)
if nested then
modeOptions = modeOptions and modeOptions[name]
defaultOptions = defaultOptions and defaultOptions[name]
end
options[name] = merge(
{},
defaultOptions,
modeOptions,
options[name]
)
end
local json_util = {
printValue = printValue,
clone = clone,
merge = merge,
null = null,
undefined = undefined,
IsArray = IsArray,
InitArray = InitArray,
isCall = isCall,
buildCall = buildCall,
decodeCall = decodeCall,
doOptionMerge = doOptionMerge
}
return json_util
|