2012-06-04 05:34:39 +00:00
|
|
|
# Serpent
|
2012-06-02 06:11:32 +00:00
|
|
|
|
2012-06-04 05:34:39 +00:00
|
|
|
Lua serializer and pretty printer.
|
|
|
|
|
|
|
|
## Features
|
|
|
|
|
|
|
|
* Human readable:
|
2012-06-04 05:52:02 +00:00
|
|
|
* Provides single-line and multi-line output.
|
|
|
|
* Nested tables are properly indented in the multi-line output.
|
|
|
|
* Numerical keys are listed first.
|
2012-06-13 06:17:44 +00:00
|
|
|
* Keys are (optionally) sorted alphanumerically.
|
2012-06-04 05:53:42 +00:00
|
|
|
* Array part skips keys (`{'a', 'b'}` instead of `{[1] = 'a', [2] = 'b'}`).
|
|
|
|
* `nil` values are included when expected (`{1, nil, 3}` instead of `{1, [3]=3}`).
|
|
|
|
* Keys use short notation (`{foo = 'foo'}` instead of `{['foo'] = 'foo'}`).
|
2012-06-13 06:17:44 +00:00
|
|
|
* Shared references and self-references are marked in the output.
|
2012-06-04 05:34:39 +00:00
|
|
|
* Machine readable: provides reliable deserialization using `loadstring()`.
|
|
|
|
* Supports deeply nested tables.
|
|
|
|
* Supports tables with self-references.
|
|
|
|
* Shared tables and functions stay shared after de/serialization.
|
|
|
|
* Supports function serialization using `string.dump()`.
|
|
|
|
* Supports serialization of global functions.
|
2012-06-04 05:53:42 +00:00
|
|
|
* Escapes new-line `\010` and end-of-file control `\026` characters in strings.
|
2012-06-13 06:17:44 +00:00
|
|
|
* Configurable with options and custom formatters.
|
2012-06-04 05:34:39 +00:00
|
|
|
|
|
|
|
## Usage
|
|
|
|
|
2012-06-06 03:09:41 +00:00
|
|
|
```lua
|
2012-06-04 05:34:39 +00:00
|
|
|
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
|
|
|
|
|
2012-06-13 06:17:44 +00:00
|
|
|
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
|
2012-06-04 05:34:39 +00:00
|
|
|
|
2012-06-13 06:17:44 +00:00
|
|
|
local fun, err = loadstring(serpent.dump(a))
|
2012-06-04 05:34:39 +00:00
|
|
|
if err then error(err) end
|
|
|
|
local copy = fun()
|
2012-06-13 06:17:44 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
## Functions
|
|
|
|
|
|
|
|
Serpent provides three functions that are shortcuts to the same
|
|
|
|
internal function, but set different options by default:
|
|
|
|
|
|
|
|
* `dump(a[, {...}])` -- full serialization; sets `name`, `compact` and `sparse` options
|
|
|
|
* `line(a[, {...}])` -- single line, no self-ref section; sets `sortkeys` and `comment` options
|
|
|
|
* `block(a[, {...}])` -- multi-line indented, no self-ref section; sets `indent`, `sortkeys`, and `comment` options
|
|
|
|
|
|
|
|
## Options
|
|
|
|
|
|
|
|
* name (string) -- name; triggers full serialization with self-ref section
|
|
|
|
* indent (string) -- indentation; triggers long multi-line output
|
2012-06-18 05:33:07 +00:00
|
|
|
* comment (true/False/maxlevel) -- provide stringified value in a comment (up to maxlevel of depth)
|
2012-06-13 06:17:44 +00:00
|
|
|
* sortkeys (true/False) -- 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
|
|
|
|
* nocode (true/False) -- disable bytecode serialization for easy comparison
|
|
|
|
* nohuge (true/False) -- disable checking numbers against undefined and huge values
|
2012-06-13 16:50:26 +00:00
|
|
|
* maxlevel (number) -- specify max level up to which to expand nested tables
|
2012-06-17 02:43:15 +00:00
|
|
|
* ignore (table) -- allows to specify a list of values to ignore (as keys)
|
2012-06-13 06:17:44 +00:00
|
|
|
* custom (function) -- provide custom output for tables
|
|
|
|
|
|
|
|
These options can be provided as a second parameter to Serpent functions.
|
|
|
|
|
|
|
|
```lua
|
|
|
|
block(a, {fatal = true})
|
2012-06-17 02:43:15 +00:00
|
|
|
line(a, {nocode = true, ignore = {[arrayToIgnore] = true}})
|
2012-06-13 06:17:44 +00:00
|
|
|
function todiff(a) return dump(a, {nocode = true, indent = ' '}) end
|
|
|
|
```
|
|
|
|
|
|
|
|
## Formatters
|
|
|
|
|
|
|
|
Serpent supports a way to provide a custom formatter that allows to fully
|
|
|
|
customize the output. For example, the following call will apply
|
|
|
|
`Foo{bar} notation to its output (used by Metalua to display ASTs):
|
2012-06-04 05:34:39 +00:00
|
|
|
|
2012-06-13 06:17:44 +00:00
|
|
|
```lua
|
|
|
|
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}))
|
2012-06-04 05:34:39 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
## 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
|
2012-06-04 05:52:02 +00:00
|
|
|
with other modules that skip these checks (`nucleo` still checks for `math.huge`).
|
2012-06-04 05:34:39 +00:00
|
|
|
|
|
|
|
## Author
|
|
|
|
|
|
|
|
Paul Kulchenko (paul@kulchenko.com)
|
|
|
|
|
|
|
|
## License
|
|
|
|
|
|
|
|
See LICENSE file.
|
2012-06-13 06:17:44 +00:00
|
|
|
|
|
|
|
## History
|
|
|
|
|
2012-11-17 05:21:42 +00:00
|
|
|
Nov 16 2012 v0.19
|
|
|
|
- Fixed an issue with serializing shared functions as keys.
|
|
|
|
- Added serialization of metatables using __tostring (when present).
|
|
|
|
|
2012-09-14 01:38:52 +00:00
|
|
|
Sep 13 2012 v0.18
|
|
|
|
- Fixed an issue with serializing data structures with circular references that require emitting temporary variables.
|
|
|
|
- Fixed an issue with serializing keys pointing to shared references.
|
|
|
|
- Improved overall serialization logic to inline values when possible.
|
|
|
|
|
2012-09-12 22:18:50 +00:00
|
|
|
Sep 12 2012 v0.17
|
|
|
|
- Fixed an issue with serializing userdata that doesn't provide tostring().
|
|
|
|
|
2012-08-29 04:42:28 +00:00
|
|
|
Aug 28 2012 v0.16
|
|
|
|
- Removed confusing --[[err]] comment from serialized results.
|
|
|
|
- Added a short comment to serialized functions when the body is skipped.
|
|
|
|
|
2012-06-18 05:33:07 +00:00
|
|
|
Jun 17 2012 v0.15
|
|
|
|
- Added `ignore` option to allow ignoring table values.
|
|
|
|
- Added `comment=num` option to set the max level up to which add comments.
|
|
|
|
- Changed all comments (except math.huge) to be controlled by `comment` option.
|
|
|
|
|
2012-06-13 18:07:59 +00:00
|
|
|
Jun 13 2012 v0.14
|
|
|
|
- Fixed an issue with string keys with numeric values `['3']` getting mixed
|
|
|
|
with real numeric keys (only with `sortkeys` option set to `true`).
|
2012-06-14 04:06:23 +00:00
|
|
|
- Fixed an issue with negative and real value numeric keys being misplaced.
|
2012-06-13 18:07:59 +00:00
|
|
|
|
2012-06-13 17:21:44 +00:00
|
|
|
Jun 13 2012 v0.13
|
2012-06-18 05:33:07 +00:00
|
|
|
- Added `maxlevel` option.
|
2012-06-13 17:21:44 +00:00
|
|
|
- Fixed key sorting such that `true` and `'true'` are always sorted in
|
|
|
|
the same order (for a more stable output).
|
|
|
|
- Removed addresses from names of temporary variables (for stable output).
|
|
|
|
|
2012-06-13 06:17:44 +00:00
|
|
|
Jun 12 2012 v0.12
|
|
|
|
- Added options to configure serialization process.
|
2012-06-18 05:33:07 +00:00
|
|
|
- Added `goto` to the list of keywords for Lua 5.2.
|
2012-06-13 06:17:44 +00:00
|
|
|
- Changed interface to dump/line/block methods.
|
2012-06-18 05:33:07 +00:00
|
|
|
- Changed `math.huge` to 1/0 for better portability.
|
2012-06-13 06:17:44 +00:00
|
|
|
- Replaced \010 with \n for better readability.
|
|
|
|
|
|
|
|
Jun 03 2012 v0.10
|
|
|
|
- First public release.
|