/serialize.lua
Lua | 113 lines | 82 code | 7 blank | 24 comment | 22 complexity | 4640ea608df9e55ae90ff0fee25846a9 MD5 | raw file
1---------------------------------------------------------------------------- 2-- Serialize tables. 3-- It works only for tables without cycles and without functions or 4-- userdata inside it. 5-- @release $Id: serialize.lua 34 2010-04-19 21:18:00Z rpusztai $ 6---------------------------------------------------------------------------- 7 8local ipairs, pairs, type = ipairs, pairs, type 9local format = string.format 10local sort, tinsert = table.sort, table.insert 11 12-- 13local value = nil 14 15---------------------------------------------------------------------------- 16-- Serializes a table. 17-- @param tab Table representing the session. 18-- @param outf Function used to generate the output. 19-- @param ind String with indentation pattern (default = ""). 20-- @param pre String with indentation prefix (default = ""). 21---------------------------------------------------------------------------- 22local function tabledump (tab, outf, ind, pre) 23 local sep_n, sep, _n = ",\n", ", ", "\n" 24 if (not ind) or (ind == "") then ind = ""; sep_n = ", "; _n = "" end 25 if not pre then pre = "" end 26 outf ("{") 27 local p = pre..ind 28 -- prepare list of keys 29 local keys = { boolean = {}, number = {}, string = {} } 30 local total = 0 31 for key in pairs (tab) do 32 total = total + 1 33 local t = type(key) 34 if t == "string" then 35 tinsert (keys.string, key) 36 else 37 keys[t][key] = true 38 end 39 end 40 local many = total > 5 41 if not many then sep_n = sep; _n = " " end 42 outf (_n) 43 -- serialize entries with numeric keys 44 if many then 45 local _f,_s,_v = ipairs(tab) 46 if _f(_s,_v) then outf (p) end 47 end 48 local num = keys.number 49 local ok = false 50 -- entries with automatic index 51 for key, val in ipairs (tab) do 52 value (val, outf, ind, p) 53 outf (sep) 54 num[key] = nil 55 ok = true 56 end 57 if ok and many then outf (_n) end 58 -- entries with explicit index 59 for key in pairs (num) do 60 if many then outf (p) end 61 outf ("[") 62 outf (key) 63 outf ("] = ") 64 value (tab[key], outf, ind, p) 65 outf (sep_n) 66 end 67 -- serialize entries with boolean keys 68 local tr = keys.boolean[true] 69 if tr then 70 outf (format ("%s[true] = ", many and p or '')) 71 value (tab[true], outf, ind, p) 72 outf (sep_n) 73 end 74 local fa = keys.boolean[false] 75 if fa then 76 outf (format ("%s[false] = ", many and p or '')) 77 value (tab[false], outf, ind, p) 78 outf (sep_n) 79 end 80 -- serialize entries with string keys 81 sort (keys.string) 82 for _, key in ipairs (keys.string) do 83 outf (format ("%s[%q] = ", many and p or '', key)) 84 value (tab[key], outf, ind, p) 85 outf (sep_n) 86 end 87 if many then outf (pre) end 88 outf ("}") 89end 90 91 92-- 93-- Serializes a value. 94-- 95value = function (v, outf, ind, pre) 96 local t = type (v) 97 if t == "string" then 98 outf (format ("%q", v)) 99 elseif t == "number" then 100 outf (tostring(v)) 101 elseif t == "boolean" then 102 outf (tostring(v)) 103 elseif t == "table" then 104 tabledump (v, outf, ind, pre) 105 else 106 outf (format ("%q", tostring(v))) 107 end 108end 109 110---------------------------------------------------------------------------- 111serialize = tabledump 112 113return tabledump