简体   繁体   English

是否存在 Lua 表/数组美化器?

[英]Does a Lua Table / Array Beautifier exist?

Considering all the json, xml formatters\/beautifies out there, I've been unable to find one for Lua tables\/arrays ?考虑到所有的 json、xml 格式化程序\/美化工具,我一直无法为 Lua 表\/数组找到一个?

The nuance here is that the output to beautify, is from what i believe is a widely used table\/array dump<\/code> function - see below..这里的细微差别是要美化的输出来自我认为广泛使用的表\/数组dump<\/code>函数 - 见下文..

function dump(o)
    if type(o) == 'table' then
        local s = '{ '
        for k,v in pairs(o) do
            if type(k) ~= 'number' then 
                k = '"'..k..'"' 
            end
            s = s .. '['..k..'] = ' .. dump(v) .. ','
        end
        return s .. '} '
    else
        return tostring(o)
    end
end

An example of such processing, you can correct the little things yourself:这样处理的一个例子,你可以自己纠正一些小事情:

function dump(o,level)
    level = level or 1
    if type(o) == 'table' then
        local s = {}
        s[1] = '{ '
        for k,v in pairs(o) do
            if type(k) ~= 'number' then 
                k = '"'..k..'"' 
            end
            s[#s+1] = string.rep('\t',level).. '['..k..'] = ' .. dump(v, level+1) .. ','
        end
        s[#s+1] = string.rep('\t',level) .. '} '
        return table.concat(s , "\n")
    else
        return tostring(o or 'nil')
    end
end
local t = {[1] = nil,[2] = { ["attr"] = { [1] = code,[2] = remaining,[3] = resetdate,["remaining"] = 990,["resetdate"] = 1638614242,["code"] = 200,} ,["tag"] = success,} ,[3] = nil,["attr"] = { } ,["tag"] = prowl,}

print (dump(t))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM