zoukankan      html  css  js  c++  java
  • lua table to string,string to table

    --[[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
    

  • 相关阅读:
    给刚工作不久的程序猿同学的一封信
    Java进阶篇设计模式之六 ----- 组合模式和过滤器模式
    Java进阶篇设计模式之五-----外观模式和装饰器模式
    Java进阶篇设计模式之四 -----适配器模式和桥接模式
    Java进阶篇设计模式之三 ----- 建造者模式和原型模式
    SpringBoot简单打包部署(附工程)
    SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)【转载】
    Eclipse快捷键大全(转载)
    在redis一致性hash(shard)中使用lua脚本的坑
    如何评价微软Connect 2015?[转载]
  • 原文地址:https://www.cnblogs.com/byfei/p/3112124.html
Copyright © 2011-2022 走看看