Difference between revisions of "Talk:binser"

(Created page with "Is here any inbuilt serialize function?")
 
m
 
(2 intermediate revisions by one other user not shown)
Line 1: Line 1:
Is here any inbuilt serialize function?
+
Is here any inbuilt serialize function? --[[User:Darkfrei|Darkfrei]] ([[User talk:Darkfrei|talk]]) 20:45, 20 January 2021 (CET)
 +
 
 +
 
 +
For example (my code)
 +
<source lang="lua">
 +
serialize = function (tabl)
 +
local str = ''
 +
for i, v in pairs (tabl) do
 +
local pr = (type(i)=="string") and i..'=' or ''
 +
if type (v) == "table" then
 +
str = str .. pr..'{'..serialize(v)..'},'
 +
elseif type (v) == "string" then
 +
str = str .. pr..'"'..tostring(v)..'",'
 +
else
 +
str = str .. pr..tostring(v)..','
 +
end
 +
end
 +
str = str:sub(1, -2) -- remove last comma
 +
return str
 +
end
 +
 
 +
local a = {1,2,3, a='b', c={"d",e="e"},d={{1},{2}}, f={}}
 +
print (serialize(a)) -- 1,2,3,d={{1},{2}},f={},a="b",c={"d",e="e"}
 +
</source>
 +
--[[User:Darkfrei|Darkfrei]] ([[User talk:Darkfrei|talk]]) 17:57, 21 February 2021 (CET)
 +
 
 +
As I know, you can use [[love.data.pack]] and [[love.data.unpack]]
 +
--[[User:Andlac028|Andlac028]] ([[User talk:Andlac028|talk]]) 16:22, 25 February 2021 (CET)

Latest revision as of 15:22, 25 February 2021

Is here any inbuilt serialize function? --Darkfrei (talk) 20:45, 20 January 2021 (CET)


For example (my code)

serialize = function (tabl)
	local str = ''
	for i, v in pairs (tabl) do
		local pr = (type(i)=="string") and i..'=' or ''
		if type (v) == "table" then
			str = str .. pr..'{'..serialize(v)..'},'
		elseif type (v) == "string" then
			str = str .. pr..'"'..tostring(v)..'",'
		else
			str = str .. pr..tostring(v)..','
		end
	end
	str = str:sub(1, -2) -- remove last comma
	return str
end

local a = {1,2,3, a='b', c={"d",e="e"},d={{1},{2}}, f={}}
print (serialize(a)) -- 1,2,3,d={{1},{2}},f={},a="b",c={"d",e="e"}

--Darkfrei (talk) 17:57, 21 February 2021 (CET)

As I know, you can use love.data.pack and love.data.unpack --Andlac028 (talk) 16:22, 25 February 2021 (CET)