Class array

LuaJson array class

Functions

JSON:array ([elm[, ...]]) Create a new array.

Methods

array:tojson () render an array as valid json
array:insert (pos, elm) Inserts an element at position.
array:del (pos) Deletes an element at position.
array:reverse () reverses the indexes of the elements of an array.
array:push (elm) Adds element to the end of an array.
array:pop () Pops and returns the last element of an array.
array:shift () Removes element at index 0.
array:unshift (elm[, ...]) adds one or more elements to the beginning of an array.
array:ref () Creates a new reference to an existing array.
array:unref () Creates an unreferenced copy of an existing array.


Functions

Methods
JSON:array ([elm[, ...]])
Create a new array.

Parameters:

  • elm any (optional)
  • ... next elm/s (optional)

Returns:

    an initialized lua json array.

See also:

Usage:

    local a = JSON:array(1,2,3.45,"test",true,null)
    print(a[0])  --> 1
    print(a[2])  --> 3.45
    print(a[#a]) --> null

Methods

array:tojson ()
render an array as valid json

Returns:

    json string

See also:

Usage:

    print(a:tojson()) --> [1,2,3.45,"test",true,null]
array:insert (pos, elm)
Inserts an element at position.

Parameters:

  • pos a valid index to insert item.
  • elm any

Returns:

    updated length of array.

Usage:

    local a = JSON:array(1,2,3.45,"test",true,null)
    print(a:tojson()) --> [1,2,3.45,"test",true,null]
    a:insert(0,0)
    print(a:tojson()) --> [0,1,2,3.45,"test",true,null]
array:del (pos)
Deletes an element at position.

Parameters:

  • pos index of item to be removed.

Returns:

    the updated length of array.

Usage:

    local a = JSON:array(0,1,2,3.45,"test",true,null)
    print(a:tojson()) --> [0,1,2,3.45,"test",true,null]
    print(a:len()) --> 30
    print(#a) --> 7
    local n = a:del(4)
    print(a:tojson()) --> [0,1,2,3.45,true,null]
    print(n)  --> 6
    print(#a) --> 6
    print(a:len()) --> 23
array:reverse ()
reverses the indexes of the elements of an array.

Returns:

    1 on success, 0 on failure.

Usage:

    local a = JSON:array(0,1,2,3.45,"test",true,null)
    print(a:tojson()) -->  [0,1,2,3.45,"test",true,null]
    a:reverse()
    print(a:tojson()) --> [null,true,"test",3.45,2,1,0]
array:push (elm)
Adds element to the end of an array.

Parameters:

  • elm Any

Returns:

    the updated length of array.

Usage:

    local a = JSON:array(0,1,2,3.45,true,null)
    print(a:tojson()) --> [0,1,2,3.45,true,null]
    print(a:len()) --> 23
    print(#a) --> 6
    local n = a:push("test")
    print(a:tojson()) --> [0,1,2,3.45,true,null,"test"]
    print(n)  --> 7
    print(#a) --> 7
    print(a:len()) --> 30
array:pop ()
Pops and returns the last element of an array.

Returns:

    the popped element or nil if array is empty.

Usage:

    local a = JSON:array(0,1,2,3.45,true,null)
    print(a:tojson()) --> [0,1,2,3.45,true,null]
    print(a:len()) --> 23
    print(#a) --> 6
    local n = a:pop()
    print(a:tojson()) --> [0,1,2,3.45,true]
    print(n)  --> 5
    print(#a) --> 5
    print(a:len()) --> 11
array:shift ()
Removes element at index 0.

Returns:

    the removed element or nil if array is empty.

Usage:

    local a = JSON:array(0,1,2,3.45,true,null)
    print(a:tojson()) --> [0,1,2,3.45,true,null]
    print(a:len()) --> 23
    print(#a) --> 6
    local n = a:shift()
    print(a:tojson()) --> [1,2,3.45,true,null]
    print(n)  --> 0
    print(#a) --> 5
    print(a:len()) --> 21
array:unshift (elm[, ...])
adds one or more elements to the beginning of an array.

Parameters:

  • elm Any
  • ... Any (optional)

Returns:

    the updated length of the array.

Usage:

    local a = JSON:array(0,1,2,3.45,true,null)
    print(a:tojson()) --> [0,1,2,3.45,true,null]
    print(a:len()) --> 23
    print(#a) --> 6
    local n = a:unshift(9,8,7)
    print(a:tojson()) --> [9,8,7,0,1,2,3.45,true,null]
    print(n)  --> 9
    print(#a) --> 9
    print(a:len()) --> 29
array:ref ()
Creates a new reference to an existing array. (pass by reference)

Returns:

    reference to array.

See also:

Usage:

    local a_ref = a:ref()
    print(a_ref[0]) --> 1
    print(a_ref[#a_ref - 1]) --> true
    a_ref[0] = 20
    print(a_ref[0], a[0]) --> 20    20
array:unref ()
Creates an unreferenced copy of an existing array. (pass by value)

Returns:

    unreference an array. (create a seperate copy)

See also:

Usage:

    local a_copy = a:unref()
    print(a_copy[0]) --> 1
    a_copy[0] = 20
    print(a_copy[0], a[0]) --> 20    1
generated by LDoc 1.5.0 Last updated 2026-02-07 08:45:50