## LuaTable A small library to convert tables back to lua code

RamiLego4Game
Citizen
Posts: 73
Joined: Tue Jun 10, 2014 7:41 pm

### LuaTable A small library to convert tables back to lua code

I have made small library for my game Robotics2D that converts tables to Lua code that can be saved, and Now I released it to public with MIT Licensing.

Since it uses Lua to save the data, It's able to save functions ! by dumping them, But they won't be readable, only loadable, unless if you decompile them.
And moreover you can do some hand written tables with smart data generation..
The bad thing that it's less secure (you are running code) .., So load the files using this Library to sandbox the files.

Gist: https://gist.github.com/RamiLego4Game/f ... f4c65efaaf (Give me a star )

*The documentation is included with the gist
*The library can work with luajit alone

Feel free to give feedback
My works:

ivan
Party member
Posts: 1346
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

### Re: LuaTable A small library to convert tables back to lua code

Hello there, and welcome to Love2D!
I think there may be a few parts in the lib that could be improved.

In order to handle "gaps" in numerically indexed tables, it might be better to write:

Code: Select all

function LuaTable.isArray(table)
local n = 0
for _ in pairs(table) do
n = n + 1
end
return #table == n
end
Also, it's probably safer to write:

Code: Select all

function LuaTable.encode_string(sz)
return string.format("%q", sz)
end
Also, table.concat can be used instead of ".." in order to avoid a lot intermediate strings.
tostring works for nil, boolean and number too so that code could be simplified.

It's a good effort but need a little bit of work, good luck!

RamiLego4Game
Citizen
Posts: 73
Joined: Tue Jun 10, 2014 7:41 pm

### Re: LuaTable A small library to convert tables back to lua code

ivan wrote:In order to handle "gaps" in numerically indexed tables, it might be better to write:

Code: Select all

function LuaTable.isArray(table)
local n = 0
for _ in pairs(table) do
n = n + 1
end
return #table == n
end
Also, it's probably safer to write:

Code: Select all

function LuaTable.encode_string(sz)
return string.format("%q", sz)
end
Good points, thanks for help I'll add them soon !
ivan wrote:Also, table.concat can be used instead of ".." in order to avoid a lot intermediate strings.
I didn't get it..
ivan wrote:tostring works for nil, boolean and number too so that code could be simplified.
I already know, But I want to give the library the ability to add more variables types (And make it possible to customize encoders for some variable types).
My works:

RamiLego4Game
Citizen
Posts: 73
Joined: Tue Jun 10, 2014 7:41 pm

### Re: LuaTable A small library to convert tables back to lua code

New release V1.0.1 2016/08/27, All credit goes for ivan in this update.

Changelog:
1. Fixed a problem in LuaTable.isArray that causes arrays with gaps to encode as normal table.
2. Improved LuaTable.encode_string to use string.format instead of multiline string.
Last edited by RamiLego4Game on Sat Aug 27, 2016 2:56 pm, edited 2 times in total.
My works:

rok
Prole
Posts: 36
Joined: Wed Dec 24, 2014 9:12 am
Location: Slovenia

### Re: LuaTable A small library to convert tables back to lua code

For table.concat you insert all the strings you'd like to combine in a table and do one concatenation at the end instead of many in between using ..

It is a BIG performance booster in case you do lots of string merging. I've tried both in a compression algorithm and using table.concat it was a lot faster.

Centauri Soldier
Prole
Posts: 40
Joined: Mon May 21, 2012 6:38 am

### Re: LuaTable A small library to convert tables back to lua code

Hi there and thanks for sharing your code. I do have a question regarding it. Instead of parsing the table, why not just use loadstring() to bring a table into lua again?

Also, a suggestion if you do use this method, would be to (if already saved) look for meta info for each table when loading it back into lua.

Last bumped by RamiLego4Game on Tue Nov 07, 2017 4:07 pm.

### Who is online

Users browsing this forum: No registered users and 5 guests