Check out our latest project ✨ OpenChapter.io: free ebooks the way its meant to be 📖

SimpleJsonClassConverter

An asset by kyo83
The page banner background of a mountain and forest
SimpleJsonClassConverter hero image

Quick Information

0 ratings
SimpleJsonClassConverter icon image
kyo83
SimpleJsonClassConverter

Utilities to convert godot classes to JSON and vice versaAims for simplicity, performance and support all types

Supported Engine Version
4.3
Version String
1.0.2
License Version
MIT
Support Level
community
Modified Date
11 months ago
Git URL
Issue URL

SimpleJsonClassConverter

This GDScript provides a set of utility functions for converting Godot classes to JSON dictionaries and vice versa.

Features

  • Serialization (Class to JSON):

    • Converts Godot class instances to JSON-like dictionaries.
    • Handles nested objects and arrays recursively.
    • Supports saving JSON data to files (with optional encryption).
    • Will store all scripts names and types in JSON.
    • Support resources.
    • Supports dictionnaries.
  • Deserialization (JSON to Class):

    • Loads JSON data from files (with optional decryption).
    • No need to specify target class.
    • Converts JSON strings and dictionaries into class instances.
    • Handles nested object structures.
  • Automatic Type Recognition: Intelligently handles various data types.

Installation

  1. Create a new script file named SimpleJsonClassConverter.gd (or similar) in your Godot project.
  2. Copy and paste the code provided into the script file.

Usage

1. Class to JSON

a) Convert a Class Instance to a JSON Dictionary:

# Assume you have a class named 'SaveData':
var save_data = SaveData.new()
# ... (Set properties of save_data)

# Convert to a JSON dictionary:
var json_data = SimpleJsonClassConverter.class_to_json(save_data) 

# json_data now contains a Dictionary representation of your class instance

b) Save JSON Data to a File:

var file_success: bool = SimpleJsonClassConverter.store_json_file("user://saves/save_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.") 

2. JSON to Class

a) Load JSON Data from a File:

var loaded_data: SaveData = SimpleJsonClassConverter.json_file_to_class("user://saves/save_data.json", "my_secret_key") # Optional decryption key

if loaded_data:
    # ... (Access properties of the loaded_data)
else:
    print("Error loading player data.")

b) Convert a JSON Dictionary to a Class Instance:

var json_dict = { "name": "Bob", "score": 2000 }
var player_data: PlayerData = SimpleJsonClassConverter.json_to_class(json_dict)

Important Notes

  • Exported Properties: Only exported properties (those declared with @export) or properties with the [PROPERTY_USAGE_STORAGE] meta will be serialized and deserialized.

  • Property naming: Don't use "gd_script" or "gd_type" in any property name or dictionary key as they are used for internal type storage for Serialization and Deserialization.

Credits

Inspired by https://github.com/EiTaNBaRiBoA/JsonClassConverter.

Thanks to him

Utilities to convert godot classes to JSON and vice versa
Aims for simplicity, performance and support all types

Reviews

0 ratings

Your Rating

Headline must be at least 3 characters but not more than 50
Review must be at least 5 characters but not more than 500
Please sign in to add a review

Quick Information

0 ratings
SimpleJsonClassConverter icon image
kyo83
SimpleJsonClassConverter

Utilities to convert godot classes to JSON and vice versaAims for simplicity, performance and support all types

Supported Engine Version
4.3
Version String
1.0.2
License Version
MIT
Support Level
community
Modified Date
11 months ago
Git URL
Issue URL

Open Source

Released under the AGPLv3 license

Plug and Play

Browse assets directly from Godot

Community Driven

Created by developers for developers