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.
Note: If you want highly performant or compliant JSON library then use a
different one. This library is part of the civstack 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.
lson JSON+ command and library.
Cmd usage: lson path/to/file.json
The command pretty-prints JSON+ as a lua object.
Lib usage: tbl = lson.decode('{1: 2}')
As a library it allows de/serialization of JSON+ to/from
Lua values.
Types: Json Lson De
Methods and Functions
- fn json(v, pretty) -> string
Encode lua value to JSON string
- fn lson(v, pretty) -> string
Encode lua value to LSON string
- fn decode(s, podder, pset) -> value
Decode JSON/LSON string or lines object to a lua value.
- fn bytes(f, s)
The bytes type-encoder. Encodes as |bytes| instead of "string" for
lson You can set Enc.string = lson.bytes for this behavior (or use the
Lson type).
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()
- fn pretty(T, self)
- 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(style, text, ...)
Call to:styled(...) if it is enabled, else simply self:write(text, ...).
This allows for configurable styling of output, both for objects as well
as command-line utilities/etc.
- fn:tableKey(v, podder, pod)
- fn nil(self)
format a nil value.
- fn:boolean(b)
format a boolean value.
- fn:number(n)
format a number value.
- fn string(f, s)
- fn:thread(th)
format a thread value.
- fn:userdata(ud)
format a userdata value.
- fn function(self, fn)
format a function value.
- fn:items(t, hasKeys, listEnd)
format items in table "list"
- fn:keyvals(t, keys)
format key/vals in table "map".
- fn:list(t)
Format only the list-elements of a table.
- fn:rawtable(t)
- fn table(f, t)
- fn:format(fmt, ...) -> varargsUsed
like string.format but use Fmt for %q.
Doesn't return the string, instead writes to Fmt
- fn:concat(sep, ...) -> self
fmt ... separated by sep
- fn:tabulated('\t', ...)
fmt ... separated by tabs
- fn:lined('\n', ...)
fmt ... separated by newlines
- fn:tostring()
Returns the concattenated string written to the
formatter.
Error if to is set.
Similar to JSON but no commas and strings are encoded as
|bytes|
Fields:
Methods
- fn pretty(T, self)
- fn string(f, s)
The bytes type-encoder. Encodes as |bytes| instead of "string" for
lson You can set Enc.string = lson.bytes for this behavior (or use the
Lson type).
De(string or lines) -> value-iter
for val in De'["my", "lson"]' do ... end
Fields:
- dat
lines-like data to parse
- null =ds.none
value to use for null
- l
- c
- line
Methods