I'm not a master of Lua, but the way I've written it, I think if you provide a :__serialize() function in the metatable, like
Code: Select all
RandomObject = {}
RandomObject.__index = RandomObject
--things that operate on random objects
function RandomObject:__serialize()
--return a string that you can make new RandomObjects with?
end
I threw it in as an afterthought.
And thanks for the tip on that, I originally had it using table.insert, then I switched to the size, but kept the concatenation.
edit: Oh, and the reason I pass t but alias it to data is because I found it sped up the script a bit. Could have just been a fluke, though. Probably was.
edit: To clarify, the reason I did the concatenation in the buffer insertion is because I originally passed the buffer to the new serialization, which unless I made size another argument or part of the table (which would slow it down) would mess up the insertion and corrupt the data, so... There's the method to some of my madness.
edit: Last one, I hope
Having made your edits (thanks, by the way), adding newlines slows down concatenation (on 4096 elements, from 0.107 seconds for 3 serializations to 0.1113). Additionally, I didn't make this library to pretty print, but rather to produce compact output that could be sent over a network or saved to a file reasonably. I'll add a poll, I guess.
Oh, and making pretty-ish printing an option slowed it down even more than either of the two.
edit: Well, I was wrong.
This is turning into a wall of text. However, I've figured out more of the method to my own madness. Serialization of the userdata could happen by the function returning something similar to
Code: Select all
function RandomObject:__serialize() return "RandomObject:new( " .. self.randomData .. " )" end
and I'm fairly sure that would work. I haven't gotten the chance to test it myself, though. I also removed tostring (what was I thinking) as an option for userdata serialization.
edit: I should think these out more
You could even do something like
Code: Select all
function RandomObject:__serialize() return "RandomObject:new(" .. serialize( self.data ) .. ")" end