Save/Load Table to and from a string

This is a simple set of functions that I found online and added to. This will convert a table to a string and load that string back into a table. This is useful for saving tables as Global,Project,Local data. This is the simplest table to string set of functions I have found. Works well and I have used it in a lot of my projects.

table.tostring(tbl) – converts the table into a string and returns a string

string.totable(str) returns a table from the given string
str:totable() returns a table from the string using self.


--# HelperFunctions
-----------------------------
-----Table to string
-----------------------------
    
function table.val_to_str ( v )
  if "string" == type( v ) then
    v = string.gsub( v, "\
", "\\\
" )
    if string.match( string.gsub(v,"[^'\"]",""), '^"+$' ) then
      return "'" .. v .. "'"
    end
    return '"' .. string.gsub(v,'"', '\\\"' ) .. '"'
  else
    return "table" == type( v ) and table.tostring( v ) or
      tostring( v )
  end
end

function table.key_to_str ( k )
  if "string" == type( k ) and string.match( k, "^[_%a][_%a%d]*$" ) then
    return k
  else
    return "[" .. table.val_to_str( k ) .. "]"
  end
end

function table.tostring( tbl )

  local result, done = {}, {}
  for k, v in ipairs( tbl ) do
    table.insert( result, table.val_to_str( v ) )
    done[ k ] = true
  end
  for k, v in pairs( tbl ) do
    if not done[ k ] then
      table.insert( result,
        table.key_to_str( k ) .. "=" .. table.val_to_str( v ) )
    end
  end
  return "{" .. table.concat( result, "," ) .. "}"
end

function string.totable(self)
    
local t = loadstring("return "..self)

return assert(t(),"Can not convert string to table.")
    
end



-------------------------------------------------------------------
--# Main
-- Save Table

-- Use this function to perform your initial setup
function setup()
    tbl1 = {10,11,12,date={Year="2013",Month={"Oct","10"},Day="14"}}
     str = table.tostring(tbl1)
    
    print("String is: "..str)
    
    --string.totable()
    test1 = string.totable(str)
    print("Year is: "..test1.date.Year)
    
    --totable using self
    print("Month is: "..str:totable().date.Month[1].." "..str:totable().date.Month[2])

end


Thanks!
To save it inside a tab, it would be great if the string was broken with a
rougthly every 40 charcters.