Save number, string, boolean, table of any depth - in text; functions - in "Lua byte-code".
Don't save userdata, thread, nil.
Search output file in LOVE/yourProgramName/appData (see http://www.love2d.org/wiki/love.filesystem)
Output file "savedTable.lua" (from this example ):
tag: lua serialization.
save lua-table to lua-script
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- AntonioModer
- Party member
- Posts: 202
- Joined: Fri Jun 15, 2012 5:31 pm
- Location: Belarus
- Contact:
save lua-table to lua-script
Last edited by AntonioModer on Mon Aug 17, 2015 1:08 pm, edited 9 times in total.
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: save lua-table in lua-script
This has a number of problems:
- It can't handle string values with embedded " characters.
- It can't handle table keys at all.
- String keys are handled wrong: keywords and non-identifier string keys need to use the ["key"] = syntax, not the key = syntax.
- If you have a table value, it always treats the key as an identifier, so {{}, {}} becomes:
Which is obviously wrong.
Code: Select all
return { 1 = { }, 2 = { }, }
- It can't deal with cyclic tables.
- I may have missed a few issues.
Help us help you: attach a .love.
- AntonioModer
- Party member
- Posts: 202
- Joined: Fri Jun 15, 2012 5:31 pm
- Location: Belarus
- Contact:
Re: save lua-table in lua-script
Thanks !Robin wrote:This has a number of problems: ...
- Centauri Soldier
- Prole
- Posts: 42
- Joined: Mon May 21, 2012 6:38 am
Re: save lua-table in lua-script
Here's a link to my recursive table.tostring function. It handles everything except userdata.
https://github.com/CentauriSoldier/Open ... /table.lua
https://github.com/CentauriSoldier/Open ... /table.lua
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: save lua-table in lua-script
Since I'm being nitpicky:Centauri Soldier wrote:Here's a link to my recursive table.tostring function. It handles everything except userdata.
https://github.com/CentauriSoldier/Open ... /table.lua
- It is very Windows-centric:
- It doesn't escape \r and \n separately, which results in plain invalid Lua.
- The output lines are separated by \r\n as well. I suggest using just \n there and opening the file in 'w' instead of 'wb'.
- Try {["007"] = "somestring"}. The output will be
To fix that use string.find(vIndex, '[a-zA-Z_][a-zA-Z0-9_]*') instead of string.find(vIndex, '%W', 1)
Code: Select all
{ 007 = "somestring", }
- The indentation of your code is fucked up in many places.
- Numeric keys have the indentation of their parent, instead of that of their string siblings.
- No support for keys that are not numbers or strings.
Help us help you: attach a .love.
- Centauri Soldier
- Prole
- Posts: 42
- Joined: Mon May 21, 2012 6:38 am
Re: save lua-table in lua-script
Thank you for you suggestions, Robin, I'll look into those problems.
BTW, aren't all keys in a lua table either numbers or strings?
BTW, aren't all keys in a lua table either numbers or strings?
- slime
- Solid Snayke
- Posts: 3132
- Joined: Mon Aug 23, 2010 6:45 am
- Location: Nova Scotia, Canada
- Contact:
Re: save lua-table in lua-script
Keys can be anything - numbers, strings, booleans, functions, userdata, or tables.Centauri Soldier wrote:BTW, aren't all keys in a lua table either numbers or strings?
- Centauri Soldier
- Prole
- Posts: 42
- Joined: Mon May 21, 2012 6:38 am
Re: save lua-table in lua-script
I've been programming in lua for years and didn't know that, thanks guys.
Also, Robin, your string search didn't work either so I just wrapped all string and number indices in quotes for simplicity.
Well, try that then, see how it suits you now.
Thanks for all the feedback , keep it coming.
Tested this and it worked.
Also, Robin, your string search didn't work either so I just wrapped all string and number indices in quotes for simplicity.
Well, try that then, see how it suits you now.
Thanks for all the feedback , keep it coming.
Tested this and it worked.
Code: Select all
local tTest = {
SomeKey = "sdfkljsdf",
["My Sub Table"] = {"a","b","c","d","e","f","g"},
["MySecondSubTable"] = {
[1] = {
[1] = 1,
[2] = 1,
[3] = 1,
[4] = 1,
[5] = 1,
[6] = 1,
},
[2] = "o",
[3] = love.graphics.getHeight,
[4] = {"q","r","s","t",},
[5] = 3,
[6] = "poi",
},
["007"] = "Bond",
[true]="This is a second type of key",
[false]="This is a false key",
[love.graphics.getWidth] = {},
[{[1]=5}] = "1",
};
table.dump(love.filesystem.getSaveDirectory().."/test.lua", tTest, false, true);
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: save lua-table in lua-script
Hm, odd. I'd swear that would work. Anyway, yeah, that works.Centauri Soldier wrote:Also, Robin, your string search didn't work either so I just wrapped all string and number indices in quotes for simplicity.
Help us help you: attach a .love.
- xXxMoNkEyMaNxXx
- Party member
- Posts: 206
- Joined: Thu Jan 10, 2013 6:16 am
- Location: Canada
Re: save lua-table in lua-script
I'm eager for criticism on my table serializing function!
Code: Select all
local next=next
local type=type
local tostring=tostring
local format=string.format
local sort=table.sort
local concat=table.concat
local function serialize(t)
local TYPE=type(t)
if TYPE=="boolean" or TYPE=="number" then
return tostring(t)
elseif TYPE=="string" then
return format("%q",t)
elseif TYPE=="table" then
local ret={}
local r_v={}
local n=0
for i,v in next,t do
local sv=serialize(v)
ret[#ret+1]="["..serialize(i).."]="..sv
r_v[i]=sv
n=n+1
end
if n==#t then
return "{"..concat(r_v,",").."}"
else
sort(ret)
return "{"..concat(ret,",").."}"
end
else
return "&"..TYPE.."="..format("%q",tostring(t))
end
end
Who is online
Users browsing this forum: No registered users and 118 guests