lua序列化table表到文件中
2013-07-19 11:39
253 查看
先上代码
function luautil.serialize(t, sort_parent, sort_child)
local mark={}
local assign={}
local function ser_table(tbl,parent)
mark[tbl]=parent
local tmp={}
local sortList = {};
for k,v in pairs(tbl) do
sortList[#sortList + 1] = {key=k, value=v};
end
if tostring(parent) == "ret" then
if sort_parent then table.sort(sortList, sort_parent); end
else
if sort_child then table.sort(sortList, sort_child); end
end
for i = 1, #sortList do
local info = sortList[i];
local k = info.key;
local v = info.value;
local key= type(k)=="number" and "["..k.."]" or k;
if type(v)=="table" then
local dotkey= parent..(type(k)=="number" and key or "."..key)
if mark[v] then
table.insert(assign,dotkey.."="..mark[v])
else
table.insert(tmp, "\n"..key.."="..ser_table(v,dotkey))
end
else
if type(v) == "string" then
table.insert(tmp, key..'="'..v..'"');
else
table.insert(tmp, key.."="..tostring(v));
end
end
end
return "{"..table.concat(tmp,",").."}";
end
return "do local ret=\n\n"..ser_table(t,"ret")..table.concat(assign," ").."\n\n return ret end"
end
function luautil.split(str, delimiter)
if (delimiter=='') then return false end
local pos,arr = 0, {}
-- for each divider found
for st,sp in function() return string.find(str, delimiter, pos, true) end do
table.insert(arr, string.sub(str, pos, st - 1))
pos = sp + 1
end
table.insert(arr, string.sub(str, pos))
return arr
end
function luautil.writefile(str, file)
os.remove(file);
local file=io.open(file,"ab");
local len = string.len(str);
local tbl = luautil.split(str, "\n");
for i = 1, #tbl do
file:write(tbl[i].."\n");
end
file:close();
end
1、基础功能抄自云风早期的代码。 这里稍微添加了排序功能,可以传入排序函数,否则表是按哈希排序的,输出后会乱掉。
2、添加了writefile函数,因为lua的文件写入有最大字节数限制,所以一行一行写入。
function luautil.serialize(t, sort_parent, sort_child)
local mark={}
local assign={}
local function ser_table(tbl,parent)
mark[tbl]=parent
local tmp={}
local sortList = {};
for k,v in pairs(tbl) do
sortList[#sortList + 1] = {key=k, value=v};
end
if tostring(parent) == "ret" then
if sort_parent then table.sort(sortList, sort_parent); end
else
if sort_child then table.sort(sortList, sort_child); end
end
for i = 1, #sortList do
local info = sortList[i];
local k = info.key;
local v = info.value;
local key= type(k)=="number" and "["..k.."]" or k;
if type(v)=="table" then
local dotkey= parent..(type(k)=="number" and key or "."..key)
if mark[v] then
table.insert(assign,dotkey.."="..mark[v])
else
table.insert(tmp, "\n"..key.."="..ser_table(v,dotkey))
end
else
if type(v) == "string" then
table.insert(tmp, key..'="'..v..'"');
else
table.insert(tmp, key.."="..tostring(v));
end
end
end
return "{"..table.concat(tmp,",").."}";
end
return "do local ret=\n\n"..ser_table(t,"ret")..table.concat(assign," ").."\n\n return ret end"
end
function luautil.split(str, delimiter)
if (delimiter=='') then return false end
local pos,arr = 0, {}
-- for each divider found
for st,sp in function() return string.find(str, delimiter, pos, true) end do
table.insert(arr, string.sub(str, pos, st - 1))
pos = sp + 1
end
table.insert(arr, string.sub(str, pos))
return arr
end
function luautil.writefile(str, file)
os.remove(file);
local file=io.open(file,"ab");
local len = string.len(str);
local tbl = luautil.split(str, "\n");
for i = 1, #tbl do
file:write(tbl[i].."\n");
end
file:close();
end
1、基础功能抄自云风早期的代码。 这里稍微添加了排序功能,可以传入排序函数,否则表是按哈希排序的,输出后会乱掉。
2、添加了writefile函数,因为lua的文件写入有最大字节数限制,所以一行一行写入。
相关文章推荐
- lua序列化table表到文件中
- cocos2dx lua 读取table 从 cvs文件中
- Lua里实现将table转成字符串(序列化)和将字符串转换回table(反序列化)
- Lua里实现将table转成字符串(序列化)和将字符串转换回table(反序列化)
- Lua读取CSV文件到table中
- [原创]从Confluence获取html table并将其序列化为C#类文件的工具
- lua处理PHP序列化过后的数据为table
- lua保存table到文件并从文件解析成table
- lua学习笔记(4)——lua配置文件和table使用
- lua Table的序列化与反序列化函数
- Lua里实现将table转成字符串(序列化)和将字符串转换回table(反序列化)
- Lua中使用table生成链表完成正序和倒序的文件读入
- Lua里实现将table转成字符串(序列化)和将字符串转换回table(反序列化)
- lua里table转string(序列化)和string转table(反序列化)
- Lua里实现将table转成字符串(序列化)和将字符串转换回table(反序列化)
- QT xml文件转化lua table文件 工具
- c、c++获取lua脚本里面定义的table可做配置文件用
- lua文件操作(3)----txt文件转换成lua文件的table格式
- lua里实现table的序列化和反序列化
- c++如何接收从luaJit 的lua文件传送的table数据?