Serde
Built-in library for:
- serialization & deserialization
- encoding & decoding
- compression
Example usage
Section titled “Example usage”local fs = require("@lune/fs")local serde = require("@lune/serde")
-- Parse different file formats into lua tableslocal someJson = serde.decode("json", fs.readFile("myFile.json"))local someToml = serde.decode("toml", fs.readFile("myFile.toml"))local someYaml = serde.decode("yaml", fs.readFile("myFile.yaml"))
-- Write lua tables to files in different formatsfs.writeFile("myFile.json", serde.encode("json", someJson))fs.writeFile("myFile.toml", serde.encode("toml", someToml))fs.writeFile("myFile.yaml", serde.encode("yaml", someYaml))
Functions
Section titled “Functions”encode
Section titled “encode”Encodes the given value using the given format.
See [EncodeDecodeFormat
] for a list of supported formats.
Parameters
Section titled “Parameters”-
format
The format to use -
value
The value to encode -
pretty
If the encoded string should be human-readable, including things such as newlines and spaces. Only supported for json and toml formats, and defaults to false
Returns
Section titled “Returns”- The encoded string
decode
Section titled “decode”Decodes the given string using the given format into a lua value.
See [EncodeDecodeFormat
] for a list of supported formats.
Parameters
Section titled “Parameters”-
format
The format to use -
encoded
The string to decode
Returns
Section titled “Returns”- The decoded lua value
compress
Section titled “compress”Compresses the given string using the given format.
See [CompressDecompressFormat
] for a list of supported formats.
Parameters
Section titled “Parameters”-
format
The format to use -
s
The string to compress -
level
The compression level to use, clamped to the format’s limits. The best compression level is used by default
Returns
Section titled “Returns”- The compressed string
decompress
Section titled “decompress”Decompresses the given string using the given format.
See [CompressDecompressFormat
] for a list of supported formats.
Parameters
Section titled “Parameters”-
format
The format to use -
s
The string to decompress
Returns
Section titled “Returns”- The decompressed string
Hashes the given message using the given algorithm and returns the hash as a hex string.
See [HashAlgorithm
] for a list of supported algorithms.
Parameters
Section titled “Parameters”-
algorithm
The algorithm to use -
message
The message to hash
Returns
Section titled “Returns”- The hash as a hex string
Hashes the given message using HMAC with the given secret and algorithm, returning the hash as a base64 string.
See [HashAlgorithm
] for a list of supported algorithms.
Parameters
Section titled “Parameters”-
algorithm
The algorithm to use -
message
The message to hash -
secret
string | buffer
Returns
Section titled “Returns”- The hash as a base64 string
EncodeDecodeFormat
Section titled “EncodeDecodeFormat”A serialization/deserialization format supported by the Serde library.
Currently supported formats:
Name | Learn More |
---|---|
json | https://www.json.org |
yaml | https://yaml.org |
toml | https://toml.io |
CompressDecompressFormat
Section titled “CompressDecompressFormat”A compression/decompression format supported by the Serde library.
Currently supported formats:
Name | Learn More |
---|---|
brotli | https://github.com/google/brotli |
gzip | https://www.gnu.org/software/gzip |
lz4 | https://github.com/lz4/lz4 |
zlib | https://www.zlib.net |
HashAlgorithm
Section titled “HashAlgorithm”A hash algorithm supported by the Serde library.
Currently supported algorithms: