Module system.serialization

The serialization module provides functions for serializing and deserializing objects in multiple formats, as well as some miscellaneous encoding types.

Index

Module

base64

Base64 encoder/decoder

json

JSON encoder/decoder

lua

Lua table encoder/decoder

toml

TOML configuration encoder/decoder

Function

struct()

Defines a structure coder from C structure code.

API Reference

module system.serialization.base64

Base64 encoder/decoder

system.serialization.base64.encode(str: string): (result: string)

Encodes a binary string into Base64.

Parameters:

str (string) – The string to encode

Returns:

result (string) – The string’s representation in Base64

system.serialization.base64.decode(str: string): (result: string)

Decodes a Base64 string to binary.

Parameters:

str (string) – The Base64 to decode

Returns:

result (string) – The decoded data

module system.serialization.json

JSON encoder/decoder

system.serialization.json.null: table
system.serialization.json.emptyArray: table
system.serialization.json.encode(val: any): (result: string)

Serializes an arbitrary Lua object into a JSON string.

Parameters:

val (any) – The value to encode

Returns:

result (string) – The JSON representation of the object

system.serialization.json.decode(str: string, opts?: {binary_escapes: boolean | nil}): (result: any)

Parses a JSON string and returns a Lua value represented by the string.

Parameters:
  • str (string) – The JSON string to decode

  • opts? ({binary_escapes: boolean | nil}) – Any options to pass

Returns:

result (any) – The Lua value from the JSON

system.serialization.json.save(val: any, path: string)

Saves a Lua value to a JSON file.

Parameters:
  • val (any) – The value to save

  • path (string) – The path to the file to save

system.serialization.json.load(path: string): (result: any)

Loads a JSON file into a Lua value.

Parameters:

path (string) – The path to the file to load

Returns:

result (any) – The loaded value

module system.serialization.lua

Lua table encoder/decoder

system.serialization.lua.encode(
    val: any,
    opts?: {minified: boolean, allow_functions: boolean}
): (result: string)

Serializes an arbitrary Lua object into a serialized Lua string.

Parameters:
  • val (any) – The value to encode

  • opts? ({minified: boolean, allow_functions: boolean}) – Any options to specify while encoding

Returns:

result (string) – The serialized Lua representation of the object

system.serialization.lua.decode(str: string, opts?: {allow_functions: boolean}): (result: any)

Parses a serialized Lua string and returns a Lua value represented by the string.

Parameters:
  • str (string) – The serialized Lua string to decode

  • opts? ({allow_functions: boolean}) – Any options to specify while decoding

Returns:

result (any) – The Lua value from the serialized Lua

system.serialization.lua.save(
    val: any,
    path: string,
    opts?: {minified: boolean, allow_functions: boolean}
)

Saves a Lua value to a serialized Lua file.

Parameters:
  • val (any) – The value to save

  • path (string) – The path to the file to save

  • opts? ({minified: boolean, allow_functions: boolean}) – Any options to specify while encoding

system.serialization.lua.load(path: string, opts?: {allow_functions: boolean}): (result: any)

Loads a serialized Lua file into a Lua value.

Parameters:
  • path (string) – The path to the file to load

  • opts? ({allow_functions: boolean}) – Any options to specify while decoding

Returns:

result (any) – The loaded value

module system.serialization.toml

TOML configuration encoder/decoder

system.serialization.toml.encode(tbl: table, opts?: {indent: boolean}): (result: string)

Encodes a table into TOML format. This table must only have integer or string keys in itself and each subtable, and cannot mix strings and ints.

Parameters:
  • tbl (table) – The table to encode

  • opts? ({indent: boolean}) – Any options to specify while encoding

Returns:

result (string) – The encoded TOML data

system.serialization.toml.decode(str: string, opts?: {binary_escapes: boolean | nil}): (result: table)

Parses TOML data into a table.

Parameters:
  • str (string) – The TOML data to decode

  • opts? ({binary_escapes: boolean | nil}) – Options for decoding

Returns:

result (table) – A table representing the TOML data

system.serialization.toml.save(val: table, path: string, opts?: {indent: boolean})

Saves a table to a TOML file.

Parameters:
  • val (table) – The value to save

  • path (string) – The path to the file to save

  • opts? ({indent: boolean}) – Any options to specify while encoding

system.serialization.toml.load(path: string, opts?: table): (result: table)

Loads a TOML file into a table.

Parameters:
  • path (string) – The path to the file to load

  • opts? (table) – Options (none available in this version)

Returns:

result (table) – The loaded value

system.serialization.struct(def: string, types?: table): (
    result: fun(obj: any, pos: number | nil): any, number | nil
)

Defines a structure coder from C structure code.

See https://gist.github.com/MCJack123/2e60f0b1c01411f4fe91d902212e33c9 for more information about how this works.

Parameters:
  • def (string) – The C code to compile into types

  • types? (table) – A table containing previously defined types; types will also be stored back into this table

Returns:

result (fun(obj: any, pos: number | nil): any, number | nil) – The coder for the last defined type, which can either take an object to encode (usually a table for a struct) and returns a string, or a string to decode and optionally a position to decode from and returns the decoded object + the next position to decode from