JSON+ de/serializer in pure lua
This folder defines an LSON spec and an example public domain pure-lua
implementation of LSON/JSON encoder and decoder.
Lua Library
lson is a pure-lua JSON+ de/serialization library. The main functions are
the encoding functions
json() and
lson() and the
decode() function
(which decodes either encoder's output).
See also the types
#lson.Json,
#lson.Lson and
#lson.Decoder and
their documentation to have more control of the usage, including pretty printing
and a decoding iterator.
Note: If you want highly performant or compliant JSON library then use a
different one. This library is part of the civlua project and is optimized for
simplicy and improved features.
LSON is a strict superset of JSON: all valid JSON is valid LSON but not vice
versa. The difference is:
- LSON ignores commas (it treats them as whitespace). List and map entries can
be separated by any whitespace (including commas). This makes it easier for
a human to write and vcs to diff.
- LSON adds binary syntax for bytes |any binary data|. The data within
- |...| is arbitrary bytes except the following will be converted:
\\ -> \ \| -> | \n -> newline
Other "escapes" are preserved, i.e. \d -> \d. This makes encoding binary
data in LSON MUCH more compact (and possible) than JSON.
Types: Json Lson De
Functions
Json Encoder (via fmt.Fmt)
This works identically to metaty.Fmt except it overrides
how tables are formatted to use JSON instead of printing them.
Fields:
Methods
- fn toPretty(f)
- fn pretty(E, t)
- fn:level(add) -> int: current level
Add to the indent level and get the new value
call with add=nil to just get the current level
- fn:write(...)
Same as file:write.
- fn:flush()
- fn:close()
- fn styled(f, style, text, ...)
Call to:styled(...) if it is enabled, else simply f:write(text, ...).
This allows for configurable styling of output, both for objects as well
as command-line utilities/etc.
- fn tableKey(f, v, podder, pod)
- fn nil(f)
format a nil value.
- fn boolean(f, b)
format a boolean value.
- fn number(f, n)
format a number value.
- fn string(enc, s)
- fn thread(f, th)
format a thread value.
- fn userdata(f, ud)
format a userdata value.
- fn function(f, fn)
format a function value.
- fn items(f, t, hasKeys, listEnd)
format items in table "list"
- fn keyvals(f, t, keys)
format key/vals in table "map".
- fn list(f, t)
Format only the list-elements of a table.
- fn rawtable(f, t)
- fn table(f, t)
- fn format(f, fmt, ...) -> varargsUsed
like string.format but use Fmt for %q.
Doesn't return the string, instead writes to Fmt
- fn concat(f, sep, ...) -> f
fmt ... separated by sep
- fn tabulated(f, ...) -> f:concat('\t', ...)
fmt ... separated by tabs
- fn lined(f, ...) -> f:concat('\n', ...)
fmt ... separated by newlines
- fn tostring
Similar to JSON but no commas and strings are encoded as
|bytes|
Fields:
Methods
- fn toPretty(f)
- fn pretty(E, t)
- fn:level(add) -> int: current level
Add to the indent level and get the new value
call with add=nil to just get the current level
- fn:write(...)
Same as file:write.
- fn:flush()
- fn:close()
- fn styled(f, style, text, ...)
Call to:styled(...) if it is enabled, else simply f:write(text, ...).
This allows for configurable styling of output, both for objects as well
as command-line utilities/etc.
- fn tableKey(f, v, podder, pod)
- fn nil(f)
format a nil value.
- fn boolean(f, b)
format a boolean value.
- fn number(f, n)
format a number value.
- fn string(f, s)
- fn thread(f, th)
format a thread value.
- fn userdata(f, ud)
format a userdata value.
- fn function(f, fn)
format a function value.
- fn items(f, t, hasKeys, listEnd)
format items in table "list"
- fn keyvals(f, t, keys)
format key/vals in table "map".
- fn list(f, t)
Format only the list-elements of a table.
- fn rawtable(f, t)
- fn table(f, t)
- fn format(f, fmt, ...) -> varargsUsed
like string.format but use Fmt for %q.
Doesn't return the string, instead writes to Fmt
- fn concat(f, sep, ...) -> f
fmt ... separated by sep
- fn tabulated(f, ...) -> f:concat('\t', ...)
fmt ... separated by tabs
- fn lined(f, ...) -> f:concat('\n', ...)
fmt ... separated by newlines
- fn tostring
De(string or lines) -> value-iter
for val in De'["my", "lson"' do ... end