serialize.lua, A simple serialization library

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
meoiswa
Prole
Posts: 48
Joined: Wed Mar 14, 2012 8:13 pm
Location: Venezuela

serialize.lua, A simple serialization library

Post by meoiswa »

I tried using the libraries avaliable in the wiki but none seemed to work (For me, I probably screwed up somewhere).
So, I ended up making my own.
Do note, this isn't optimized in any way, and probably produces serialized strings far larger than what they need to be.
Serialize.love
Contains serialize.lua and a little main.lua showing how to use it :)
(2.22 KiB) Downloaded 373 times
This demo will print a table using simple print() statements. It will then serialize that table, unserialize the resulting string, and print the new table, using the same printing method.
It can serialize any of the following:
- Numbers

Code: Select all

n = 1000234123.324
serialize(n)
- Strings (duh)

Code: Select all

n = "Lalala I'm so pretty!"
serialize(n)
- Tables, with Numbers, Strings or other Tables, even if there is recursion (AKA infinite tables)

Code: Select all

n = {}
n.numbah = 123
n.string = "No, you're uggly!"
table.insert(n,321)
table.insert(n,"WHAT IS THIS I DON'T EVEN")
n.self = n
serialize(n)

It can't serialize any of the following:
- Metatables of tables (You could, theoretically, serialize the metatable apart and then re-join after unserializing)

Code: Select all

n = {}
meta = {}
meta.something = "that" -- I don't even know how to use metatables!
table.setMetatable(n,meta) -- Or whatever it is
serialize(n) -- The metatable will be lost!
- Functions

Code: Select all

gimmeAZero = function() return 0 end
serialize(gimmeAZero) -- Will return AN EMPTY STRING (Not nil)
n = {}
n.func = gimmeAZero
serialize(n) -- n.func will be lost!
Feel free to use it if it suits your needs
Write the ö of Löve with Alt+numPad(148) or Alt+numPad(153)
Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests