This GDScript provides a powerful set of utility functions for converting Godot classes to JSON dictionaries and vice versa, simplifying serialization and deserialization tasks.
.tres
files or directly embed their data.@export
) JsonClassConverter.gd
file from this repository.JsonClassConverter.gd
file in your Godot project folder (e.g., in a scripts/
directory).a) Convert a Class Instance to a JSON Dictionary:
# Assuming you have a class named 'PlayerData' (see Example section):
var player_data = PlayerData.new()
# ... Set properties of player_data ...
# Convert to a JSON dictionary:
# Option 1: Save resources inline within the JSON (default)
var json_data = JsonClassConverter.class_to_json(json_data)
# Option 2: Save resources as separate temporary .tres files (in 'user://temp_resource/')
var json_data = JsonClassConverter.class_to_json(json_data, true)
json_data now holds a Dictionary representation of your class instance.
# Option 3: Convert a Class Instance to a JSON String
var json_string: String = JsonClassConverter.class_to_json_string(player_data)
b) Save JSON Data to a File:
var file_success: bool = JsonClassConverter.store_json_file("user://saves/player_data.json", json_data, "my_secret_key") # Optional encryption key
# Check if saving was successful:
if file_success:
print("Player data saved successfully!")
else:
print("Error saving player data.")
a) Load JSON Data from a File:
var loaded_data: PlayerData = JsonClassConverter.json_file_to_class(PlayerData, "user://saves/player_data.json", "your_secret_key")
if loaded_data:
# ... Access properties of loaded_data ...
else:
print("Error loading player data.")
b) Convert a JSON String to a Class Instance:
var json_string = '{ "name": "Alice", "score": 1500 }'
var player_data: PlayerData = JsonClassConverter.json_string_to_class(PlayerData, json_string)
c) Convert a JSON Dictionary to a Class Instance:
var json_dict = { "name": "Bob", "score": 2000 }
var player_data: PlayerData = JsonClassConverter.json_to_class(PlayerData, json_dict)
@export
or the [PROPERTY_USAGE_STORAGE]
meta tag will be serialized and deserialized.castClass
argument (e.g., PlayerData
) matches the exact class name of the data you're loading. class_name PlayerData
@export var name: String
@export var score: int
@export var inventory: Array = []
# Create a PlayerData instance
var player = PlayerData.new()
player.name = "Bob"
player.score = 100
player.inventory = ["Sword", "Potion"]
# Save the player data to a JSON file
JsonClassConverter.store_json_file("user://player.sav", JsonClassConverter.class_to_json(player))
# Load the player data from the JSON file
var loaded_player: PlayerData = JsonClassConverter.json_file_to_class(PlayerData, "user://player.sav")
# Print the loaded player's name
print(loaded_player.name) # Output: Bob