You can find usages in the GDScript Unirest plugin
This is a fast serializer/deserializer written in GDScript to convert a JSON (Dictionary) to a class, using something similar to the Reflection concecpt.
json_to_class
can be used to convert a Dictionary to a Class. All keys in the Dictionary will be treated as variables and their types will be guessed in the best way possible.
A class (defined by the class_name
keyword) must extend Reference
or Object
or be an inner class
, or else Godot will not able to see its properties.
It is also possible to deserialize a JSON key to a class property with a different name using the export
hint as a variable name.
example usage:
class User:
var createdAt: String
var name: String
var avatar: String
var id: int
var user: Dictionary = {
createdAt = "1601239812",
name = "William",
avatar = "https://google.com/images/avatar",
id = 0
}
func _ready() -> void:
var u: User = json_to_class(user, User.new())
print(u.name) # will print "William"
print(class_to_json(u)) # will print '{avatar=...}'