Supported Lua Libraries
Standard Lua Libraries
Lua version: 5.3.5
Libraries that are implicitly loaded:
| Name | Reference |
|---|---|
basic |
|
string |
|
table |
|
math |
|
package |
Libraries that need to be required:
| Name | Reference |
|---|---|
utf8 |
|
io |
|
os |
|
coroutine |
|
debug |
Extensions
rapidjson
Extended lua-rapidjson functions.
Return Type
Lua table if JSON is an object or array.
Return true, false, number and rapidjson.null respectively if JSON is a simple value.
On error returns nil and an error message as a second result when the passed string is not a valid JSON.
If the error is returned as a second result, in addition to the encountered problem description, its location is shown as an offset in bytes.
Can also raise Lua errors, in these cases, pcall can be used to catch the error.
| For the decode function, the maximum depth of the JSON string is 256. If the decode function is used on a JSON string with a depth greater than 256, it will return an error. |
Parameters
| Name | Type | Optional | Description |
|---|---|---|---|
input |
variant |
no |
Normally it’s a Lua table, but can also be a Boolean or a number. |
parse_flags |
number |
yes |
Optional bit mask. |
parse_flags can be a combination of the following values
| Value | Description |
|---|---|
|
No flags are set. |
|
Parse number in full precision (but slower). |
|
Allow one-line ( |
|
Allow parsing |
Examples
local JSON = require('rapidjson')
-- Don't use such stright comparison of floating point numbers in production.
return {
full_precision = JSON.decode([[ { "abc":0.9868011474609375 } ]], 16).abc == 0.9868011474609375, -- true
default = JSON.decode([[ { "abc":0.9868011474609375 } ]] ).abc == 0.9868011474609375 -- false
}
local JSON = require('rapidjson')
return {JSON.decode([[ {
"abc":1, // this is a one line comment
/* multi
"line":
"comment",
*/
"xyz": 2
} ]])} -- it is enabled by default
Result:
[{"abc":1,"xyz":2}]
local JSON = require('rapidjson')
return {JSON.decode([[ {
"abc":1, // this is a one line comment
/* multi
"line":
"comment",
*/
"xyz": 2
} ]], 0)} -- explicitly disable comments
Result:
[null,"Missing a name for object member. (13)"]
local JSON = require('rapidjson')
local test_special_fp_nums = JSON.decode(
[[{"valuePInf":Infinity,"valueNInf":-Infinity,"valuePInf2":Inf,"valueNInf2":-Inf,"valueNaN":NaN}]],
256)
-- manually encode for Console in order to not be affected by its implementation of serialization
return JSON.encode(test_special_fp_nums, {write_flags=2, pretty=true})
Result:
{
"valueNInf": -Infinity,
"valueNaN": NaN,
"valuePInf": Infinity,
"valuePInf2": Infinity,
"valueNInf2": -Infinity
}
Description
Writes Lua value into a JSON file. This function is a full equiualent to rapidjson.encode.
Return Type
Returns true on success.
Return nil on failure, plus an error message as a second result. Can raise Lua errors.
Parameters
| Name | Type | Optional | Description |
|---|---|---|---|
input |
variant |
no |
See |
file_path |
string |
no |
A path to JSON file to write. |
options |
table |
yes |
See |
Return Type
String on success.
Return nil on failure, plus an error message as a second result. Can raise Lua errors.
Parameters
| Name | Type | Optional | Description |
|---|---|---|---|
input |
variant |
no |
Normally it’s a Lua table, but can also be a string, a Boolean, a number, or |
options |
table |
yes |
Optional table with fine tuning parameters. |
options table can have the following fields
| Name | Type | Description |
|---|---|---|
pretty |
Boolean |
Set |
sort_keys |
Boolean |
Set |
empty_table_as_array |
Boolean |
Set |
write_flags |
Boolean |
Set |
Examples
local JSON = require('rapidjson')
local testNaN = {
valueNaN = 0/0,
valueNInf = -1/0,
valuePInf= 1/0
}
return JSON.encode(testNaN, {write_flags=2})
Result:
{"valueNInf":-Infinity,"valuePInf":Infinity,"valueNaN":NaN}
Compare with dkjson
local JSON = require('dkjson')
local testNaN = {
valueNaN = 0/0,
valueNInf = 1/0,
valuePInf= -1/0
}
return JSON.encode(testNaN)
Result:
{"valueNaN":null,"valueNInf":null,"valuePInf":null}
Return Type
See rapidjson.decode.
Can raise Lua errors.
Parameters
| Name | Type | Optional | Description |
|---|---|---|---|
file_path |
string |
no |
A path to JSON file to load. |
parse_flags |
number |
yes |
Optional bit mask. See |