local m = dofile"lib/marshal.lua"
local car_t = m.g.struct {
	brand = m.t.str;
	year = m.t.u16;
}
local pack, unpack = m.transcoder {
	version = m.t.u16;
	name = m.t.str;
	networth = m.t.s32;
	byte = m.t.u8;
	blob = m.t.text;
	taxes = m.g.array(8, m.t.s32);
	mistresses = m.g.array(16, m.t.str);
	maincar = car_t;
	cars = m.g.array(8,car_t);
}

local s = pack {
	version = 0xAAEE;
	name = 'lord wigglesmith';
	networth = -1239201;
	byte = 0xBE;
	blob = [[it was a dark and stormy night]];
	taxes = {12,5123,12,633,-5816235};
	mistresses = {
		'evangeline';
		'clarissa';
		'catherine';
		'fredericka the great';
	};
	maincar = {
		brand = 'ford';
		year = 1234;
	};
	cars = {
		{brand = 'dodge', year = 2596};
		{brand = 'subaru', year = 321};
	};
}
if m.wrong(s) then print(s.exp) os.exit(1) end

local str = 'serialized:'
for i=1,#s do
	str = str ..' '.. string.format("%x",string.byte(str, i))
end
print(str)

local v = unpack(s)

local function dump(o)
	if type(o) == "table" then
		local str = ''
		for k,p in pairs(o) do
			str = str .. (k .. ' = {' .. dump(p) ..'}\n')
		end
		return str
	else
		return tostring(o)
	end
end

print(dump(v))
