Re: object serialization

Jason McKesson <[email protected]>
Newsgroups gmane.comp.lang.lua.bind.user
Message-ID <[email protected]>
On 4/21/2011 6:28 PM, Szymon Gatner wrote:
> Hi,
>
> what would be the simple and elegant way to do serialization /
> persistance of a specific luabind::object?
>
> I only need to save numeric and string values and tables of them. I
> was thinking about recursive visitation like so:
>
> void visit(const luabind::object&  obj)
> {
>    using namespace luabind;
>
>    if (type(obj) == LUA_TTABLE)
>    {
>      cout<<  "{"<<  endl;
>
>      for (luabind::iterator it(obj); it != luabind::iterator(); ++it)
>      {
>        if (type(it.key()) == LUA_TSTRING)
>        {
>          if (type(*it) != LUA_TTABLE)
>          {
>            cout<<  it.key()<<  " = "<<  *it<<  endl;
>          }
>          else
>          {
>            visit(*it);
>          }
>        }
>      }
>
>      cout<<  "}"<<  endl;
>    }
> }
>
> cout is used here to print name/value pairs but in real case actual
> persistance code would be used.
>
> Am I going the right way?
>
> Cheers,
> Szymon
If the luabind::object is a Lua table, wouldn't the easiest way to 
serialize it be to convert it into a Lua script file? I've included a 
Lua script I wrote which takes an arbitary table (that contains only 
standard Lua types: numbers, tables, arrays, and strings) and writes a 
file that contains the table as a valid Lua script. All you need to do 
is print "return" to the file before calling "WriteTable", and you will 
get a Lua script. If you call "dofile" on this Lua script, it will 
return the table.

Luabind is *way* overkill for this.

------------------------------------------------------------------------------
Fulfilling the Lean Software Promise
Lean software platforms are now widely adopted and the benefits have been 
demonstrated beyond question. Learn why your peers are replacing JEE 
containers with lightweight application servers - and what you can gain 
from the move. http://p.sf.net/sfu/vmware-sfemails

_______________________________________________
luabind-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/luabind-user
_TableWriter.lua (text/plain, 2 KB)
--This file exports a funciton, WriteTable, that writes a given table out to a given file handle.

require "_util";

local writeKey = {};

function writeKey.string(hFile, value, iRecursion)
	WriteFormatted(hFile, "[\"%s\"]", value);
end

function writeKey.number(hFile, value, iRecursion)
	WriteFormatted(hFile, "[%i]", value);
end

local writeValue = {};

function writeValue.string(hFile, value, iRecursion)
	WriteFormatted(hFile, "[==[%s]==]", value);
end

function writeValue.number(hFile, value, iRecursion)
	WriteFormatted(hFile, "%i", value);
end

function writeValue.boolean(hFile, value, iRecursion)
	if(value) then hFile:write("true"); else hFile:write("false"); end;
end

function writeValue.table(hFile, value, iRecursion)
	WriteTable(hFile, value, iRecursion)
end

local function WriteTabs(hFile, iRecursion)
	for iCount = 1, iRecursion, 1 do
		hFile:write("\t");
	end
end

function WriteTable(hFile, outTable, iRecursion)
	if(iRecursion == nil) then iRecursion = 1; end
	
	hFile:write("{\n");
	
	local bHasArray = false;
	local arraySize = 0;
	
	if(#outTable > 0) then bHasArray = true; arraySize = #outTable; end;
	
	for key, value in pairs(outTable) do
		if(writeKey[type(key)] == nil) then print("Malformed table key."); return; end
		if(writeValue[type(value)] == nil) then
			print( string.format("Bad value in table: key: '%s' value type '%s'.", key, type(value)));
			return;
		end
		
		--If the key is not an array index, process it.
		if((not bHasArray) or
				(type(key) ~= "number") or
				not((1 <= key) and (key <= arraySize))) then
			WriteTabs(hFile, iRecursion);
			writeKey[type(key)](hFile, key, iRecursion + 1);
			hFile:write(" = ");
			writeValue[type(value)](hFile, value, iRecursion + 1);
			
			hFile:write(",\n");
		end
	end
	
	if(bHasArray) then
		for i, value in ipairs(outTable) do
			WriteTabs(hFile, iRecursion);
			writeValue[type(value)](hFile, value, iRecursion + 1);
			hFile:write(",\n");
		end
	end

	WriteTabs(hFile, iRecursion - 1);
	hFile:write("}");
end
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.