lljson.decode(json: string) : string|number|integer|vector|uuid|quaternion|boolean|{}|nil
decode json string to lua value
lljson.decode
will error if an invalid string is passed to it.
It is recommended to use
pcall
or
xpcall
to wrap the
method and handle the resulting error
pcall
local valid_json = '["first","second","third"]'
local ok, result = pcall(lljson.decode, valid_json)
if ok then
ll.OwnerSay(`The json was valid. '{result[1]}' '{result[3]}'`) -- prints: The json was valid. 'first' 'third'
else
ll.OwnerSay("The json was invalid") -- will not be called
end
local invalid_json = '"first","second","third"'
local ok, result = pcall(lljson.decode, invalid_json)
if ok then
ll.OwnerSay(`The json was valid. '{result[1]}' '{result[3]}'`) -- will not be called
else
ll.OwnerSay("The json was invalid") -- prints: The json was invalid
end
pcall
local valid_json = '["first","second","third"]'
local result = lljson.decode(valid_json)
ll.OwnerSay(`The json was valid. '{result[1]}' '{result[3]}'`) -- prints: The json was valid. 'first' 'third'
local invalid_json = '"first","second","third"'
local result = lljson.decode(invalid_json) -- Script errors and dies
ll.OwnerSay(`The json was valid. '{result[1]}' '{result[3]}'`) -- will not be called
{
"def": "func",
"name": "decode",
"energy": 0,
"pure": true,
"sleep": 0,
"signatures": [
{
"result": [
{
"name": "",
"def": "result",
"desc": "",
"variadic": false,
"type": [
"string",
"number",
"integer",
"vector",
"uuid",
"quaternion",
"boolean",
"{}",
"nil"
],
"optional": false
}
],
"args": [
{
"def": "arg",
"name": "json",
"desc": "json string to decode",
"type": [
"string"
],
"variadic": false,
"optional": false
}
]
}
],
"desc": "decode json string to lua value",
"link": ""
}