Utility methods of GS2-Script

description of utility methods available in Lua scripts executed by GS2-Script

Extension methods available in GS2-Script extension scripts (Lua language).

util.table_to_json

Convert a Lua table type (array) to a JSON format string.

Request

Argument nameTypeDescription
tabletableLua table

Result

Member nameTypeDescription
isErrorboolpresence of error
statusCodeintstatus code
errorMessagestringerror message
resultstringJSON string of the conversion result

Sample

Code

result = util.table_to_json({a="a", b=1, c=false})
if result.isError then
  fail(result['statusCode'], result['errorMessage'])
end
json_str = result["result"]

Output

{"a":"a","b":1,"c":false}

util.json_to_table

Converts a string in JSON format to a Lua table type (array).

Request

Argument nameTypeDescription
jsonTextstringstring in JSON format

Result

Member nameTypeDescription
isErrorboolpresence of error
statusCodeintstatus code
errorMessagestringerror message
resulttableLua table of conversion results

Sample

Code

result = util.json_to_table("{\"a\": \"a\", \"b\": 1, \"c\": false}")
if result.isError then
  fail(result['statusCode'], result['errorMessage'])
end
json_table = result["result"]

util.split

Split a string.

Request

Argument nameTypeDescription
valuestringoriginal string
sepstringdelimiter

Result

Member nameTypeDescription
isErrorboolpresence of error
statusCodeintstatus code
errorMessagestringerror message
resulttableLua table of split strings

Sample

Code

result = util.split("a,b,c", ",")
if result.isError then
  fail(result['statusCode'], result['errorMessage'])
end
split_table = result["result"]
print(split_table[1])
print(split_table[2])
print(split_table[3])

Output

a
b
c

http.get

Issues HTTP GET requests.

Request

Argument nameTypeDescription
urlstringURL of the connection

Result

Member nameTypeDescription
isErrorboolpresence of error
statusCodeintstatus code
errorMessagestringerror message
resultstringHTTP Response Body

Sample

Code

result = http.get("https://example.com")
if result.isError then
  fail(result['statusCode'], result['errorMessage'])
end
get_result = result["result"]

http.post

Issue an HTTP POST request.

Request

Argument nameTypeDescription
urlstringURL of the connection
contentTypestringContent-Type of the HTTP header
bodystringHTTP request message body

Result

Member nameTypeDescription
isErrorboolpresence of error
statusCodeintstatus code
errorMessagestringerror message
resultstringHTTP Response Body

Sample

Code

result = http.get("https://example.com")
if result.isError then
  fail(result['statusCode'], result['errorMessage'])
end
get_result = result["result"]

util.random

Generate a random floating point number between 0 and 1.

Request

Argument nameTypeDescription

Result

member nametypedescription
isErrorboolpresence of error
statusCodeintstatus code
errorMessagestringerror message
resultfloatgenerated random number

Sample

Code

result = util.random()
if result.isError then
  fail(result['statusCode'], result['errorMessage'])
end
random_value = result["result"]