Module JSON
LuaJson base class
Functions
| .array (arr[, ...]) | Create a new array. |
| .object (obj[, ...]) | Create a new object. |
| .tojson (elm) | get a lua json elements. |
| .tostring (elm) | get a lua json elements type and memory address. |
| .size (elm) | get a lua json elements size (alternative to #elm ) |
| .parse (elm) | Serialize a lua json element to json string. |
| .parse_lua (table) | Serialize a lua table to json string. |
| .stringify (elm) | Serialize lua json element to json string. |
| .stringify_lua (elm) | Serialize lua json object to json string. |
Functions
Methods- .array (arr[, ...])
-
Create a new array.
Parameters:
- arr array name
- ... elements (optional)
Returns:
-
an initialized lua json array.
Usage:
local a = JSON.array(a, 1,2,3.45,"test",true,null) print(a[0]) --> 1 print(a[2]) --> 3.45 print(a[#a]) --> null
- .object (obj[, ...])
-
Create a new object.
Parameters:
- obj object name
- ... key value pair/s (optional)
Returns:
-
an initialized lua json object
Usage:
local o = JSON.object(o, "test","obj", "age",99, "root",null) print(o.test) --> obj print(o.age) --> 99 print(o.root) --> null
- .tojson (elm)
-
get a lua json elements.
Parameters:
- elm a valid lua json element.
Returns:
-
serialized lua json element.
Usage:
local ta = JSON.array(ta, 1,2,3.45,true) ta = JSON.tojson(ta) print(ta) --> [1,2,3.45,true]
- .tostring (elm)
-
get a lua json elements type and memory address.
Parameters:
- elm a valid lua json element.
Returns:
-
formatted string containing element type and memory address
- .size (elm)
-
get a lua json elements size (alternative to #elm )
Parameters:
- elm a valid lua json element.
Returns:
-
number of entries in lua json element.
- .parse (elm)
-
Serialize a lua json element to json string.
Parameters:
- elm lua table
Returns:
-
a valid json element
Usage:
local a = JSON.parse('[1,2,3.45,true]') print(a:tojson()) --> [1,2,3.45,true] print(a[0]) --> 1
- .parse_lua (table)
-
Serialize a lua table to json string.
Parameters:
- table lua table
Returns:
-
a valid json representation of the table.
Usage:
local t = {1,2,3.45,"test",true} print(t) --> table: 0x5c92968f1c90 local ta = JSON.parse_lua(ta, t) print(ta) --> array: 0x5c92968f09a8 print(ta[0]) --> 1 print(ta[2]) --> 3.45 print(ta[#ta]) --> true
- .stringify (elm)
-
Serialize lua json element to json string.
Parameters:
- elm lua json element
Returns:
-
a valid json representation of the lua json element.
Usage:
local a = JSON.parse('[1,2,3.45,true]') print(a:tojson()) --> [1,2,3.45,true] local json_a = JSON.stringify(a) print(json_a) --> [1,2,3.45,"test",true]
- .stringify_lua (elm)
-
Serialize lua json object to json string.
Parameters:
- elm lua json element
Returns:
-
a valid json representation of the lua json element.
Usage:
local t = {1,2,3.45,"test",true} print(t) --> table: 0x5c92968f1c90 local json_a = JSON.stringify_lua(t) print(json_a) --> [1,2,3.45,"test",true]