This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Wojbie's profile picture

SerializeRec - Recursive Table Serialize - Compatible with textutils.unserialize(). v1

Started by Wojbie, 19 October 2015 - 09:51 AM
Wojbie #1
Posted 19 October 2015 - 11:51 AM
SerializeRec - Recursive Table Serialize - Compatible with textutils.unserialize(). v1.0

Have you ever needed to store a table with recursive entries? Did standard textutils.serialize() failed you? Then this api is for you!

SerializeRec is simple one function api that allows you to serialize tables that contain recursive entries. It preserves the structure of input table and outputs string that can be unserialized using standard cc textutils.unserialize back into said table with the same structure and correct everything.

Download Here: http://pastebin.com/APF0HTE0
Or ingame by using command:
pastebin get APF0HTE0 serializerec
(requires http-api to be active)

As it is api you need to os.loadAPI("serializerec") it. It contains one function in both spelling versions serialiseRec/serializeRec. You treat it like normal serialize and it will output string like normal serialize.

Features List:
  • Serializes recursive tables
  • Textutils.unserialize compatible
  • Awesome!
Example:
SpoilerIf given table:

a={}
a.a=a
And said table is recursive and not serializeable by basic serialize.

If serialized via this api it will output string:

(function()
t1={}
t1["a"]=t1
return t1 end)()
And said string when send to textutils.unserialise will return said table with recursion rebuild.

No screenshots cause its kinda weird thing to screenshot.


Change-log:
Spoilerv1.0 - First Relase

Plans For Future:
Spoiler–Fix any bugs if found.

Any Comment and Suggestions how to improve Code are appreciated.
Edited on 16 January 2016 - 06:18 PM
Konlab #2
Posted 19 October 2015 - 02:42 PM
Sounds very interesting and useful for example for saving the _G variable (containing _G).
Can this handle non-recursive tables in tables? And recursive tables pointing to grandparent? (E.g. a.b.c = a or a.b = _G)
On feature list Avesome should be Awesome
Wojbie #3
Posted 19 October 2015 - 02:50 PM
Can this handle non-recursive tables in tables? And recursive tables pointing to grandparent? (E.g. a.b.c = a
Yes it handles that!

or a.b = _G)
But this will error cause _G contains functions and functions are not serialisable.

Basically it makes internal list of tables and when it encounters same table again it just notes that its same tale.
I am currently thinking of ways to shorten the output string cause when compared with standard one its longer and less readable.
Edited on 19 October 2015 - 12:54 PM
Creator #4
Posted 19 October 2015 - 02:56 PM
You can string dump functions.
Wojbie #5
Posted 19 October 2015 - 03:00 PM
You can string dump functions.

True but only functions that are lua based and don't contain upvalues. And then you hit string bug when attempting to save/transmit resulting string.
Edited on 19 October 2015 - 01:08 PM
FUNCTION MAN! #6
Posted 19 October 2015 - 11:20 PM
Base64 the functions.
Wojbie #7
Posted 19 October 2015 - 11:49 PM
Base64 the functions.
True that would work. Only problem left is that you don't have access to _G inside textutils.unserialise(). So there is no way to load() the string dump back into function. And making custom one that lets the string to unserialise access to _G would open hole that would allow for remote code execution. So while i agree its doable i simply don't see the usefulness of that.
apemanzilla #8
Posted 19 October 2015 - 11:55 PM
Also I don't think there will be a way to load dumped functions once CC updates to Lua 5.2 given that Dan's compatibility layer only allows text chunks, not binary chunks.
FUNCTION MAN! #9
Posted 19 October 2015 - 11:59 PM
The binary chunk thing only doesn't work if it's explicit, look:



edit: that is in game, I just cropped the screenshot so it isn't huge.

more edit: relevant lines
Edited on 19 October 2015 - 10:03 PM