Add a value to the end of a JSON array.
void json_array::add(var@ value);
void main() {
json_array@ arr = parse_json("[]");
for (uint i = 0; i < 5; i++)
arr.add(random(1, 100));
alert("Filled array", arr.stringify());
}
Clears the JSON array completely.
void json_array::clear();
void main() {
string data = "[1, 2, 3, 4, 5]";
json_array@ arr = parse_json(data);
alert("Before being cleared", arr.stringify());
arr.clear();
alert("After being cleared", arr.stringify());
}
Determine if the value at the given index is a JSON array or not.
bool json_array::is_array(uint index);
bool: true if the value at the specified index is a JSON array, false otherwise.
void main() {
json_array@ arr = parse_json("[[1, 2], [3, 4]]");
alert("Info", arr.is_array(1) ? "It is an array": "It is not an array");
}
Determine if the value at the given index is null.
bool json_array::is_null(uint index);
bool: true if the value at the specified index is null, false otherwise.
void main() {
json_array@ arr = parse_json("[1, 2, null, 4]");
alert("Info", "Index 1 " + (arr.is_null(1) ? " is null": " is not null"));
alert("Info", "Index 2 " + (arr.is_null(2) ? " is null": " is not null"));
}
Determine if the value at the given index is a JSON object.
bool json_array::is_object(uint index);
bool: true if the value at the specified index is a JSON object, false otherwise.
void main() {
json_array@ arr = parse_json("""[{}, {}, "test", {}]""");
alert("Info", "Position 0 " + (arr.is_object(0) ? "is an object" : "is not an object"));
alert("Info", "Position 2 " + (arr.is_object(2) ? "is an object": "is not an object"));
}
Remove a value from the JSON array given an index.
void json_array::remove(uint index);
void main() {
string data = """[47, 4, 584, 43, 8483]""";
json_array@ arr = parse_json(data);
alert("Initial", arr.stringify());
arr.remove(2);
alert("After removal", arr.stringify());
}
Return the size of the JSON array.
uint json_array::size();
void main() {
string data = "[";
for (int i = 0; i < random(10, 20); i++)
data += i + ", ";
// Strip off the trailing comma and space character, as they'll make the JSON throw a syntax error.
data.erase(data.length() - 1);
data.erase(data.length() - 1);
data += "]";
clipboard_set_text(data);
json_array@ arr = parse_json(data);
alert("Info", "The JSON array contains " + arr.size() + " items");
}
Return the JSON array as a string.
string json_array::stringify(int indent = 0, int step = -1);
int indent = 0: specifies how many spaces to use to indent the JSON. If 0, no indentation or formatting is performed.
int step = -1: the outwards indentation step, for example setting it to two would cause your outdents to only be two spaces. -1 (the default) keeps it how it is, normally you don't need to use this parameter.
string: the JSON array as a string.
void main() {
string data = """["Apple", "Banana", "Orange", "Lemon"]""";
json_array@ arr = parse_json(data);
alert("JSON array", arr.stringify(4));
}
Get a value out of the JSON array with literal syntax, for example arr[2].
var@ json_array::get_opIndex(uint index) property;
var@: a handle to the object, or null if not found.
void main() {
json_array@ cats = parse_json("""["Athena", "Willow", "Punk", "Franky", "Yoda", "Waffles"]""");
alert("The third cat is", cats[2]);
for (uint i = 0; i < cats.size(); i++)
alert("Awww", "Hi " + cats[i]);
}
Get a value out of a JSON array using a query.
var@ json_array::opCall(string query);
var@: a handle to the object, or null if it couldn't be found.
Queries are formatted like so, using key names and indexes separated with ".":
world.player.0
It can be as nested as you like.
void main() {
string data = """[[["Colorado", "Kansas", "Minnesota"]]]""";
json_array@ arr = parse_json(data);
string location = arr("0.0.0");
alert("My favorite state is", location);
}
Set a value in the JSON array at a particular position.
void json_array::set_opIndex(uint index, var@ value) property;
uint index: the index to insert the item at.
var@ value: the value to set.
void main() {
json_array@ arr = parse_json("[]");
arr[0] = "meow";
alert("Info", "Cats say " + arr[0]);
}
Determine if the JSON array is empty.
const bool json_array::empty;
void main() {
json_array@ arr = parse_json("[]");
json_array@ arr2 = parse_json("[1, 2, 3]");
alert("First is empty", arr.empty ? "true" : "false");
alert("Second is empty", arr2.empty ? "true" : "false");
}
Determines the amount of escaping that occurs in JSON parsing.
bool json_array::escape_unicode;
If this property is true, escaping will behave like normal. If it's false, only the characters absolutely vital to JSON parsing are escaped.
Clears the JSON object completely.
void json_object::clear();
void main() {
string data = """{"numbers": [1, 2, 3, 4, 5]}""";
json_object@ o = parse_json(data);
alert("Before being cleared", o.stringify());
o.clear();
alert("After being cleared", o.stringify());
}
Determine if a key exists in the JSON object.
bool json_object::exists(const string?&in key);
bool: true if the key exists, false if not.
void main() {
string data = """{"engine": "NVGT"}""";
json_object@ o = parse_json(data);
alert("Info", (o.exists("engine") ? "The key exists" : "The key does not exist"));
}
Get a list of all the keys in the JSON object.
string[]@ json_object::get_keys();
string[]@: a handle to an array of strings containing all the keys of the JSON object.
Note that this function isn't recursive, you only get the keys for the object you're running this function on, not any child objects.
void main() {
string data = """{"thing": 1, "other_thing": "test", "another": true}""";
json_object@ o = parse_json(data);
string[]@ keys = o.get_keys();
alert("The keys are", join(keys, ", "));
}
Determine if the value with the given key is a JSON array or not.
bool json_object::is_array(string key);
bool: true if the value with the specified key is a JSON array, false otherwise.
void main() {
string data = """{"classes": ["json_object", "json_array"]}""";
json_object@ o = parse_json(data);
alert("Info", o.is_array("classes") ? "array": "nonarray");
}
Determine if the value with the given key is null.
bool json_object::is_null(string key);
bool: true if the value with the specified key is null.
void main() {
string data = """{"brain": null}""";
json_object@ o = parse_json(data);
alert("Info", o.is_null("brain") ? "null" : "not null");
}
Determine if the value with the given key is a JSON object or not.
bool json_object::is_object(string key);
bool: true if the value with the specified key is a JSON object, false otherwise.
void main() {
string data = """{"json_object": {}}""";
json_object@ o = parse_json(data);
alert("Info", o.is_object("json_object") ? "Object" : "Non-object");
}
Remove a value from the JSON object given a key.
void json_object::remove(const string&in key);
void main() {
string data = """{"name": "Quin"}""";
json_object@ o = parse_json(data);
alert("Initial", o.stringify());
o.remove("name");
alert("After removal", o.stringify());
}
Set a value in a JSON object.
void json_object::set(const string&in key, var@ value);
const string&in key: the key to give the value.
var@ value: a handle to the value to be set.
void main() {
json_object@ o = parse_json("{}");;
o.set("nvgt_user", true);
alert("Info", (bool(o["nvgt_user"]) ? "You are an NVGT user" : "You are not a regular NVGT user"));
}
Return the size of the JSON object.
uint json_object::size();
Note that this function returns the number of top-level keys; it's not recursive.
void main() {
string data = """{"numbers": [1, 2, 3, 4, 5]}""";
json_object@ o = parse_json(data);
alert("Info", "The json object's size is " + o.size());
}
Return the JSON object as a string.
string json_object::stringify(int indent = 0, int step = -1);
int indent = 0: specifies how many spaces to use to indent the JSON. If 0, no indentation or formatting is performed.
int step = -1: the outwards indentation step, for example setting it to two would cause your outdents to only be two spaces. -1 (the default) keeps it how it is, normally you don't need to use this parameter.
string: the JSON object as a string.
void main() {
string data = """{"name": "Quin", "age": 18}""";
json_object@ o = parse_json(data);
alert("JSON object", o.stringify(4));
}
Get a value out of the JSON object with literal syntax, for example json["key"].
var@ json_object::get_opIndex(const string&in key) property;
var@: a handle to the object, or null if not found.
void main() {
string data = """{"name": "Quin", "age": 18}""";
json_object@ o = parse_json(data);
alert("Info", o["name"] + " is " + o["age"]);
}
Get a value out of a JSON object using a query.
var@ json_object::opCall(const string&in query);
var@: a handle to the object, null if not found.
Queries are formatted like so:
first_field.subfield.subfield
It can be as nested as you like.
This method also works on json_array's, and you access array indexes just with their raw numbers, like so:
world.players.0
void main() {
string data = """{"countries":{"us":["Colorado", "Kansas", "Minnesota"]}}""";
json_object@ o = parse_json(data);
string locations = o("countries.us");
alert("My favorite states are", locations);
}
Set a value in the JSON object with a particular key.
void json_object::set_opIndex(const string&in key, var@ value) property;
const string&in key: the key of the object.
var@ value: the value to set.
void main() {
json_object@ o = parse_json("{}");
o["name"] = "Quin";
alert("Hi", "I am " + o["name"]);
}
Determines the amount of escaping that occurs in JSON parsing.
bool json_object::escape_unicode;
If this property is true, escaping will behave like normal. If it's false, only the characters absolutely vital to JSON parsing are escaped.
Add a file on disk to a pack.
bool pack::add_file(const string&in disk_filename, const string&in pack_filename, bool allow_replace = false);
bool: true if the file was successfully added, false otherwise.
Add content stored in memory to a pack.
bool pack::add_memory(const string&in pack_filename, const string&in data, bool replace = false);
bool: true if the data was successfully added, false otherwise.
Closes a pack, freeing all its resources.
bool pack::close();
bool: true if the pack was successfully closed, false otherwise.
Attempt to remove a file from a pack.
bool pack::delete_file(const string&in filename);
bool: true if the file was successfully deleted, false otherwise.
Query whether or not a file exists in your pack.
bool pack::file_exists(const string&in filename);
bool: true if the file exists in the pack, false if not.
Get the name of a file with a particular index.
string pack::get_file_name(int index);
string: the name of the file if found, or an empty string if not.
The index you pass to this function is the same index you'd use to access an element in the return value from pack::list_files().
Get the offset of a file in your pack.
uint pack::get_file_offset(const string&in filename);
uint: the offset of the file (in bytes).
Do not confuse this with the offset parameter in the pack::read_file method. This function is provided encase you wish to re-open the pack file with your own file object and seek to a file's data within that external object. The offset_in_file parameter in the read_file method is relative to the file being read.
Returns the size of a particula file in the pack.
uint pack::get_file_size(const string&in filename);
uint: the size of the file (in bytes), or 0 if no file with that name was found.
Get a list of all the files in the pack.
string[]@ pack::list_files();
string[]@: a handle to an array of strings containing the names of every file in the pack.
Open a pack to perform operations on it.
bool pack::open(const string&in filename, uint mode, bool memload = false);
pack_open_modes for more information).bool: true if the pack was successfully opened with the given mode, false otherwise.
To open an embedded pack, prefix it with *, like this:
pack.open("*embedded.dat");
For BGT compatibility, you can use a single * to open the first available embedded pack.
pack.open("*");
Get the contents of a file contained in a pack.
string pack::read_file(const string&in pack_filename, uint offset_in_file, uint size);
pack::get_file_size to read the entire file).string: the contents of the file.
This function allows you to read chunks of data from a file, as well as an entire file in one call. To facilitate this, the offset and size parameters are provided. offset is relative to the file being read E. 0 for the beginning of it. So to read a file in one chunk, you could execute:
string file_contents = my_pack.read_file("filename", 0, my_pack.get_file_size("filename"));
Set the identifier of this pack object (e.g. the first 8-bytes that determine if you have a valid pack or not).
bool pack::set_pack_identifier(const string&in ident);
bool: true if the pack's identifier was properly set, false otherwise.
The regexp object allows for the easy usage of regular expressions in NVGT.
regexp(const string&in pattern, int options = RE_UTF8);
const string&in pattern: the regular expression's pattern (see remarks).
int options = RE_UTF8: a combination of any of the values from the regexp_options enum.
Regular expressions are a language used for matching, substituting, and otherwise manipulating text. To learn about the regular expression syntax that nVGT uses (called PCRE), see this link: https://en.wikipedia.org/wiki/Perl_Compatible_Regular_Expressions
void main() {
regexp re("^[a-z]{2,4}$"); // Will match any lowercase alphabetical string between 2 and 4 characters in length.
string compare = input_box("Text", "Enter the text to check against the regular expression.");
if (compare.empty()) {
alert("Regular expression tester", "You didn't type a string to test.");
exit(1);
}
bool matches = re.match(compare);
alert("The regular expression", matches ? "matches" : "does not match");
}
Determine if the regular expression matches against a particular string or not.
bool regexp::match(const string&in subject, uint64 offset = 0);bool regexp::match(const string&in subject, uint64 offset, int options);regexp_options enum.bool: true if the regular expression matches the given string, false if not.
This is a list of all the possible open modes to be passed to the pack::open() function.
This enum holds various constants that can be passed to the regular expression classes in order to change their behavior.
Portions of this regexp_options documentation were Copied from POCO header files.
See the PCRE documentation for more information.
Return the character that corresponds to the given ascii value.
string ascii_to_character(uint8 ascii);
string: the character for the given ascii value.
void main() {
uint8 ascii = parse_int(input_box("ASCII value", "Enter the ascii value to convert."));
alert("Info", ascii + " has a value of " + ascii_to_character(ascii));
}
Return the ascii value for the given character.
uint8 character_to_ascii(const string&in character);
uint8: the ascii value for the given character.
If a string containing more than one character is passed as input to this function, only the left-most character is checked.
If an empty string is passed, the function returns 0.
void main() {
string character = input_box("Character", "Enter a character to convert");
if (character.is_empty()) {
alert("Error", "You did not type anything.");
exit();
}
if (character.length() != 1) { // The user typed more than one character.
alert("Error", "You must only type a single character.");
exit();
}
alert("Info", character + " has an ascii value of " + character_to_ascii(character));
}
turn an array into a string, with each element being separated by the given delimiter.
string join(const string[]@ elements, const string&in delimiter);
const string[]@ elements: a handle to the array to join.
const string&in delimiter: the delimiter used to separate each element in the array (can be empty).
string: the given array as a string.
If an empty array is passed, a blank string is returned.
The delimiter is not appended to the end of the last item in the returned string. For example if an array contains [1, 2, 3] and you join it with a delimiter of . (period), the result would be "1.2.3" without an extra delimiter appended.
void main() {
string[] names = {"Sam", "Quin", "Patrick"};
string people = join(names, ", ");
alert("Info", people);
}
Convert a number into its string equivalent, for example, one thousand four hundred and fifty six.
string number_to_words(int64 the_number, bool include_and = true);
int64 the_number: the number to convert.
bool include_and = true: whether or not to include the word "and" in the output.
string: the specified number, converted to a readable string.
At this time, this function only produces English results.
void main() {
int64 num = random(1000, 100000);
string result = number_to_words(num);
alert("Info", num + " as a string is " + result);
}
Set the global identifier of all your packs (e.g. the first 8-bytes that determine if you have a valid pack or not).
bool pack_set_global_identifier(const string&in ident);
bool: true if the identifier was properly set, false otherwise.
Test if the text matches the specified regular expression.
bool regexp_match(const string&in text, const string&in pattern);
const string&in text: the text to compare against the regular expression.
const string&in pattern: the regular expression to match.
bool: true if the text matches the given regular expression, false otherwise.
If you would like a lower level interface to regular_expressions in NVGT, check out the regexp class which this function more or less wraps.
To learn more about regular expressions, visit: https://en.wikipedia.org/wiki/Regular_expression
void main() {
bool match = regexp_match("Test", "^[a-zA-Z]+$");
alert("the regular expression", match ? "matches" : "does not match");
}
Decodes a string from base32.
string string_base32_decode(const string&in the_data);
String: The decoded string on success or an empty string on failure.
You can learn more about the base32 format here.
void main() {
string text = input_box("Text", "Enter the text to decode.");
if (text.is_empty()) {
alert("Error", "You did not type any text.");
exit();
}
alert("Info", string_base32_decode(text));
}
Encodes a string as base32.
string string_base32_encode(const string&in the_data);
String: The encoded string on success or an empty string on failure.
You can learn more about the base32 format here.
void main() {
string text = input_box("Text", "Enter the text to encode.");
if (text.is_empty()) {
alert("Error", "You did not type any text.");
exit();
}
alert("Info", string_base32_encode(text));
}
Normalize a string to conform to the base32 spec.
string string_base32_normalize(const string&in the_data);
string: the normalized string.
The primary thing this function does is to skip separators, convert all letters to uppercase, and make sure the length is a multiple of 8.
To learn more about base32, click here.
void main() {
string text = input_box("Text", "Enter the text to normalize.");
if (text.is_empty()) {
alert("Error", "You didn't type anything.");
exit();
}
alert("Info", string_base32_normalize(text));
}
Decodes a string which has previously been encoded in base64.
string string_base64_decode(const string&in the_data, string_base64_options options = STRING_BASE64_PADLESS);
const string&in the_data: The data that is to be decoded.
string_base64_options options = STRING_BASE64_PADLESS: A bitwise of options that control the behavior of the decoding (see remarks).
String: The decoded string on success or an empty string on failure.
The following options may be passed to the options argument of this function:
STRING_BASE64_DEFAULT: The defaults passed to the string_base64_encode function.
STRING_BASE64_URL: Use base64 URL encoding as opposed to the / and = characters.
STRING_BASE64_PADLESS: Allows decoding even if the string does not end with = padding characters.
You can learn more about base64 here.
void main() {
alert("example", string_base64_decode("aGVsbG8=")); // Should print "hello".
}
Encodes a string as base64.
string string_base64_encode(const string&in the_data, string_base64_options options = STRING_BASE64_DEFAULT);
const string&in the_data: The data that is to be encoded.
string_base64_options options = STRING_BASE64_DEFAULT: A bitwise of options that control the behavior of the encoding (see remarks).
String: The encoded string on success or an empty string on failure.
The following options may be passed to the options argument of this function:
STRING_BASE64_DEFAULT: The default options (i.e. insert padding and do not use URL encoding).
STRING_BASE64_URL: Use base64 URL encoding as opposed to the / and = characters.
STRING_BASE64_PADLESS: Allows decoding even if the string does not end with = padding characters.
You can learn more about base64 here.
void main() {
alert("example", string_base64_encode("Hello")); // Should print SGVsbG8=
}
Represents the identifier currently being used for all your packs.
const string pack_global_identifier;