save lua-table to lua-script

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
AntonioModer
Party member
Posts: 202
Joined: Fri Jun 15, 2012 5:31 pm
Location: Belarus
Contact:

save lua-table to lua-script

Post by AntonioModer »

Save number, string, boolean, table of any depth - in text; functions - in "Lua byte-code".
Don't save userdata, thread, nil.
main.lua
v 0.62
(6.82 KiB) Downloaded 215 times
Search output file in LOVE/yourProgramName/appData (see http://www.love2d.org/wiki/love.filesystem)
Output file "savedTable.lua" (from this example ):
savedTable.lua
example output
(1.83 KiB) Downloaded 207 times
tag: lua serialization.
Last edited by AntonioModer on Mon Aug 17, 2015 1:08 pm, edited 9 times in total.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: save lua-table in lua-script

Post by Robin »

This has a number of problems:
  1. It can't handle string values with embedded " characters.
  2. It can't handle table keys at all.
  3. String keys are handled wrong: keywords and non-identifier string keys need to use the ["key"] = syntax, not the key = syntax.
  4. If you have a table value, it always treats the key as an identifier, so {{}, {}} becomes:

    Code: Select all

    return
    {
    1 = {
        },
    2 = {
        },
    }
    Which is obviously wrong.
  5. It can't deal with cyclic tables.
  6. I may have missed a few issues.
Help us help you: attach a .love.
User avatar
Centauri Soldier
Prole
Posts: 42
Joined: Mon May 21, 2012 6:38 am

Re: save lua-table in lua-script

Post by Centauri Soldier »

Here's a link to my recursive table.tostring function. It handles everything except userdata.

https://github.com/CentauriSoldier/Open ... /table.lua
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: save lua-table in lua-script

Post by Robin »

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
Since I'm being nitpicky:
  1. 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'.
  2. Try {["007"] = "somestring"}. The output will be

    Code: Select all

    {
        007 = "somestring",
    }
    To fix that use string.find(vIndex, '[a-zA-Z_][a-zA-Z0-9_]*') instead of string.find(vIndex, '%W', 1)
  3. The indentation of your code is fucked up in many places.
  4. Numeric keys have the indentation of their parent, instead of that of their string siblings.
  5. No support for keys that are not numbers or strings.
Help us help you: attach a .love.
User avatar
Centauri Soldier
Prole
Posts: 42
Joined: Mon May 21, 2012 6:38 am

Re: save lua-table in lua-script

Post by Centauri Soldier »

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?
User avatar
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

Post by slime »

Centauri Soldier wrote:BTW, aren't all keys in a lua table either numbers or strings?
Keys can be anything - numbers, strings, booleans, functions, userdata, or tables.
User avatar
Centauri Soldier
Prole
Posts: 42
Joined: Mon May 21, 2012 6:38 am

Re: save lua-table in lua-script

Post by Centauri Soldier »

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 :D, 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);
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: save lua-table in lua-script

Post by Robin »

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.
Hm, odd. I'd swear that would work. Anyway, yeah, that works. :)
Help us help you: attach a .love.
User avatar
xXxMoNkEyMaNxXx
Party member
Posts: 206
Joined: Thu Jan 10, 2013 6:16 am
Location: Canada

Re: save lua-table in lua-script

Post by xXxMoNkEyMaNxXx »

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
Post Reply

Who is online

Users browsing this forum: No registered users and 118 guests