Page 1 of 1

table to lua file

Posted: Wed Jun 07, 2023 8:39 am
by Rigachupe
Ok here is the thing. I thought about using json and other types of serialization of a typical table that i needed to be stored as data table only (this means no functions, no special data or pointers on images etc) into a .lua file. I looked at the serialization libraries and they were huge around 10kB.

So this is the smallest version i share here. Store it into utils.lua or copy into your own library path.

Code: Select all

local Utils={}

function Utils.serialize(o,str,indent)
	indent=indent or 1
	local t=type(o)
	if t=="function" then
		-- skip
	elseif t=="boolean" then
		str=str..tostring(o)..",\n"
	elseif t=="number" then
		str=str..o..",\n"
	elseif t=="string" then
		str=str..string.format("%q",o)..",\n"
	elseif t=="table" then
		if indent==1 then
			str="return {\n"
		else
			str=str.."{\n"
		end
		local spaces=string.rep("  ",indent)
		for k,v in pairs(o) do
			local tv=type(v)
			if tv~="function" then
				local tk=type(k)
				if tk=="string" then
					str=str..spaces..k.."="
				elseif tk=="number" then
					str=str..spaces.."["..tostring(k).."]="
				end
				str=Utils.serialize(v,str,indent+1)
			end
		end
		if indent>1 then
			local espaces=string.rep("  ",indent-1)
			str=str..espaces.."},\n"
		else
			str=str.."}\n"
		end
	else
		error("cannot serialize a "..tostring(t))
	end
	
	return str
end

return Utils
Then use it this way:

Code: Select all

local Utils=require("src/utils")

-- convert table tbl1 to string str
local tbl1={width=150,height=160,stats={str=15,dex=18,con=10},hitpoints=50,items={[1]="apple",[2]="corn"},}
local str=Utils.serialize(tbl1)
print(str)

-- write string of tbl1 to tbl1.lua in love 2D save folder in appdata
love.filesystem.write("tbl1.lua",str)

-- get the lua code for tbl1 from tbl1.lua file as a pointer chunk
local chunk=love.filesystem.load("tbl1.lua")

-- now if you call it like this you get back your stored table. then check it in debugger to see if it is correctly restored
local tbl=chunk()
Hope it helps you if you for some reason do not want to use json or xml to store table data per file.

Re: table to lua file

Posted: Wed Jun 07, 2023 10:53 am
by GVovkiv
Rigachupe wrote: Wed Jun 07, 2023 8:39 am I looked at the serialization libraries and they were huge around 10kB.
You could also try to minify this libraries, if size limits is tight

Re: table to lua file

Posted: Wed Jun 07, 2023 11:16 am
by Rigachupe
The worst problem i encounterd with a library that uses json is this.
You convert a table to a json string. It could be big, that means it will be slower to get that string the more size the table has.
Then you store it on disk as json. That is cool and i do not say "don't do it". If you need json or xml or txt do it that way.
But storing data in lua as table in a .lua file looks more promising and a lot of faster.

Re: table to lua file

Posted: Wed Jun 07, 2023 11:18 am
by pgimeno
It may cover a few use cases, but it's not general enough. Neither of these entries is correctly serialized:

Code: Select all

local ser = require('serial')
print(ser.serialize({["It's raning"] = true, [{}] = true, [false] = false}))
But does the world really need yet another serialize-to-Lua library?

Re: table to lua file

Posted: Wed Jun 07, 2023 12:01 pm
by Rigachupe
pgimeno wrote: Wed Jun 07, 2023 11:18 am Does the world need another ... library?
Yes. Then choose. Or kill it. Either way, the results may vary. We only can compare speed of the solution. Not the mass of the code included and the energy spend in development of such code. That also includes the food one need to chew down the pipe to achieve such results.

Around the tables please try it the normal way. Not the immortal highlander way please. :) It small because it is for the basic tables like this:

Code: Select all

{
[123]="abc",
["afg"]="589,
[12]={dog="waffles",age=13,size="-----"},
}
Just data. No pointers, no functions there to store them in .lua file. The index is number or string based. No strange special cases. A table can contain another table as a structured record. But no looping the rope around the neck or some other kind of trap style coding involved due to enormous strain on the brain and body caused by anomalies like black hole compression algorythm that suprassed even 7zip year wonders.

Re: table to lua file

Posted: Wed Jun 07, 2023 4:10 pm
by tourgen
pretty nice. seems to work for my needs. I wrote something similar but it didn't work with strings all that well. your solution for strings is simple and seems to work alright.

Re: table to lua file

Posted: Thu Jun 08, 2023 2:23 pm
by Rigachupe
tourgen wrote: Wed Jun 07, 2023 4:10 pm pretty nice. seems to work for my needs. I wrote something similar but it didn't work with strings all that well. your solution for strings is simple and seems to work alright.
Glad it helps you to develop your app further.

Re: table to lua file

Posted: Fri Jun 09, 2023 2:00 pm
by dusoft
pgimeno wrote: Wed Jun 07, 2023 11:18 am It may cover a few use cases, but it's not general enough. Neither of these entries is correctly serialized:

Code: Select all

local ser = require('serial')
print(ser.serialize({["It's raning"] = true, [{}] = true, [false] = false}))
But does the world really need yet another serialize-to-Lua library?
And that's why you don't create another library with lots of untested edge cases that will fail. (response to the op)
10 Kb is too much these days? I mean you can always simplify and throw out parts of existing code you don't use instead of making your own.

e.g.:
https://github.com/rxi/json.lua

Re: table to lua file

Posted: Fri Jun 09, 2023 3:26 pm
by Rigachupe
dusoft wrote: Fri Jun 09, 2023 2:00 pm
pgimeno wrote: Wed Jun 07, 2023 11:18 am It may cover a few use cases, but it's not general enough. Neither of these entries is correctly serialized:

Code: Select all

local ser = require('serial')
print(ser.serialize({["It's raning"] = true, [{}] = true, [false] = false}))
But does the world really need yet another serialize-to-Lua library?
And that's why you don't create another library with lots of untested edge cases that will fail. (response to the op)
10 Kb is too much these days? I mean you can always simplify and throw out parts of existing code you don't use instead of making your own.

e.g.:
https://github.com/rxi/json.lua
This is about im not gonna use json because i prefer .lua file with a table inside. I just want to make it clear. The one json you pointed at in the link i personally used on my project because it is small and nice. But this time json in another topic to not clutter this thread.

Re: table to lua file

Posted: Fri Jun 09, 2023 8:52 pm
by dusoft
OK, understood.