### Install Lua CJSON using Make Source: https://kyne.au/~mark/software/lua-cjson-manual.html/index Installs the Lua CJSON module using the Makefile. This method is suitable for Unix-like systems and Windows. It involves running 'make install' or manually copying the compiled 'cjson.so' file to the Lua module directory. ```bash make install # Or: make cp cjson.so $LUA_MODULE_DIRECTORY ``` -------------------------------- ### Build and Install Lua CJSON with RPM Source: https://kyne.au/~mark/software/lua-cjson-manual.html/index Installs the Lua CJSON module on Linux distributions using the RPM package manager. This method involves building an RPM package from the source and then installing it using 'rpmbuild' and 'rpm'. Ensure the 'rpm-build' package is installed. ```bash rpmbuild -tb lua-cjson-2.1.0.tar.gz rpm -Uvh $LUA_CJSON_RPM ``` -------------------------------- ### Install Lua CJSON using LuaRocks Source: https://kyne.au/~mark/software/lua-cjson-manual.html/index Installs and manages the Lua CJSON module using LuaRocks, a package manager for Lua. This is a convenient method for installing modules across various platforms, including Windows. It requires extracting the source package and running 'luarocks make'. ```bash cd lua-cjson-2.1.0 luarocks make ``` -------------------------------- ### Configure and Build Lua CJSON with CMake Source: https://kyne.au/~mark/software/lua-cjson-manual.html/index Generates build configurations and installs the Lua CJSON module using CMake. This approach supports various platforms including Unix and Windows. It requires CMake to be installed and optionally allows specifying the Lua installation directory via the LUA_DIR environment variable. ```bash mkdir build cd build # Optional: export LUA_DIR=$LUA51_PREFIX cmake .. make install # Or: make cp cjson.so $LUA_MODULE_DIRECTORY ``` -------------------------------- ### Module Instantiation Source: https://kyne.au/~mark/software/lua-cjson-manual.html/index Demonstrates how to import and instantiate the Lua CJSON module, including safe and new instances. ```APIDOC ## Module Instantiation ### Description Import Lua CJSON via the Lua `require` function. Lua CJSON does not register a global module table. The `cjson` module will throw an error during JSON conversion if any invalid data is encountered. The `cjson.safe` module behaves identically to the `cjson` module, except when errors are encountered during JSON conversion, in which case `cjson_safe.encode` and `cjson_safe.decode` will return `nil` followed by the error message. `cjson.new` can be used to instantiate an independent copy of the Lua CJSON module with a separate persistent encoding buffer and default settings. ### Method `require` (for initial import) `cjson.new` (for instantiation) ### Usage ```lua local cjson = require "cjson" local cjson2 = cjson.new() local cjson_safe = require "cjson.safe" ``` ### Notes - Lua CJSON uses `strtod` and `snprintf` for numeric conversion, requiring a workaround for locales with a comma decimal separator, which is automatically detected and implemented. - If the process locale changes, Lua CJSON should be reinitialized via `cjson.new`. - Using a different locale per thread is not supported. ``` -------------------------------- ### Instantiate Lua CJSON Module Source: https://kyne.au/~mark/software/lua-cjson-manual.html/index Demonstrates how to import and instantiate the Lua CJSON module using `require`. It covers both the standard `cjson` module and the `cjson.safe` variant, explaining their error handling differences. It also shows how to create independent module copies with `cjson.new` for thread safety. ```lua local cjson = require "cjson" local cjson2 = cjson.new() local cjson_safe = require "cjson.safe" ``` -------------------------------- ### Lua CJSON API Synopsis Source: https://kyne.au/~mark/software/lua-cjson-manual.html/index Provides a synopsis of the Lua CJSON module's API functions for encoding and decoding JSON, as well as configuring module behavior. This includes instantiating the module, translating Lua values to/from JSON, and setting various encoding/decoding parameters. ```lua -- Module instantiation local cjson = require "cjson" local cjson2 = cjson.new() local cjson_safe = require "cjson.safe" -- Translate Lua value to/from JSON text = cjson.encode(value) value = cjson.decode(text) -- Get and/or set Lua CJSON configuration setting = cjson.decode_invalid_numbers([setting]) setting = cjson.encode_invalid_numbers([setting]) keep = cjson.encode_keep_buffer([keep]) depth = cjson.encode_max_depth([depth]) depth = cjson.decode_max_depth([depth]) convert, ratio, safe = cjson.encode_sparse_array([convert[, ratio[, safe]]]) ``` -------------------------------- ### Configure Sparse Array Encoding in Lua CJSON Source: https://kyne.au/~mark/software/lua-cjson-manual.html/index Manages how Lua tables with missing elements are encoded into JSON arrays. Options include converting excessively sparse arrays to JSON objects. Default settings are convert=false, ratio=2, safe=10. ```lua convert, ratio, safe = cjson.encode_sparse_array([convert[, ratio[, safe]]]) -- "convert" must be a boolean. Default: false. -- "ratio" must be a positive integer. Default: 2. -- "safe" must be a positive integer. Default: 10. ``` ```lua cjson.encode({ [3] = "data" }) -- Returns: '[null,null,"data"]' ``` ```lua cjson.encode_sparse_array(true) cjson.encode({ [1000] = "excessively sparse" }) -- Returns: '{"1000":"excessively sparse"}' ``` -------------------------------- ### cjson.decode_max_depth Source: https://kyne.au/~mark/software/lua-cjson-manual.html/index Sets or retrieves the maximum nesting depth for JSON decoding. ```APIDOC ## cjson.decode_max_depth ### Description Sets or retrieves the maximum allowed nesting depth for JSON arrays and objects during decoding. This prevents excessively complex JSON from consuming excessive resources or causing stack overflows. The default maximum depth is 1000. ### Method `cjson.decode_max_depth([depth])` ### Parameters - **depth** (positive integer, optional) - The new maximum nesting depth. If not provided, the current depth is returned. ### Returns - (integer) - The current maximum nesting depth setting. ``` -------------------------------- ### cjson.decode Source: https://kyne.au/~mark/software/lua-cjson-manual.html/index Deserializes a UTF-8 JSON string into a Lua value or table. ```APIDOC ## cjson.decode ### Description Deserializes any UTF-8 JSON string into a Lua value or table. UTF-16 and UTF-32 JSON strings are not supported. NULL (ASCII 0) and double quote (ASCII 34) characters must be escaped within strings. All escape codes will be decoded, and other bytes will be passed transparently. UTF-8 characters are not validated during decoding. JSON `null` is converted to a NULL `lightuserdata` value, comparable with `cjson.null`. By default, numbers incompatible with the JSON specification (infinity, NaN, hexadecimal) can be decoded. ### Method `cjson.decode(json_text)` ### Parameters - **json_text** (string) - The UTF-8 JSON string to decode. ### Returns - (any) - The deserialized Lua value or table. ### Request Example ```lua json_text = '[ true, { "foo": "bar" } ]' value = cjson.decode(json_text) -- value will be: { true, { foo = "bar" } } ``` ### Caution Numeric keys in decoded JSON objects are stored as Lua strings. Code assuming a `number` type for these keys may break. ``` -------------------------------- ### Decode JSON Text to Lua Value Source: https://kyne.au/~mark/software/lua-cjson-manual.html/index Explains the `cjson.decode` function for deserializing UTF-8 JSON strings into Lua values. It highlights limitations such as unsupported UTF-16/UTF-32, required escaping of NULL and double quotes, and the conversion of JSON `null` to a Lua lightuserdata. It also notes that numeric keys in JSON objects are decoded as strings in Lua. ```lua local json_text = '[ true, { "foo": "bar" } ]' local value = cjson.decode(json_text) -- Returns: { true, { foo = "bar" } } ``` -------------------------------- ### cjson.encode_max_depth Source: https://kyne.au/~mark/software/lua-cjson-manual.html/index Sets the maximum recursion depth for encoding Lua tables. Exceeding this depth will result in an error, preventing potential crashes due to deeply nested or recursive data structures. ```APIDOC ## PUT /lua-cjson/encode_max_depth ### Description Sets the maximum table nesting depth allowed during JSON encoding. Exceeding this depth will generate an error, preventing application crashes from deeply nested or recursive data structures. The default is 1000. ### Method PUT ### Endpoint /lua-cjson/encode_max_depth ### Parameters #### Query Parameters - **depth** (integer) - Required - The maximum nesting depth (positive integer). ### Response #### Success Response (200) - **depth** (integer) - The current maximum encoding depth. #### Response Example ```json { "depth": 1000 } ``` ``` -------------------------------- ### cjson.encode_keep_buffer Source: https://kyne.au/~mark/software/lua-cjson-manual.html/index Controls whether Lua CJSON reuses the JSON encoding buffer to improve performance. Keeping the buffer (`true`) grows it to the largest required size and reuses it, while freeing it (`false`) after each call can reduce memory usage. ```APIDOC ## PUT /lua-cjson/encode_keep_buffer ### Description Configures whether Lua CJSON reuses the JSON encoding buffer to improve performance. `true` (default) keeps the buffer, growing it as needed. `false` frees the buffer after each `cjson.encode` call. ### Method PUT ### Endpoint /lua-cjson/encode_keep_buffer ### Parameters #### Query Parameters - **keep** (boolean) - Required - `true` to keep the buffer, `false` to free it. ### Response #### Success Response (200) - **keep** (boolean) - The current setting for keeping the encode buffer. #### Response Example ```json { "keep": true } ``` ``` -------------------------------- ### Set Maximum Table Nesting Depth for Encoding with cjson.encode_max_depth Source: https://kyne.au/~mark/software/lua-cjson-manual.html/index Sets the maximum depth for table nesting during JSON encoding to prevent application crashes due to deeply nested or recursive data structures. Exceeding this depth generates an error. The default is 1000. The current setting is always returned. ```lua depth = cjson.encode_max_depth([depth]) -- "depth" must be a positive integer. Default: 1000. ``` ```lua a = {} a[1] = a ``` -------------------------------- ### Encode Lua Value to JSON String with cjson.encode Source: https://kyne.au/~mark/software/lua-cjson-manual.html/index Serializes a Lua value into a JSON string representation. Supports booleans, numbers, strings, tables, nil, and lightuserdata (NULL only). Errors on functions, non-NULL lightuserdata, threads, and userdata. Numbers are encoded with 14 significant digits by default. Control characters, double quotes, forward slashes, backslashes, and delete are escaped within strings. Binary strings are supported but may not be compatible with other JSON libraries; UTF-8 strings are recommended. Tables with positive integer keys are encoded as arrays, others as objects. JSON object keys must be numbers or strings. Standards-compliant JSON requires a table as input. ```lua json_text = cjson.encode(value) ``` ```lua value = { true, { foo = "bar" } } json_text = cjson.encode(value) -- Returns: '[true,{"foo":"bar"}]' ``` -------------------------------- ### Configure Encoding of Invalid Numbers with cjson.encode_invalid_numbers Source: https://kyne.au/~mark/software/lua-cjson-manual.html/index Controls how floating-point numbers not supported by the JSON specification (infinity, NaN) are handled during encoding. Can be set to `true` to encode them as non-standard JSON, or to the string `"null"` to encode them as JSON `null` values. The default is `false`, which throws an error. The current setting is always returned. ```lua setting = cjson.encode_invalid_numbers([setting]) -- "setting" must a boolean or "null". Default: false. ``` -------------------------------- ### Configure Decoding of Invalid Numbers Source: https://kyne.au/~mark/software/lua-cjson-manual.html/index Describes the `cjson.decode_invalid_numbers` setting, which controls whether numbers not conforming to the JSON specification (infinity, NaN, hexadecimal) are decoded or cause an error. The function returns the current setting and updates it when a boolean argument is provided. The default is `true`. ```lua local setting = cjson.decode_invalid_numbers(true) -- or local current_setting = cjson.decode_invalid_numbers() ``` -------------------------------- ### Control JSON Encoding Buffer Reuse with cjson.encode_keep_buffer Source: https://kyne.au/~mark/software/lua-cjson-manual.html/index Determines whether the JSON encoding buffer is reused to improve performance. When `true` (default), the buffer grows as needed and is not freed until module garbage collection. When `false`, the buffer is freed after each `cjson.encode` call. The current setting is always returned. ```lua keep = cjson.encode_keep_buffer([keep]) -- "keep" must be a boolean. Default: true. ``` -------------------------------- ### cjson.encode_invalid_numbers Source: https://kyne.au/~mark/software/lua-cjson-manual.html/index Configures how Lua CJSON handles floating-point numbers not supported by the JSON specification (infinity, NaN). It can either throw an error (default), encode them as JSON `null`, or allow non-standard encoding. ```APIDOC ## PUT /lua-cjson/encode_invalid_numbers ### Description Configures the behavior for encoding floating-point numbers not supported by the JSON specification (infinity, NaN). Available settings are `true` (encode non-standard JSON), `"null"` (encode as JSON `null`), and `false` (throw an error, default). ### Method PUT ### Endpoint /lua-cjson/encode_invalid_numbers ### Parameters #### Query Parameters - **setting** (boolean | string) - Required - The desired setting: `true`, `"null"`, or `false`. ### Response #### Success Response (200) - **setting** (boolean | string) - The current setting for encoding invalid numbers. #### Response Example ```json { "setting": false } ``` ``` -------------------------------- ### Set Number Precision for Lua CJSON Encoding Source: https://kyne.au/~mark/software/lua-cjson-manual.html/index Controls the number of significant digits when encoding numbers to JSON. Accepts an integer between 1 and 14. Default is 14. Lowering this can improve performance for large datasets. ```lua precision = cjson.encode_number_precision([precision]) -- "precision" must be an integer between 1 and 14. Default: 14. ``` -------------------------------- ### cjson.encode Source: https://kyne.au/~mark/software/lua-cjson-manual.html/index Serializes a Lua value into a JSON string representation. It supports various Lua types and handles character escaping according to JSON standards. The encoding of Lua tables as JSON arrays or objects depends on their keys. ```APIDOC ## POST /lua-cjson/encode ### Description Serializes a Lua value into a string containing the JSON representation. Supports boolean, lightuserdata (NULL only), nil, number, string, and table types. Other types will generate an error. Numbers are encoded with 14 significant digits by default, configurable via `cjson.encode_number_precision`. Control characters, double quote, forward slash, backslash, and delete are escaped within UTF-8 strings. Binary strings can be encoded but are not standard JSON; ensure strings are UTF-8 for compatibility. Lua tables are encoded as JSON arrays if they have only positive integer keys, otherwise as JSON objects. Metamethods are not used; `rawget` iterates arrays and `next` iterates objects. Sparse arrays can be handled via `cjson.encode_sparse_array`. Table keys must be numbers or strings. ### Method POST ### Endpoint /lua-cjson/encode ### Parameters #### Request Body - **value** (any) - Required - The Lua value to encode. ### Request Example ```json { "value": [true, {"foo": "bar"}] } ``` ### Response #### Success Response (200) - **json_text** (string) - The JSON string representation of the input Lua value. #### Response Example ```json { "json_text": "[true,{"foo":"bar"}]" } ``` ``` -------------------------------- ### cjson.decode_invalid_numbers Source: https://kyne.au/~mark/software/lua-cjson-manual.html/index Controls whether numbers not supported by the JSON specification (infinity, NaN, hexadecimal) can be decoded. ```APIDOC ## cjson.decode_invalid_numbers ### Description This function controls whether Lua CJSON will attempt to decode numbers not strictly supported by the JSON specification, such as infinity, NaN, and hexadecimal numbers. When set to `true` (default), these numbers are accepted and decoded. When set to `false`, an error is thrown if such numbers are encountered. ### Method `cjson.decode_invalid_numbers([setting])` ### Parameters - **setting** (boolean, optional) - If provided, sets the decoding behavior. `true` to accept invalid numbers, `false` to throw an error. ### Returns - (boolean) - The current setting for decoding invalid numbers. ``` -------------------------------- ### Set Maximum JSON Decoding Depth Source: https://kyne.au/~mark/software/lua-cjson-manual.html/index Details the `cjson.decode_max_depth` setting, used to prevent excessively nested JSON from causing performance issues or crashes. It sets a limit on the maximum array/object nesting depth, with a default of 1000. The function returns the current depth and updates it when a positive integer argument is provided. ```lua local depth = cjson.decode_max_depth(500) -- or local current_depth = cjson.decode_max_depth() ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.