This function converts a single struct or a hierarchy of nested structs and arrays into a valid JSON string.
You supply the initial value to use (a Struct or an Array) and then the function will "stringify" it, i.e. convert it into a JSON string (converting GameMaker arrays into JSON arrays, and GameMaker structs into JSON objects). You can optionally choose to "prettify" it, meaning the final JSON will be formatted for easy readability.
NOTE See Guide To Using JSON for detailed information on how to work with JSON in GameMaker.
The function optionally takes a Function, which runs once for each value in the structure, including all nested structs/arrays and all the values inside them.
It takes two arguments (key, value) where key is the struct key name (String) or array index (Real), and value is what's stored in that key/index.
The filter function must always return a value. It can return a new value, which replaces the key's value in the final converted format returned by json_stringify. If no new value should be returned for a particular key, the function must return the original value.
NOTE When overriding a key's value in the filter function (using return), make sure you check its type first, as the filter function runs for the root structure and any nested structures as well, meaning accidentally overriding them will result in a broken final structure. See Example 3 at the bottom.
json_stringify(val, [pretty_print], [filter_func]);
Argument | Type | Description |
---|---|---|
val | Struct or Array | The reference value for a struct or array to convert into a JSON string |
prettify | Boolean | OPTIONAL Whether to prettify the string, i.e. insert indentation and line breaks for readability |
filter_func | Function | OPTIONAL Filter function that processes each item. Syntax: function(key, value) -> new_value |
var _contents =
{
version : "1.0.0",
data:
{
coins : 4,
mana : 15,
playername : "Gurpreet",
items :
[
ITEM.SWORD,
ITEM.BOW,
ITEM.GUITAR
]
}
};
var _json_string = json_stringify(_contents);
The above code will convert the _contents struct into a JSON string and stores the string in a variable. The returned string would look like this:
{ "data": { "items": [ 0.0, 1.0, 2.0 ], "coins": 4.0, "mana": 15.0, "playername": "Gurpreet" }, "version": "1.0.0" }
var _contents =
{
version: "1.0.0",
data:
{
coins : 5,
mana : 0,
playername : "Bart",
items :
[
ITEM.SWORD,
ITEM.BOW,
ITEM.PIANO
]
}
}
var _json_string = json_stringify(_contents, true);
The above code converts the _contents struct into a JSON string and stores it in a variable. With the pretty_print parameter set to true, the struct's contents are pretty printed to the string, i.e. indentation and line breaks are inserted to make the resulting string look "pretty" and more readable. The string then looks like this:
{
"data":{
"mana":0.0,
"playername":"Bart",
"items":[
0,
1,
2
],
"coins":5.0
},
"version":"1.0.0"
}
var data =
{
x: 5.2344,
y: 10.601,
last_clicked: undefined,
values : [ 2000.1, 30.56, undefined, { slot : 10, skin : undefined } ]
}
var json = json_stringify(data, true, function(key, value)
{
if (is_real(value)) return round(value);
if (is_undefined(value)) return 0;
return value;
});
show_debug_message(json);
The above code takes a struct and stringifies it to JSON, pretty-printing it, and using a filter function to modify some types of values.
If the value is a Real, it rounds it, if it's undefined, it changes it to 0, otherwise it simply returns the same value (meaning it remains unchanged).