Class object
JSON.object Class
Methods
| object:tojson (start, end, escape) | render an object as valid json object |
| object:tolua (start, end, escape) | render an object as a serialized lua table |
| object:totable () | return lua json object as a lua table |
| object:keys () | Create a new array containing the keys of an object |
| object:move (move, to) | move an existing key/value pair to any exisiting index |
| object:insert (at, new, value) | insert new key/value pair at any existing position |
| object:push (key, value) | push new key/value pair to end of object |
| object:pop () | pop the last key/value pair from object |
| object:shift () | Remove the first key/value pair from object. |
| object:unshift (key, val[, ...]) | add one or more key/value pairs to the beginning of an object. |
| object:reverse (from, to) | reverses the indexes of an object key/value pairs. |
Methods
- object:tojson (start, end, escape)
-
render an object as valid json object
Parameters:
- start string or number: start position (optional)
- end string or number: end position (optional)
- escape bool: escape output (optional)
Returns:
-
string: json string
Usage:
print(obj:tojson()) --> {"test":"obj","age":99,"root":null}
print(obj:tojson(0)) --> {"test":"obj"}
print(obj:tojson("test", "age")) --> {"test":"obj", "age", 99}
print(obj:tojson(1,2,true)) --> {\"age\":99,\"root\":false}
- object:tolua (start, end, escape)
-
render an object as a serialized lua table
Parameters:
- start string or number: start position (optional)
- end string or number: end position (optional)
- escape bool: escape output (optional)
Returns:
-
string: serialized table
Usage:
print(obj:tolua()) --> {test="obj",age=99,root=null,}
print(obj:tolua(0)) --> {test="obj"}
print(obj:tolua("test", "age")) --> {test="obj",age=99}
print(obj:tolua(1,2,true)) --> {test=\"obj\",age=99,root=false}
- object:totable ()
-
return lua json object as a lua table
Returns:
-
lua table
See also:
Usage:
local t = o:totable() print(t) --> table: 0x627a723b6400 print(o) --> object: 0x627a723a9d30 print(o[0]) --> test print(t[1]) --> test print(o[2]) --> null print(t[3]) --> null
- object:keys ()
-
Create a new array containing the keys of an object
Returns:
-
array: json array of the objects keys
Usage:
print(obj:tojson()) --> {"test":"obj","age":99,"root":null} local keys = obj:keys() print(keys:tojson())--> ["test","age","root"]
- object:move (move, to)
-
move an existing key/value pair to any exisiting index
Parameters:
- move string or number: exisiting key or index of pair to move
- to string or number: exisiting key or index a pair to start the shift right
Returns:
-
no value retured.
Usage:
print(obj:tojson()) --> {"test":"obj","age":99,"root":null} obj:move("root", "test") print(obj:tojson()) --> {"root":null,"test":"obj","age":99}
- object:insert (at, new, value)
-
insert new key/value pair at any existing position
Parameters:
- at string or number: exisitng key or index to insert the new pair
- new
string: new key - value
any: new value
Returns:
-
number: updated size of object.
Usage:
print(obj:tojson()) --> {"test":"obj","age":99,"root":null} local s = obj:insert(0, "new","value") print(obj:tojson()) --> {"new":"value","test":"obj","root":null,"age":99} print(s) --> 4
- object:push (key, value)
-
push new key/value pair to end of object
Parameters:
- key string: new key
- value any: new value
Returns:
-
number: updated size of object.
Usage:
print(obj:tojson()) --> {"test":"obj","age":99,"root":null} local s = obj:push("new","value") print(obj:tojson()) --> {"test":"obj","age":99,"root":null,"new":"value"} print(s) --> 4
- object:pop ()
-
pop the last key/value pair from object
Returns:
-
any: the popped value.
Usage:
print(obj:tojson()) --> {"test":"obj","age":99,"root":null} local p = obj:pop() print(obj:tojson()) --> {"test":"obj","age":99} print(p) --> null
- object:shift ()
-
Remove the first key/value pair from object.
Returns:
-
any: the shifted value.
Usage:
print(obj:tojson()) --> {"test":"obj","age":99,"root":null} local s = obj:shift() print(obj:tojson()) --> {"age":99,"root":null} print(s) --> obj
- object:unshift (key, val[, ...])
-
add one or more key/value pairs to the beginning of an object.
Parameters:
- key string: new key
- val any: new value
- ... additional pairs (optional)
Returns:
-
number: the updated object size.
Usage:
print(obj:tojson()) --> {"test":"obj","age":99,"root":null} local npairs = obj:unshift("name", "teddy", "id", 101, "admin", false)) print(obj:tojson()) --> {"name":"teddy","id":101,"admin":false,"age":99,"root":null} print(npairs) --> 5
- object:reverse (from, to)
-
reverses the indexes of an object key/value pairs.
Parameters:
- from string or number: key or index to start reversal. (optional)
- to string or number: key or index to end of reversal. (optional)
Returns:
-
no value returned
Usage:
print(obj:tojson()) --> {"test":"obj","age":99,"root":null, "name":"teddy"} npairs = obj:reverse("test", "root")) print(obj:tojson()) --> {""root":null,"age":99,test":"obj","name":"teddy"}