Skip to content

json

Fields

This class has no fields.


Functions

decode_invalid_numbers

Lua CJSON may generate an error when trying to decode numbers not supported by the JSON specification. Invalid numbers are defined as: infinity, not-a-number (NaN) or hexadecimal. The current value wil always be returned.

Name Type Description
setting bool Pass true to accept and decode invalid numbers or false to throw an error

Return value

Name Type Description
value bool
json.decode_invalid_numbers([setting: bool]): bool

decode_max_depth

Lua CJSON will generate an error when parsing deeply nested JSON once the maximum array/object depth has been exceeded. This check prevents unnecessarily complicated JSON from slowing down the application, or crashing the application due to lack of process stack space.

Name Type Description
setting int Depth must be a positive integer. Default: 1000.

Return value

Name Type Description
value int
json.decode_max_depth([setting: number]): number

encode_invalid_numbers

Lua CJSON may generate an error when encoding floating point numbers not supported by the JSON specification (invalid numbers): infinity, not-a-number (NaN)

Name Type Description
setting bool Pass true to allow invalid numbers to be encoded. This will generate non-standard JSON, but this output is supported by some libraries.

Return value

Name Type Description
value bool
json.encode_invalid_numbers([setting: bool]): bool

encode_keep_buffer

Lua CJSON can reuse the JSON encoding buffer to improve performance.

Name Type Description
setting bool The buffer will grow to the largest size required and is not freed until the Lua CJSON module is garbage collected when true is passed.

Return value

Name Type Description
value bool
json.encode_keep_buffer([setting: bool]): bool

encode_max_depth

Once the maximum table depth has been exceeded Lua CJSON will generate an error. This prevents a deeply nested or recursive data structure from crashing the application.

Name Type Description
depth int Depth must be a positive integer. Default: 1000.

Return value

Name Type Description
value int
json.encode_max_depth([depth: number]): number

encode_number_precision

The amount of significant digits returned by Lua CJSON when encoding numbers can be changed to balance accuracy versus performance. For data structures containing many numbers, setting cjson.encode_number_precision to a smaller integer, for example 3, can improve encoding performance by up to 50%.

Name Type Description
precision number Precision must be an integer between 1 and 14. Default: 14.

Return value

Name Type Description
value int
json.encode_number_precision([precision: number]): number

encode_sparse_array

Name Type Description
convert boolean Convert must be a boolean. Default: false
ratio number Ratio must be a positive integer. Default: 2.
safe number Safe must be a positive integer. Default: 10.

Return value

Name Type Description
value int
json.encode_sparse_array([convert: boolean] [, ratio: number] [, safe: number]): boolean, number, number

decode

Will deserialise any UTF-8 JSON string into a Lua value or table. null will be converted to a NULL lightuserdata value. This can be compared with json.null for convenience.

Name Type Description
json_text string UTF-8 JSON text

Return value

Name Type Description
value any
json.decode(json_text: string): any

encode

Will serialise a Lua value into a string containing the JSON representation.

Name Type Description
value any A lua boolean, number, string, table or nil

Return value

Name Type Description
value string
json.encode(value: any): string