How to serialize ANY table?
-
- Prole
- Posts: 7
- Joined: Thu Apr 21, 2022 3:16 pm
How to serialize ANY table?
How to serialize ANY table? I sure this table contains only other tables and simple values (string, nil, number)
Re: How to serialize ANY table?
nil is not a value, no element it table has nil value.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)
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
-
- Prole
- Posts: 7
- Joined: Thu Apr 21, 2022 3:16 pm
Re: How to serialize ANY table?
Does it work for tables-structures (when index is string)?
-
- Party member
- Posts: 548
- Joined: Wed Oct 05, 2016 11:53 am
Re: How to serialize ANY table?
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.
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.
-
- Prole
- Posts: 7
- Joined: Thu Apr 21, 2022 3:16 pm
Re: How to serialize ANY table?
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.
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.
Re: How to serialize ANY table?
Yes, pairs iterates all indices, but ipairs from 1 to first gap (exclusive) with step by one.20_NickName_20 wrote: ↑Sun Apr 24, 2022 9:01 pm Does it work for tables-structures (when index is string)?
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.
-
- Party member
- Posts: 548
- Joined: Wed Oct 05, 2016 11:53 am
Re: How to serialize ANY 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?
-
- Prole
- Posts: 7
- Joined: Thu Apr 21, 2022 3:16 pm
Re: How to serialize ANY table?
Not sure, but it looks like that you can just comment this three lines:
-
- Prole
- Posts: 7
- Joined: Thu Apr 21, 2022 3:16 pm
Re: How to serialize ANY table?
I already tried it but it didn't work. (i think after this, function returns an empty string)
Who is online
Users browsing this forum: Bing [Bot] and 5 guests