Serpent
Lua serializer and pretty printer.
Install / Use
/learn @pkulchenko/SerpentREADME
Serpent
Lua serializer and pretty printer.
Features
- Human readable:
- Provides single-line and multi-line output.
- Nested tables are properly indented in the multi-line output.
- Numerical keys are listed first.
- Keys are (optionally) sorted alphanumerically.
- Array part skips keys (
{'a', 'b'}instead of{[1] = 'a', [2] = 'b'}). nilvalues are included when expected ({1, nil, 3}instead of{1, [3]=3}).- Keys use short notation (
{foo = 'foo'}instead of{['foo'] = 'foo'}). - Shared references and self-references are marked in the output.
- Machine readable: provides reliable deserialization using
loadstring(). - Supports deeply nested tables.
- Supports tables with self-references.
- Keeps shared tables and functions shared after de/serialization.
- Supports function serialization using
string.dump(). - Supports serialization of global functions.
- Supports
__tostringand__serializemetamethods. - Escapes new-line
\010and end-of-file control\026characters in strings. - Configurable with options and custom formatters.
Usage
local serpent = require("serpent")
local a = {1, nil, 3, x=1, ['true'] = 2, [not true]=3}
a[a] = a -- self-reference with a table as key and value
print(serpent.dump(a)) -- full serialization
print(serpent.line(a)) -- single line, no self-ref section
print(serpent.block(a)) -- multi-line indented, no self-ref section
local fun, err = loadstring(serpent.dump(a))
if err then error(err) end
local copy = fun()
-- or using serpent.load:
local ok, copy = serpent.load(serpent.dump(a))
print(ok and copy[3] == a[3])
Functions
Serpent provides three functions that are shortcuts to the same internal function, but set different options by default:
dump(a[, {...}])-- full serialization; setsname,compactandsparseoptions;line(a[, {...}])-- single line pretty printing, no self-ref section; setssortkeysandcommentoptions;block(a[, {...}])-- multi-line indented pretty printing, no self-ref section; setsindent,sortkeys, andcommentoptions.
Note that line and block functions return pretty-printed data structures and if you want to deserialize them, you need to add return before running them through loadstring.
For example: loadstring('return '..require('mobdebug').line("foo"))() == "foo".
While you can use loadstring or load functions to load serialized fragments, Serpent also provides load function that adds safety checks and reports an error if there is any executable code in the fragment.
ok, res = serpent.load(str[, {safe = true}])-- loads serialized fragment; you need to pass{safe = false}as the second value if you want to turn safety checks off.
Similar to pcall and loadstring calls, load returns status as the first value and the result or the error message as the second value.
Options
- indent (string) -- indentation; triggers long multi-line output.
- comment (true/false/maxlevel) -- provide stringified value in a comment (up to
maxlevelof depth). - sortkeys (true/false/function) -- sort keys.
- sparse (true/false) -- force sparse encoding (no nil filling based on
#t). - compact (true/false) -- remove spaces.
- fatal (true/False) -- raise fatal error on non-serilizable values.
- fixradix (true/False) -- change radix character set depending on locale to decimal dot.
- nocode (true/False) -- disable bytecode serialization for easy comparison.
- nohuge (true/False) -- disable checking numbers against undefined and huge values.
- maxlevel (number) -- specify max level up to which to expand nested tables.
- maxnum (number) -- specify max number of elements in a table.
- maxlength (number) -- specify max length for all table elements.
- metatostring (True/false) -- use
__tostringmetamethod when serializing tables (v0.29); set tofalseto disable and serialize the table as is, even when__tostringis present. - numformat (string; "%.17g") -- specify format for numeric values as shortest possible round-trippable double (v0.30). Use "%.16g" for better readability and "%.17g" (the default value) to preserve floating point precision.
- valignore (table) -- allows to specify a list of values to ignore (as keys).
- keyallow (table) -- allows to specify the list of keys to be serialized. Any keys not in this list are not included in final output (as keys).
- keyignore (table) -- allows to specity the list of keys to ignore in serialization.
- valtypeignore (table) -- allows to specify a list of value types to ignore (as keys).
- custom (function) -- provide custom output for tables.
- name (string) -- name; triggers full serialization with self-ref section.
These options can be provided as a second parameter to Serpent functions.
block(a, {fatal = true})
line(a, {nocode = true, valignore = {[arrayToIgnore] = true}})
function todiff(a) return dump(a, {nocode = true, indent = ' '}) end
Serpent functions set these options to different default values:
dumpsetscompactandsparsetotrue;linesetssortkeysandcommenttotrue;blocksetssortkeysandcommenttotrueandindentto' '.
Metatables with __tostring and __serialize methods
If a table or a userdata value has __tostring or __serialize method, the method will be used to serialize the value.
If __serialize method is present, it will be called with the value as a parameter.
if __serialize method is not present, but __tostring is, then tostring will be called with the value as a parameter.
In both cases, the result will be serialized, so __serialize method can return a table, that will be serialize and replace the original value.
Sorting
A custom sort function can be provided to sort the contents of tables. The function takes 2 parameters, the first being the table (a list) with the keys, the second the original table. It should modify the first table in-place, and return nothing. For example, the following call will apply a sort function identical to the standard sort, except that it will not distinguish between lower- and uppercase.
local mysort = function(k, o) -- k=keys, o=original table
local maxn, to = 12, {number = 'a', string = 'b'}
local function padnum(d) return ("%0"..maxn.."d"):format(d) end
local sort = function(a,b)
-- this -vvvvvvvvvv- is needed to sort array keys first
return ((k[a] and 0 or to[type(a)] or 'z')..(tostring(a):gsub("%d+",padnum))):upper()
< ((k[b] and 0 or to[type(b)] or 'z')..(tostring(b):gsub("%d+",padnum))):upper()
end
table.sort(k, sort)
end
local content = { some = 1, input = 2, To = 3, serialize = 4 }
local result = require('serpent').block(content, {sortkeys = mysort})
Formatters
Serpent supports a way to provide a custom formatter that allows to fully customize the output. The formatter takes four values:
- tag -- the name of the current element with '=' or an empty string in case of array index,
- head -- an opening table bracket
{and associated indentation and newline (if any), - body -- table elements concatenated into a string using commas and indentation/newlines (if any),
- tail -- a closing table bracket
}and associated indentation and newline (if any), and - level -- the current level.
For example, the following call will apply `Foo{bar} notation to its output (used by Metalua to display ASTs):
print((require "serpent").block(ast, {comment = false, custom =
function(tag,head,body,tail)
local out = head..body..tail
if tag:find('^lineinfo') then
out = out:gsub("\n%s+", "") -- collapse lineinfo to one line
elseif tag == '' then
body = body:gsub('%s*lineinfo = [^\n]+', '')
local _,_,atag = body:find('tag = "(%w+)"%s*$')
if atag then
out = "`"..atag..head.. body:gsub('%s*tag = "%w+"%s*$', '')..tail
out = out:gsub("\n%s+", ""):gsub(",}","}")
else out = head..body..tail end
end
return tag..out
end}))
Limitations
- Doesn't handle userdata (except filehandles in
io.*table). - Threads, function upvalues/environments, and metatables are not serialized.
Performance
A simple performance test against serialize.lua from metalua, pretty.write
from Penlight, and tserialize.lua from lua-nucleo is included in t/bench.lua.
These are the results from one of the runs:
- nucleo (1000): 0.256s
- metalua (1000): 0.177s
- serpent (1000): 0.22s
- serpent (1000): 0.161s -- no comments, no string escapes, no math.huge check
- penlight (1000): 0.132s
Serpent does additional processing to escape \010 and \026 characters in
strings (to address http://lua-users.org/lists/lua-l/2007-07/msg00362.html,
which is already fixed in Lua 5.2) and to check all numbers for math.huge.
The seconds number excludes this processing to put it on an equal footing
with other modules that skip these checks (nucleo still checks for math.huge).
Author
Paul Kulchenko (paul@kulchenko.com)
License
See LICENSE file.
History
v0.30 (Sep 01 2017)
- Updated
pairsto avoid using__pairsin Lua 5.2+. - Added
metatostringoption to disable__tostringprocessing during serialization. - Added
levelto the parameters of the custom function (closes #25). - Added
maxlengthoption to limit the space taken by table elements. - Optimized serialization of functions when
nocodeoption is specified. - Added protection from
__serializecheck failing (pkulchenko/ZeroBraneStudio#732). - Added
keyignoreoption for the serializer. - Added check for environments that may not have 'default' tables as tables.
- Added numeric format to preserve floating point precision (closes #17).
- Added using
debug.metatablewhen available. - Improved handling of failures in
__tostring(pkulchenko/ZeroBraneStudio#446).
v0.28 (May 06 2015)
- Switched to a method proposed by @SoniEx2 to disallow function
Related Skills
node-connect
347.0kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
107.8kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
347.0kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
347.0kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
