json.encode(value)
Encode any Lua value in a JSON representation string, following those rules :
Lua value type | JSON |
---|---|
"string" |
"string" |
number |
number |
boolean |
"true" / "false" |
table |
object {} |
table (integer keys) |
array [] |
|
null |
object |
Not supported (converted to JSON object {}) |
thread |
Not supported (converted to "string") |
userdata |
Not supported (converted to "string") |
Parameters
value
Lua value to be encoded.
Return value
Returns the encoded JSON string or anil
value in case of error.
Example
-- use the json binary module
-- must be in the same folder as this Lua script
-- or in the 'modules' folder of your LuaRT installation path
local json = require "json"
-- Lua array (integer only indexed table)
local array = { 1, 2, 3, "Hello !" }
-- prints the JSON representation of the array value
-- should output [1,2,3,"Hello !"]
print(json.encode(array))