• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Lua Serializing and unserializing a metatable

dudeim

Member
Joined
Oct 5, 2007
Messages
121
Reaction score
9
Hey,

So I got this problem I have a table containing a metatable and well I want to serialize that table, but the built in function only works for non metatables.

These are the functions I'm talking about:
Lua:
function table.serialize(x, recur)
	local t = type(x)
	recur = recur or {}

	if(t == nil) then
		return "nil"
	elseif(t == "string") then
		return string.format("%q", x)
	elseif(t == "number") then
		return tostring(x)
	elseif(t == "boolean") then
		return x and "true" or "false"
	elseif(getmetatable(x)) then --as you can see it doesn't accept tables (also if i remove this line i don't think it will work it isn't in here for no reason
		error("Can not serialize a table that has a metatable associated with it.")
	elseif(t == "table") then
		if(table.find(recur, x)) then
			error("Can not serialize recursive tables.")
		end
		table.append(recur, x)

		local s = "{"
		for k, v in pairs(x) do
			s = s .. "[" .. table.serialize(k, recur) .. "]" .. " = " .. table.serialize(v, recur) .. ", "
		end

		return s:sub(0, s:len() - 2) .. "}"
	end

	error("Can not serialize value of type '" .. t .. "'.")
end

function table.unserialize(str)
	return loadstring("return " .. str)()
end
So is there anyway I can do this with metatables?

Thanks!
 
Yeah, it's true, i confess than i read it to learn when somebody helps you, but i also try to help another person either and i post him post here too by accident.

Anyway, i hope you find the solution to your problem

-¡Free Bump!-


¡Bless you and Cheers up!
(Y)(Y)


-Obsdark-
 
Back
Top