Only Lua 5.3 is supported.
Decoding is done by applying decoding functions to streams.
A stream is anything with a read
method supporting the number format as the
standard file:read
function.
Decoding functions always return a single value when successful or nil and a string describing the error otherwise.
Takes a string s
and returns a stream of it's contents. The stream is a
table with a read
function like the standard file:read
but supporting
only the number format.
Decoding functions for the bale integer types which return lua numbers given a sufficiently long input stream.
Decoding functions for floating-point types, these work only if the platform native single and double precision floating point formats are from IEEE 754.
Decoding function for the bale variable length type.
This function takes as arguments alternating strings and decoding functions.
Returns a decoding function that produces a table with the strings as keys and the results of applying the decoding functions as values.
The decoding functions are applied to the input stream in the order they are
given to the tuple
function.
Creates a bale union decoding function, arguments should be decoding functions for the alternatives of the union in order.
Either none
or nil
can be used to skip indices.
Returns a function that decodes a bale array into a lua sequence.
f
is the decoding function for each item of the array.
Decoding function for none
, will always return nil.
Returns a decoding function for a bale void
. Since an empty table would
not be very useful, the value to return is specified via v
.
Decodes a boolean.
Returns a decoding function for an optional value. f
is the decoding function
for the value if it is present, otherwise default
is returned.
Decodes a string.
Returns a decoding function for a bale map
whose keys can be decoded with
fk
and values with fv
.
Repeats the decoding function f
n
times and returns the result as a lua
sequence.
Returns a decoding function for a raw read of n
bytes.
Transforms the decoding function f
into one that returns nil if the input
stream has not been entirely consumed.
We encode by repeatedly applying encoding functions to an encoder and finally
calling done
on it to retrieve the result as a string.
An encoder is a sequence of strings so you may also call ipairs
on it to
iterate over fragments of the result.
An encoding function is a function that takes an encoder as the first argument optionally followed by further arguments and returns the encoder it was passed.
Apart from new
and done
, all functions below are encoding functions which
are exported by the module as well as being available as methods of an encoder.
Create a new encoder.
Encode lua signed and unsigned numbers.
Encode floating point numbers. As with the decoding functions these rely on the platform native format to be the right one.
Encode v as a bale uv
.
Encode a lua sequence a
as a bale array
by encoding each item using an
encoding function f
.
Encode a bale none
, this will always throw an error.
Encode a bale void
, leaves the encoder unmodified.
Encode a boolean b
.
Encode v
using the encoding function f
if v
is not nil
, results in a
bale maybe
.
Encode a lua table m
as a bale map
by using the encoding functions fk
and
fv
to encode keys and values respectively.
Encode a string.
Encode the sequence a
as a fixed length tuple, encoding each item using f
.
Append s
as raw data to the encoder.
Returns the encoded data as a string.