How to serialize ANY table?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
20_NickName_20
Prole
Posts: 7
Joined: Thu Apr 21, 2022 3:16 pm

How to serialize ANY table?

Post by 20_NickName_20 »

How to serialize ANY table? I sure this table contains only other tables and simple values (string, nil, number)
User avatar
darkfrei
Party member
Posts: 1181
Joined: Sat Feb 08, 2020 11:09 pm

Re: How to serialize ANY table?

Post by darkfrei »

20_NickName_20 wrote: Sun Apr 24, 2022 6:32 pm How to serialize ANY table? I sure this table contains only other tables and simple values (string, nil, number)
nil is not a value, no element it table has nil value.

the simplest deep serialization:

Code: Select all

function easyDeepSer (tabl, deep)
	deep = 0 or deep
	for i, v in pairs (tabl) do
		if type (v) == "table" then
			easyDeepSer (v, deep+1)
		else
			print (deep, i, tostring (v))
		end
	end
end
But better to use the serpent: https://github.com/pkulchenko/serpent
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
20_NickName_20
Prole
Posts: 7
Joined: Thu Apr 21, 2022 3:16 pm

Re: How to serialize ANY table?

Post by 20_NickName_20 »

Does it work for tables-structures (when index is string)?
MrFariator
Party member
Posts: 515
Joined: Wed Oct 05, 2016 11:53 am

Re: How to serialize ANY table?

Post by MrFariator »

Both the sample script by darkfrei and serpent can handle those, yes, you'd just have to adapt darkfrei's code to output to a file or so. A pairs() loop iterates over all key-value pairs within a table, whether the key is a number or string, but the order of access can be a bit random.

However, darkfrei's code wouldn't be able to handle a circular table where keys reference the table that contains the tables.

For an alternative suggestion, bitser can also serialize great many tables, if you don't mind the output being unreadable to humans.
20_NickName_20
Prole
Posts: 7
Joined: Thu Apr 21, 2022 3:16 pm

Re: How to serialize ANY table?

Post by 20_NickName_20 »

Okay, i think this works, but now i get this error:

Error

bitser.lua:280: cannot serialize type userdata


Traceback

[love "callbacks.lua"]:228: in function 'handler'
[C]: in function 'error'
bitser.lua:280: in function 'serialize_value'
bitser.lua:221: in function <bitser.lua:190>
bitser.lua:281: in function 'serialize_value'
bitser.lua:222: in function <bitser.lua:190>
bitser.lua:281: in function 'serialize_value'
bitser.lua:287: in function 'serialize'
bitser.lua:415: in function 'dumps'
main.lua:43: in function 'draw'
[love "callbacks.lua"]:168: in function <[love "callbacks.lua"]:144>
[C]: in function 'xpcall'

I think I didn't put userdata in my table.
User avatar
darkfrei
Party member
Posts: 1181
Joined: Sat Feb 08, 2020 11:09 pm

Re: How to serialize ANY table?

Post by darkfrei »

20_NickName_20 wrote: Sun Apr 24, 2022 9:01 pm Does it work for tables-structures (when index is string)?
Yes, pairs iterates all indices, but ipairs from 1 to first gap (exclusive) with step by one.

If you have table = {[0] = "zero", [1.5] = "one and half", [math.pi] = "pi", pi = math.pi}, then use pairs for it.
In this case all not defined elements are nil and have no value and need no memory.


If you have no userdata or don't need to save it, just add the exception for it in line bitser.lua:280

For my opinion it's very helpful to use file output that you can read, sometimes the code makes not what you think, but what you wrote.
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
MrFariator
Party member
Posts: 515
Joined: Wed Oct 05, 2016 11:53 am

Re: How to serialize ANY table?

Post by MrFariator »

20_NickName_20 wrote: Mon Apr 25, 2022 4:45 am I think I didn't put userdata in my table.
If the table you are trying to serialize contains any instances of a class, you may have to register that class with bitser. Alternatively, the error may also be caused by any löve objects you have in your table, like images. See the USAGE.md on the github page for details.

Can you post some sample code about what your table contains?
20_NickName_20
Prole
Posts: 7
Joined: Thu Apr 21, 2022 3:16 pm

Re: How to serialize ANY table?

Post by 20_NickName_20 »

darkfrei wrote: Mon Apr 25, 2022 7:13 am If you have no userdata or don't need to save it, just add the exception for it in line bitser.lua:280
How can I do it?
User avatar
darkfrei
Party member
Posts: 1181
Joined: Sat Feb 08, 2020 11:09 pm

Re: How to serialize ANY table?

Post by darkfrei »

20_NickName_20 wrote: Mon Apr 25, 2022 1:23 pm
darkfrei wrote: Mon Apr 25, 2022 7:13 am If you have no userdata or don't need to save it, just add the exception for it in line bitser.lua:280
How can I do it?
Not sure, but it looks like that you can just comment this three lines:
2022-04-25T19_36_03-bitser_bitser.lua at master · gvx_bitser.png
2022-04-25T19_36_03-bitser_bitser.lua at master · gvx_bitser.png (6.29 KiB) Viewed 3923 times
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
20_NickName_20
Prole
Posts: 7
Joined: Thu Apr 21, 2022 3:16 pm

Re: How to serialize ANY table?

Post by 20_NickName_20 »

I already tried it but it didn't work. (i think after this, function returns an empty string)
Post Reply

Who is online

Users browsing this forum: No registered users and 50 guests