跳转到内容

模組:EncoderUtil

本页使用了标题或全文手工转换
被永久保护的模块
维基百科,自由的百科全书

这是本页的一个历史版本,由A2569875留言 | 贡献2020年8月24日 (一) 02:40 建立内容为“local p={} local function _safejson_number(obj, flat_out) if type(obj) == type(0) then if not not tostring(obj):lower():find('inf') then return…”的新页面)编辑。这可能和当前版本存在着巨大的差异。

(差异) ←上一修订 | 最后版本 (差异) | 下一修订→ (差异)

local p={}
local function _safejson_number(obj, flat_out)
	if type(obj) == type(0) then
  		if not not tostring(obj):lower():find('inf') then return tostring(obj) end
    	if not not tostring(obj):lower():find('nan') then return flat_out and tostring(obj) or '\127null\127'end
		return flat_out and tostring(obj) or obj
	else
		return obj
	end
end
local function _safejson_clone(obj)
	if type(obj) == type({'table'}) then
		local new_table = {}
		for key,val in pairs(obj) do
			if type(val) == type({'table'}) then 
				if xpcall(function()new_table[key] = mw.text.jsonDecode(mw.text.jsonEncode(val))end,function()end) then else new_table[key] = key end
			elseif type(val) == type(function()end) then new_table[key] = key
			elseif type(val) == type(nil) then 
			elseif type(val) == type(0) then new_table[key] = _safejson_number(val)
			else new_table[key] = mw.clone(val) end
		end
		return new_table
	else
		return obj
	end
end
local function _check_table(obj)
	if type(obj) == type({'table'}) then
		local new_table = {}
		for key,val in pairs(obj) do
			if type(val) == type({'table'}) then new_table[key] = _safejson_clone(val)
			elseif type(val) == type(function()end) then new_table[key] = key
			elseif type(val) == type(nil) then new_table[key] = '\127null\127'
			elseif type(val) == type(0) then new_table[key] = _safejson_number(val)
			else new_table[key] = mw.clone(val) end
		end
		return new_table
	else
		return obj
	end
end
local function _jsonEncode(obj)
	local result = ''
	if type(obj) == type({'table'}) then
		result = mw.text.jsonEncode(_check_table(obj))
	elseif type(obj) == type(function()end) then result = 'function'
	elseif type(obj) == type(nil) then result = 'null'
	elseif type(obj) == type(0) then 
		local can_encode = false
		can_encode,result = xpcall(function()return mw.text.jsonEncode(obj)end,function()end)
		if can_encode==false then result=_safejson_number(obj,true)end
	else result = mw.text.jsonEncode(obj) end
	return result
end
function p.jsonEncode(obj)
	local args, working_frame
    if frame == mw.getCurrentFrame() then
        -- We're being called via #invoke. The args are passed through to the module
        -- from the template page, so use the args that were passed into the template.
        if lib_arg.getArgs == nil then lib_arg = require('Module:Arguments') end
        args = lib_arg.getArgs(frame, {
        	parentFirst=true,
        	trim = false,
			removeBlanks = false
        })
        working_frame = frame
    else
        -- We're being called from another module or from the debug console, so assume
        -- the args are passed in directly.
        args = frame
        working_frame = mw.getCurrentFrame()
        if type(args) ~= type({}) then args = {frame} end
    end
    return _jsonEncode(args)
end
p._jsonEncode=_jsonEncode
return p