(sometimes AI explains it better)
| Created | Updated | Size | License |
|---|---|---|---|
| 2017-05 | 2026-08-31 | < 60 K | LGPL3 |
Function to serialize data in Lua table to string with Lua code that recreates this data.
Lua tables can contain cross-references, so actually it's graph encoder to Lua source code.
Primary objective is serialization graph to source code. Nice output is secondary objective.
t2s = require('serialize_lua_graph')
print(t2s(_G))Encoding options is optional table that can be passed as second argument.
Serializer function supports three encoding styles: minimal,
readable_short and readable_long. Encoding style governs whitespaces
and determines general text layout. Style lives in style string field.
Serializer function supports three behavior flags: use_compact_indices,
use_compact_sequences and omit_tail_delimiter. Behavior flags
govern optional lexical elements emission. They determine what
syntax elements will be present. Behavior flags are boolean fields.
Example:
local g2s = require('serialize_lua_graph')
local Options = { style = 'readable_short', use_compact_sequences = false }
local str = g2s(_G, Options)
print(str)We'll demonstrate their effects on excerpt of _G table printout.
| Style | Output |
|---|---|
minimal |
local T_1={};local T_2={}; |
readable_short |
local T_1 = { }; |
local T_2 = { }; |
|
readable_long |
local T_4 = { |
huge = 1/0, |
|
maxinteger = 9223372036854775807, |
| Behavior flag | Output | Value |
|---|---|---|
use_compact_indices |
['coroutine'] = T_1, |
☐ |
coroutine = T_1, |
☑ | |
use_compact_sequences |
[1] = 'nil', |
☐ |
'nil', |
☑ | |
omit_tail_delimiter |
['utf8'] = T_8, }; |
☐ |
['utf8'] = T_8 }; |
☑ |
-
It does not distinguish between -NaN and NaN
Our check for NaN is
n ~= n.So both
-(0/0)and0/0are serialized to string0/0.From our point of view concept "this is not a number, but negative" is gibberish.
-
You can hit limit of Lua
local'sFor common subtables we emit something like
local T_2 = {.Lua implementations have limit on number of locals near 200.
So when number of common subtables is over 200 your Lua interpreter won't be able to load code.
We think it's not our problem. From our point of view we're exporting statement with value capture.
-
Functions, threads, userdata and metatables are not serialized
First, it makes no practical sense to serialize functions.
C functions can't be serialized.
For Lua functions you can store their bytecode. But its instructions may use upvalues. We see no practical sense in trouble of retrieving upvalues (which may end up to something unserializeable).
Second, threads (coroutines) and userdata are not serializeable.
Third, metatables.
Our common pattern is that metatables contain functions. Not serializeable.
Even if in your case they contain plain strings/tables we see no practical need in tracking links between base table and metatable and adding AST node to emit
setmetatable.
- Lua 5.3 (or 5.4, 5.5)
- Save file
serialize_lua_graph.luafromdeploy/ - Place it to your Lua workplace for
require()
- Clone repo
- Modify files in
src/
- Clone
workshoprepo - Checkout it to date near "Updated" date from stats plate (at header of this Readme)
- Modify
package.pathinbuilder/create_deploy.luaso it can find your clonedworkshoprepo - Run
builder/rebuild.sh