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 name | Type | Description |
---|
table | table | Lua table |
Result
Member name | Type | Description |
---|
isError | bool | presence of error |
statusCode | int | status code |
errorMessage | string | error message |
result | string | JSON 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 name | Type | Description |
---|
jsonText | string | string in JSON format |
Result
Member name | Type | Description |
---|
isError | bool | presence of error |
statusCode | int | status code |
errorMessage | string | error message |
result | table | Lua 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 name | Type | Description |
---|
value | string | original string |
sep | string | delimiter |
Result
Member name | Type | Description |
---|
isError | bool | presence of error |
statusCode | int | status code |
errorMessage | string | error message |
result | table | Lua 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
http.get
Issues HTTP GET requests.
Request
Argument name | Type | Description |
---|
url | string | URL of the connection |
Result
Member name | Type | Description |
---|
isError | bool | presence of error |
statusCode | int | status code |
errorMessage | string | error message |
result | string | HTTP 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 name | Type | Description |
---|
url | string | URL of the connection |
contentType | string | Content-Type of the HTTP header |
body | string | HTTP request message body |
Result
Member name | Type | Description |
---|
isError | bool | presence of error |
statusCode | int | status code |
errorMessage | string | error message |
result | string | HTTP 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 name | Type | Description |
---|
Result
member name | type | description |
---|
isError | bool | presence of error |
statusCode | int | status code |
errorMessage | string | error message |
result | float | generated random number |
Sample
Code
result = util.random()
if result.isError then
fail(result['statusCode'], result['errorMessage'])
end
random_value = result["result"]