title: Serialize Lua table, including pure Lua functions?
body: <p>We need to serialize a Lua table that includes strings, numbers, tables, and functions.  There is code in the Programming Lua book to serialize tables that consist of strings, numbers, and tables.  We tried to adapt this function to add the ability to serialize functions.  Specifically, we added a case for type == 'function' and called string.dump on the function:</p>
  
  <pre><code>if type(o) == "number" then
      return tostring(o)
  elseif type(o) == "function" then
      return "loadstring("..string.dump(o)..")"
  else
      -- assume it is a string
      return string.format("%q", o)
  end
  </code></pre>
  
  <p>This injected Lua bytecode into the rest of the plaintext table representation.  That was the closest thing we could come up with.</p>
  
  <p>We don't care if he serialized result is human-readable or not, it just has to be able to work like this:</p>
  
  <pre><code>mytable = [some complicated lua table with functions]
  dump = dump_t(mytable)
  
  ...
  
  loaded_table = load_t(dump)
  </code></pre>
  
  <p>Any ideas?</p>