Class array

JSON.array Class

Methods

array:tojson (start, end, escape) render an array as valid json array
array:tolua (start, end, escape) render an array as lua table dump
array:totable () return lua json array as a lua table
array:move (move, to) Move an element to position.
array:insert (pos, elm) Inserts an element at position.
array:del (pos) Deletes an element at position.
array:reverse (from, to) 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.


Methods

array:tojson (start, end, escape)
render an array as valid json array

Parameters:

  • start number: start position (optional)
  • end number end position (optional)
  • escape boolean: escape output (optional)

Returns:

    json string

See also:

Usage:

  • print(a:tojson()) --> [1,2,3.45,"test",true,null]
  • print(a:tojson(0)) --> [1]
  • print(a:tojson(4,5)) --> [true,null]
  • print(a:tojson(0,4,true)) --> [1,2,3.45,\"test\",true]
array:tolua (start, end, escape)
render an array as lua table dump

Parameters:

  • start number: start position (optional)
  • end number end position (optional)
  • escape boolean: escape output (optional)

Returns:

    lua tuble as string

See also:

Usage:

  • print(a:tolua()) --> {1,2,3.45,"test",true,null}
  • print(a:tolua(0)) --> {1}
  • print(a:tolua(4,5)) --> {true,null]}
  • print(a:tolua(0,4,true)) --> {1,2,3.45,\"test\",true}
array:totable ()
return lua json array as a lua table

Returns:

    lua table

See also:

Usage:

    t = a:totable()
    print(a) --> array: 0x627a723b1ad0
    print(t) --> table: 0x627a723b70d0
    print(t[4]) --> test
    print(a[3]) --> test
array:move (move, to)
Move an element to position.

Parameters:

  • move number: index of item to move.
  • to number: index to move item to

Returns:

    no value returned

Usage:

    local a = JSON:array(1,2,3.45,"test",true,null)
    print(a:tojson()) --> [1,2,3.45,"test",true,null]
    a:move(0,5)
    print(a:tojson()) --> [2,3.45,"test",true,null,1]
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 (from, to)
reverses the indexes of the elements of an array.

Parameters:

  • from index to start of reversal. (optional)
  • to index to end of reversal. (optional)

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(0,3)
    print(a:tojson()) -->  [3.45,2,1,0,"test",true,null]
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-05-09 15:28:03