码迷,mamicode.com
首页 > 其他好文 > 详细

lua 对表的简单序列化与反序列化

时间:2014-05-08 22:47:29      阅读:347      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   java   color   

参考文档:http://blog.csdn.net/xiaodan007/article/details/7096718

bubuko.com,布布扣
function sz_T2S(_t)
    local szRet = "{"
    function doT2S(_i, _v)
        if "number" == type(_i) then
            szRet = szRet .. "[" .. _i .. "] = "
            if "number" == type(_v) then
                szRet = szRet .. _v .. ","
            elseif "string" == type(_v) then
                szRet = szRet .. " .. _v .. " .. ","
            elseif "table" == type(_v) then
                szRet = szRet .. sz_T2S(_v) .. ","
            else
                szRet = szRet .. "nil,"
            end
        elseif "string" == type(_i) then
            szRet = szRet .. [" .. _i .. "] = 
            if "number" == type(_v) then
                szRet = szRet .. _v .. ","
            elseif "string" == type(_v) then
                szRet = szRet .. " .. _v .. " .. ","
            elseif "table" == type(_v) then
                szRet = szRet .. sz_T2S(_v) .. ","
            else
                szRet = szRet .. "nil,"
            end
        end
    end
    table.foreach(_t, doT2S)
    szRet = szRet .. "}"
    return szRet
end

function unsz_S2T(str)
    str = "return " .. str;
    local fun = loadstring(str);
    return fun();
end

local tt = {}
tt[1] = "aa";
tt.name = "halldd";
tt.tt = {};
tt.tt[3] = "bb";
print (tt[1]);

local ss = sz_T2S(tt);
local ttv = unsz_S2T(ss);
print (tt.name, tt.tt[3]);
bubuko.com,布布扣

 

lua 对表的简单序列化与反序列化,布布扣,bubuko.com

lua 对表的简单序列化与反序列化

标签:style   blog   class   code   java   color   

原文地址:http://www.cnblogs.com/emyueguang/p/3716765.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!