lua table to string,string to table
2013-01-24 11:20
267 查看
--[[serialize.lua 声明序列化接口,并提供serialize和unserialize函数用于对Table对象进行序列化和反序列化操作 Exported API: Example: --]] require "base.interface" Serializable = interface(nil, "writeObject", "readObject") -------------------------------------------------------------------------------- --序列化一个Table function serialize(t) local mark={} local assign={} local function table2str(t, parent) mark[t] = parent local ret = {} if table.isArray(t) then for i,v in pairs(t) do local k = tostring(i) local dotkey = parent.."["..k.."]" local t = type(v) if t == "userdata" or t == "function" or t == "thread" or t == "proto" or t == "upval" then --ignore elseif t == "table" then if mark[v] then table.insert(assign, dotkey.."="..mark[v]) else table.insert(ret, table2str(v, dotkey)) end elseif t == "string" then table.insert(ret, string.format("%q", v)) elseif t == "number" then if v == math.huge then table.insert(ret, "math.huge") elseif v == -math.huge then table.insert(ret, "-math.huge") else table.insert(ret, tostring(v)) end else table.insert(ret, tostring(v)) end end else for f,v in pairs(t) do local k = type(f)=="number" and "["..f.."]" or f local dotkey = parent..(type(f)=="number" and k or "."..k) local t = type(v) if t == "userdata" or t == "function" or t == "thread" or t == "proto" or t == "upval" then --ignore elseif t == "table" then if mark[v] then table.insert(assign, dotkey.."="..mark[v]) else table.insert(ret, string.format("%s=%s", k, table2str(v, dotkey))) end elseif t == "string" then table.insert(ret, string.format("%s=%q", k, v)) elseif t == "number" then if v == math.huge then table.insert(ret, string.format("%s=%s", k, "math.huge")) elseif v == -math.huge then table.insert(ret, string.format("%s=%s", k, "-math.huge")) else table.insert(ret, string.format("%s=%s", k, tostring(v))) end else table.insert(ret, string.format("%s=%s", k, tostring(v))) end end end return "{"..table.concat(ret,",").."}" end if type(t) == "table" then return string.format("%s%s", table2str(t,"_"), table.concat(assign," ")) else return tostring(t) end end local EMPTY_TABLE = {} -------------------------------------------------------------------------------- --反序列化一个Table function unserialize(str) if str == nil or str == "nil" then return nil elseif type(str) ~= "string" then EMPTY_TABLE = {} return EMPTY_TABLE elseif #str == 0 then EMPTY_TABLE = {} return EMPTY_TABLE end local code, ret = pcall(loadstring(string.format("do local _=%s return _ end", str))) if code then return ret else EMPTY_TABLE = {} return EMPTY_TABLE end end
相关文章推荐
- lua table to string
- lua 实现tableToString
- lua table to string,string to table
- lua里table转string(序列化)和string转table(反序列化)
- Lua里实现将Table转为String输出,Debug神器
- UDF_StringToTable 普通与递归实现
- LUA中遇到这个错误attempt to call a nil value (global 'loadstring')
- lua转换string到Table
- 【Lua学习笔记】 --> 《字符串string与table的函数操作》
- Lua中table.sort() 报错 attempt to compare number with nil和invalid order function for sorting
- SqlServer StringToTable性能测试
- 记录lua的table转string一个问题
- PostgreSql Procedure copy split string to table
- StringSplitToTable
- Lua Save Table to File
- Sql Character String split to table
- (pbc lua 加入)c++_lua_Python with/without extension性能测试 (10万次SerializeToString & ParseFromString)
- ORA-01652 unable to extend temp segment by string in tablespace string
- [LUA ERROR] [string "cocos/cocos2d/Cocos2dConstants.lua"]:620: attempt to index a nil value解决办法
- NPOI, table string to excel