Can I use this wrapper around JSON inside my native module?

Godot Version

4.2

Question

Can I use the sugar for JSON module? (And the same for other instantiated resources)

Dictionary MyUtilityClass::jsonParse(String str) {
	Ref<JSON> json;
	json.instantiate();
	Error error = json->parse(str);
	if (error != OK) {
		ERR_PRINT(vformat("Validate extension JSON: Error parsing '%s' at line %d: %s", str, json->get_error_line(), json->get_error_message()));
		return Dictionary();
	}
	return json->get_data();
}

Do I have some memory leaks or other troubles of this approach? How to destroy instantiated json?

If you truly want to use JSON, follow this guide: JSON In Godot - Complete Guide - GameDev Academy

Otherwise I would recommend you to just store a dictionary like this:

const path = "user://my_game.save"
func save_info():
	var content = [info, settings]
	var f = File.new()
	f.open(path, File.WRITE)
	f.store_var(content)
	f.close()
func load_info():
	var content
	var f = File.new()
	if f.file_exists(path):
		var error = f.open(path, File.READ)
		if error == OK:
			content = f.get_var()
			info = content[0]
			settings = content[1]
			f.close()

This way you have way more integrated storage system that also works on mobile.

I need it for parsing http-like response inside C++ module only. And all examples inside Godot editor code looks like too verbose…

I don’t want to write

Ref<JSON> json;
json.instantiate();
Error error = json->parse(str);
if (error != OK) {
	ERR_PRINT(vformat("Validate extension JSON: Error parsing '%s' at line %d: %s", str, json->get_error_line(), json->get_error_message()));
	return Dictionary();
}
return json->get_data();

instead of JSON->parse() every time I need parse a string.

P.S. That article is just for gdscript

In cpp I use struct maps for sth like that, but this doesnt look anything like that

I get data from WebView and JSON is default format