pokeapi_importer/editor/pokeapi_json.gd
2026-10-04 13:44:13 +02:00

36 lines
1.4 KiB
GDScript

# editor/pokeapi_json.gd
@tool
class_name PokeApiJson
extends RefCounted
## Tous les accesseurs tolèrent un dictionnaire absent, une clé manquante,
## ou une valeur JSON null — ils renvoient le défaut plutôt que de planter.
static func get_str(j : Dictionary, key : String, default : String = "") -> String:
var v : Variant = j.get(key)
return String(v) if v != null else default
static func get_int(j : Dictionary, key : String, default : int = 0) -> int:
var v : Variant = j.get(key)
return int(v) if v != null else default
static func get_bool(j : Dictionary, key : String, default : bool = false) -> bool:
var v : Variant = j.get(key)
return bool(v) if v != null else default
static func get_dict(j : Dictionary, key : String) -> Dictionary:
var v : Variant = j.get(key)
return v if v is Dictionary else {}
static func get_array(j : Dictionary, key : String) -> Array:
var v : Variant = j.get(key)
return v if v is Array else []
## Cas très fréquent dans PokeAPI : {"name": "...", "url": "..."} imbriqué, parfois null.
## get_dict_name({"type": {"name": "electric"}}, "type") -> "electric"
static func get_dict_name(j : Dictionary, key : String, default : String = "") -> String:
return get_str(get_dict(j, key), "name", default)
static func get_dict_url(j : Dictionary, key : String, default : String = "") -> String:
return get_str(get_dict(j, key), "url", default)