dreamworkAPI
Ctrl K
GitHub
libraryDocs / json

json

librarysharedmenusource

The JSON format is a lightweight data interchange format.

Follows RFC 8259 strictly: objects, arrays, strings, numbers, true, false and null.

Functions

json.decodeDecodes a JSON string into a Lua value, the root can be any JSON value.
json.deserializeDeserializes a JSON string into a Lua table, the root must be an object or an array.
json.encodeEncodes a Lua value into a JSON string, the root can be any JSON value.
json.serializeSerializes a Lua table into a JSON string, the root must be an object or an array.

#json.decode

sharedmenusource
function json.decode(json_str: string, max_depth?: integer): json.Value

Decodes a JSON string into a Lua value, the root can be any JSON value.

JSON null becomes null (never nil), numbers become Lua numbers, objects and arrays become plain tables.

A leading UTF-8 BOM is skipped, nesting deeper than max_depth levels is rejected.

Raises a DecodeError (with the byte position of the problem) on malformed input or trailing data.

Parameters
NameTypeDescription
json_strstringThe JSON string to decode.
max_depthoptionalintegerThe maximum nesting depth, default is 512.
Returns
TypeNameDescription
json.Valuejson_valueThe decoded value.
See also

#json.deserialize

sharedmenusource
function json.deserialize(json_str: string, max_depth?: integer): json.Table

Deserializes a JSON string into a Lua table, the root must be an object or an array.

JSON null becomes null (never nil), numbers become Lua numbers, objects and arrays become plain tables.

A leading UTF-8 BOM is skipped, nesting deeper than max_depth levels is rejected.

Raises a DecodeError (with the byte position of the problem) on malformed input, trailing data or a non-table root.

Parameters
NameTypeDescription
json_strstringThe JSON string to deserialize.
max_depthoptionalintegerThe maximum nesting depth, default is 512.
Returns
TypeNameDescription
json.Tablejson_tableThe deserialized table.
See also

#json.encode

sharedmenunodiscardsource
function json.encode(value: json.Value | nil, prettify?: boolean, max_depth?: integer): string

Encodes a Lua value into a JSON string, the root can be any JSON value.

Tables with only sequential keys 1..n (and empty tables) become arrays, all other tables become objects.

nil and null are written as null.

Tables with a metatable are converted by the first available metamethod: __tojson (must return a valid JSON string, written as is), __tonumber (number) or __tostring (string).

Object keys that are not strings are converted: numbers and booleans are formatted, other values need __tostring.

Raises an EncodeError for any other non-JSON value (functions, threads, NaN/inf), unconvertible keys, keys that collide after conversion, or nesting deeper than max_depth levels (circular references).

Parameters
NameTypeDescription
valuejson.Value | nilThe value to encode.
prettifyoptionalbooleanWhether to indent the output, default is false.
max_depthoptionalintegerThe maximum nesting depth, default is 512.
Returns
TypeNameDescription
stringjson_strThe JSON string.
See also

#json.serialize

sharedmenunodiscardsource
function json.serialize(tbl: json.Table, prettify?: boolean, max_depth?: integer): string

Serializes a Lua table into a JSON string, the root must be an object or an array.

Tables with only sequential keys 1..n (and empty tables) become arrays, all other tables become objects.

nil and null are written as null.

Tables with a metatable are converted by the first available metamethod: __tojson (must return a valid JSON string, written as is), __tonumber (number) or __tostring (string).

Object keys that are not strings are converted: numbers and booleans are formatted, other values need __tostring.

Raises an EncodeError for any other non-JSON value (functions, threads, NaN/inf), unconvertible keys, keys that collide after conversion, or nesting deeper than max_depth levels (circular references).

Parameters
NameTypeDescription
tbljson.TableThe table to serialize.
prettifyoptionalbooleanWhether to indent the output, default is false.
max_depthoptionalintegerThe maximum nesting depth, default is 512.
Returns
TypeNameDescription
stringjson_strThe JSON string.
See also
Generated from the dreamwork meta branch.