Module rsjson

Require: require("rsjson")

Index

Function

decode()

Deserialize a JSON string into a Lua object

encode()

Serialize a Lua object into a JSON string

Class

array_metatable

A metatable attachable to a Lua table to systematically encode it as Array (instead of Map). As a result, encoded Array will contain only sequence part of the table, with the same length as the # operator on that table.

DecodeConfig

EncodeConfig

null

Represents the JSON null value. This can be used in the place of nil to represent empty values.

Api reference

class rsjson.null: lightuserdata

Represents the JSON null value. This can be used in the place of nil to represent empty values.

class rsjson.array_metatable: table

A metatable attachable to a Lua table to systematically encode it as Array (instead of Map). As a result, encoded Array will contain only sequence part of the table, with the same length as the # operator on that table.

class rsjson.EncodeConfig: userdata
staticmethod new(): rsjson.EncodeConfig

Create a new rsjson.EncodeConfig

indent: number

The number of prefix to indent lines

prefix: string

The string to use for indentation

sort_keys: boolean

Sort JSON keys

encode_empty_tables_as_array: boolean

Convert empty tables to empty arrays

detect_mixed_tables: boolean

Detect mixed sequence and key tables

deny_unsupported_types: boolean

Error on unsupported types (functions, threads, etc)

deny_recursive_tables: boolean

Error on recursive tables.

set_indent(self, indent?: integer): self

Set the indent level

set_prefix(self, prefix?: string): self

Set the indent prefix string

set_deny_unsupported_types(self, deny?: boolean): self

Set whether to deny serializing unsupported Lua types.

This includes functions, threads, lightuserdata, and errors

set_deny_recursive_tables(self, deny?: boolean): self

Set whether to deny serializing recursive tables.

If true, an attempt to serialize a recursive table will cause an error. Otherwise subsequent attempts to serialize the same table will be ignored.

set_sort_keys(self, enable?: boolean): self

Whether to sort keys in order.

set_encode_empty_tables_as_array(self, enable?: boolean): self

Whether to encode empty tables as arrays.

If false, empty tables will be serialized as maps.

set_detect_mixed_tables(self, enable?: boolean): self

Whether to detext mixed tables.

When false, a table with a non-zero length (with one or more borders) will be always encoded as an array.

class rsjson.DecodeConfig: userdata
staticmethod new(): rsjson.DecodeConfig

Create a new rsjson.DecodeConfig

null: boolean

Convert nil to rsjson.null

cast_u64_to_f64: boolean

Convert u64 numbers to f64 if they overflow i64

array_metatable: boolean

Set the metatable of JSON array tables to mlua::Lua::array_metatable

set_null(self, enable: boolean): self

Whether to decode JSON null to rsjson.null or nil

set_cast_u64_to_f64(self, enable: boolean): self

Whether to cast u64 JSON numbers to floats.

set_array_metatable(self, enable: boolean): self

Whether to set the metatable of JSON arrays to rsjson.array_mt.s

rsjson.encode(obj: any, config?: rsjson.EncodeConfig): string

Serialize a Lua object into a JSON string

Parameters:

obj (any) – Any Lua object

Returns:

_1 (string) – The serialized Lua object

rsjson.decode(str: string, config?: rsjson.DecodeConfig): any

Deserialize a JSON string into a Lua object

Parameters:

str (string) – The JSON string

Returns:

_1 (any) – The deserialized JSON object