CSV-like table serialization

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
User avatar
kbmonkey
Party member
Posts: 122
Joined: Tue Sep 01, 2015 12:19 pm

CSV-like table serialization

Post by kbmonkey » Mon Jan 08, 2018 6:22 pm

There's a bunch of serialization functions out there, here is yet another. It outputs a CSV-like format. Handles standard types and circular table references. Does not serialize user data, functions or up-values.

A perf test against the trusted pickle function shows it is 0.114 seconds slower (across 10,000 iterations) and produces a string 0.8 times the size of pickle.

This function is only a curiosity. I can't say it is fool proof but tests have been positive so far, and the output is pleasantly readable.

example output

this table

Code: Select all

local alice = { name="Alice", balance=100 }
local bob = { name="Bob \"the coder\"", balance=-42 }
local financials = {
    ["version"] = 42,
    ["accounts"] = { alice, bob },
    ["transactions"] = {
        { account=alice, amount=50 },
        { account=alice, amount=40 },
        { account=alice, amount=10 },
        { account=bob, amount=-20 },
        { account=bob, amount=-20 },
        { account=bob, amount=-2 }
    }
}
is output as this string

Code: Select all

00,accounts,version,transactions
00,1,2
00,1,2,3,4,5,6
00,name,balance
00,name,balance
00,amount,account
00,amount,account
00,amount,account
00,amount,account
00,amount,account
00,amount,account
01,[02],42,[03]
02,[04],[05]
03,[06],[07],[08],[09],[10],[11]
04,Alice,100
05,"Bob ""the coder""",-42
06,50,[04]
07,40,[04]
08,10,[04]
09,-20,[05]
10,-20,[05]
11,-2,[05]
Cerial.lua

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests