Tserial

About

Tserial is a library for turning tables into strings ("serialization") and vice-versa. This has several uses, including making a game save/load system (you write all your game state to a file) and multiplayer (you can pass tables across the network). Tserial turns tables into Lua script, for maximum ease-of-use.

Download

Direct from Dropbox

Contact

  • Taehl - SelfMadeSpirit@gmail.com

Setup

  1. Put Tserial.lua in your game's folder
  2. At the top of main.lua, add the line require"Tserial"

FAQ

Q) Can Tserial pack a table that contains other tables?
A) Yes. Any number of nested tables is fine (it's suitable even for things like tile maps). However, Tserial can NOT handle self-referential tables (such as t={t} or t={{1,2,3},{t}}).
Q) What data types can/can't be handled by Tserial?
A) It will properly serialize strings, numbers, booleans, and tables. A table may have any of these types as either keys or values. Userdata and functions can't be serialized by default, but can be supported with a little extra work.
Q) How can I serialize userdata?
A) The second parameter of Tserial.pack is used for these. If it's true, Tserial will simply skip userdata and functions. If it's a function, this function will be used to serialize the data. If it's a table, the table will be used for serial lookups.

Functions

Tserial.pack

Tserial.pack(t, drop, indent)

Serializes a table into a string. Returns a string of Lua script which recreates the table.

table t
The table to be serialized. May not be self-referential.
table drop
What to do upon encountering data that can't be serialized, like userdata and functions. If absent, Tserial will throw an error upon encountering them. If drop is a function, then it will be called with one parameter (the data) and expect a string in return (so you can supply your own serializing function). If drop is a table, it will be used to look up serials (with data as keys and serials as values).
boolean indent
If true, output will be in "human-readable mode" with newlines and proper indentation. Otherwise, the string will all be on one line (resulting in smaller sizes).

Tserial.unpack

Tserial.unpack(s)

Turns a serial back into a table. Returns a table recreated from the string.

string s
The serial to be turned into a table.