FrostyCat
Redemption Seeker
JSON Toolkit v1.1.0
Companion scripts for json_encode() and json_decode()
Overview
JSON Toolkit is a set of companion scripts facilitating the use of GML's built-in json_encode() and json_decode() functions. It contains utilities for visually building JSON structure, accessing and manipulating deeply nested values, iterating through a JSON structure, and saving/loading JSON data in files. With JSON Toolkit, many common JSON operations that would otherwise take several lines, intermediate values and repetitions in conventional GML can be shortened into concise, easy-to-read one-liners.
Downloads
YoYo Marketplace: Link
GMS 2.x: Link | Repository
GMS 1.4: Link | Repository
Examples
Creating Nested JSON Structures
Accessing Nested JSON Structures
Loading from a JSON File
Feedback Welcome!
If you have any suggestions for new constructors/use cases or bug reports, please open an issue or contribute on GitHub.
Companion scripts for json_encode() and json_decode()
Overview
JSON Toolkit is a set of companion scripts facilitating the use of GML's built-in json_encode() and json_decode() functions. It contains utilities for visually building JSON structure, accessing and manipulating deeply nested values, iterating through a JSON structure, and saving/loading JSON data in files. With JSON Toolkit, many common JSON operations that would otherwise take several lines, intermediate values and repetitions in conventional GML can be shortened into concise, easy-to-read one-liners.
Downloads
YoYo Marketplace: Link
GMS 2.x: Link | Repository
GMS 1.4: Link | Repository
Examples
Creating Nested JSON Structures
Conventional GML:
With JSON Toolkit:
Code:
global.stats = ds_map_create();
ds_map_add_map(global.stats, "Alice", ds_map_create());
ds_map_add(global.stats[? "Alice"], "HP", 5);
ds_map_add(global.stats[? "Alice"], "ATK", 5);
ds_map_add(global.stats[? "Alice"], "DEF", 4);
ds_map_add_map(global.stats, "Bob", ds_map_create());
ds_map_add(global.stats[? "Bob"], "HP", 7);
ds_map_add(global.stats[? "Bob"], "ATK", 6);
ds_map_add(global.stats[? "Bob"], "DEF", 2);
Code:
global.stats = JsonStruct(JsonMap(
"Alice", JsonMap(
"HP", 5,
"ATK", 5,
"DEF", 4
),
"Bob", JsonMap(
"HP", 7,
"ATK", 6,
"DEF", 2
)
));
Accessing Nested JSON Structures
JSON String to Decode:
Conventional GML:
With JSON Toolkit:
Code:
[
{
"name": "Alice",
"HP": 5,
"ATK": 5,
"DEF": 4
},
{
"name": "Bob",
"HP": 7,
"ATK": 6,
"DEF", 2
}
]
Code:
// Access Bob's HP
var json_data = json_decode(json_str);
var bob_hp = json_data[? "default"];
bob_hp = bob_hp[| 1];
bob_hp = bob_hp[? "HP"];
Code:
// Access Bob's HP
var json_data = json_decode(json_str);
var bob_hp = json_get(json_data, 1, "HP");
Loading from a JSON File
Conventional GML:
With JSON Toolkit:
Code:
// Load from save.json
var f = file_text_open_read(working_directory + "save.json"),
json_str = "";
while (!file_text_eof(f)) {
json_str += file_text_read_string(f);
file_text_readln(f);
}
file_text_close();
var json_data = json_decode(json_str);
Code:
// Load from save.json
var json_data = json_load(working_directory + "save.json");
Feedback Welcome!
If you have any suggestions for new constructors/use cases or bug reports, please open an issue or contribute on GitHub.
Last edited: