From 4c9755dc2494f7642b2324ab75343fc4c70b3502 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Mon, 8 Jun 2026 21:14:06 +0000 Subject: [PATCH] chore(legacy JSON): migrate all 8 files from JSON.new().parse() to JSON.parse_string() Migrated JSON parsing logic in 8 files from the legacy Godot 3.x pattern (JSON.new().parse()) to the modern Godot 4.x idiomatic pattern (JSON.parse_string()). Files migrated: - scripts/combat/room_loader.gd - scripts/core/encounter_system.gd - scripts/core/loot_table.gd - scripts/core/room_generator.gd - scripts/autoload/settings_manager.gd - scripts/autoload/grid_system.gd - scripts/autoload/save_manager.gd - scripts/entities/apparition_renderer.gd Verified via headless Godot syntax check and GdUnit4 test suite (171/171 passed). Co-authored-by: niyazmft <9331133+niyazmft@users.noreply.github.com> --- scripts/autoload/grid_system.gd | 9 +++------ scripts/autoload/save_manager.gd | 14 ++------------ scripts/autoload/settings_manager.gd | 22 ++++++---------------- scripts/combat/room_loader.gd | 13 ++++--------- scripts/core/encounter_system.gd | 6 +----- scripts/core/loot_table.gd | 13 ++++--------- scripts/core/room_generator.gd | 10 ++++++---- scripts/entities/apparition_renderer.gd | 14 ++++---------- 8 files changed, 30 insertions(+), 71 deletions(-) diff --git a/scripts/autoload/grid_system.gd b/scripts/autoload/grid_system.gd index 5404b770..4e247018 100644 --- a/scripts/autoload/grid_system.gd +++ b/scripts/autoload/grid_system.gd @@ -163,13 +163,10 @@ func load_room_from_file(path: String) -> Error: return FileAccess.get_open_error() var text: String = f.get_as_text() f.close() - var json := JSON.new() - var err: Error = json.parse(text) - if err != OK: - return err - if not json.data is Dictionary: + var data: Variant = JSON.parse_string(text) + if not data is Dictionary: return ERR_INVALID_DATA - return load_room(json.data) + return load_room(data) ## ------------------------------------------------------------------ diff --git a/scripts/autoload/save_manager.gd b/scripts/autoload/save_manager.gd index 3c0da46e..a048f380 100644 --- a/scripts/autoload/save_manager.gd +++ b/scripts/autoload/save_manager.gd @@ -128,19 +128,9 @@ func load_game() -> Dictionary: var raw_text: String = file.get_as_text() file.close() - var json: JSON = JSON.new() - var parse_error: Error = json.parse(raw_text) - if parse_error != OK: - var reason: String = ( - "JSON parse error at line %d: %s" % [json.get_error_line(), json.get_error_message()] - ) - push_error("[SaveManager] load_game: %s" % reason) - load_failed.emit(reason) - return {} - - var raw_data: Variant = json.get_data() + var raw_data: Variant = JSON.parse_string(raw_text) if typeof(raw_data) != TYPE_DICTIONARY: - var reason: String = "Parsed JSON is not a Dictionary." + var reason: String = "Parsed JSON is not a Dictionary or failed to parse." push_error("[SaveManager] load_game: %s" % reason) load_failed.emit(reason) return {} diff --git a/scripts/autoload/settings_manager.gd b/scripts/autoload/settings_manager.gd index 5a303af2..4fe98bd0 100644 --- a/scripts/autoload/settings_manager.gd +++ b/scripts/autoload/settings_manager.gd @@ -72,23 +72,13 @@ func load_settings() -> void: var json_string: String = file.get_as_text() file.close() - var json: JSON = JSON.new() - var error: Error = json.parse(json_string) - if error == OK: - var loaded_settings: Variant = json.data - if loaded_settings is Dictionary: - _merge_dict(settings, loaded_settings as Dictionary) - _apply_loaded_bindings() - _print_debug("Settings loaded successfully from JSON") - else: - _print_error("Settings JSON is not a dictionary") + var loaded_settings: Variant = JSON.parse_string(json_string) + if loaded_settings is Dictionary: + _merge_dict(settings, loaded_settings as Dictionary) + _apply_loaded_bindings() + _print_debug("Settings loaded successfully from JSON") else: - _print_error( - ( - "JSON Parse Error: %s at line %d" - % [json.get_error_message(), json.get_error_line()] - ) - ) + _print_error("Settings JSON is not a dictionary or failed to parse") else: _print_error("Failed to open settings file for reading: %s" % SAVE_PATH) diff --git a/scripts/combat/room_loader.gd b/scripts/combat/room_loader.gd index 802cdee1..27a7abd7 100644 --- a/scripts/combat/room_loader.gd +++ b/scripts/combat/room_loader.gd @@ -26,17 +26,12 @@ static func load_room_data(room_id: String) -> Dictionary: var text := f.get_as_text() f.close() - var json := JSON.new() - var err := json.parse(text) - if err != OK: - push_error("Failed to parse room JSON: " + path + " Error: " + json.get_error_message()) + var data: Variant = JSON.parse_string(text) + if data == null or not data is Dictionary: + push_error("Failed to parse room JSON or data is not a Dictionary: " + path) return {} - if not json.data is Dictionary: - push_error("Room JSON data is not a Dictionary: " + path) - return {} - - return json.data as Dictionary + return data as Dictionary ## Configure the GridSystem with room layout. diff --git a/scripts/core/encounter_system.gd b/scripts/core/encounter_system.gd index 0560975f..3958bb15 100644 --- a/scripts/core/encounter_system.gd +++ b/scripts/core/encounter_system.gd @@ -52,12 +52,8 @@ static func _getConfig() -> Dictionary: var f: FileAccess = FileAccess.open(ENCOUNTERS_CONFIG_PATH, FileAccess.READ) if not f: return {} - var json := JSON.new() - var err: Error = json.parse(f.get_as_text()) + var data: Variant = JSON.parse_string(f.get_as_text()) f.close() - if err != OK: - return {} - var data: Variant = json.data if data is Dictionary: return data as Dictionary return {} diff --git a/scripts/core/loot_table.gd b/scripts/core/loot_table.gd index a271cd6d..9ca42525 100644 --- a/scripts/core/loot_table.gd +++ b/scripts/core/loot_table.gd @@ -26,17 +26,12 @@ static func load_loot_table(table_id: String) -> LootTable: var text := f.get_as_text() f.close() - var json := JSON.new() - var err := json.parse(text) - if err != OK: - push_error("LootTable: JSON parse error in %s: %s" % [path, json.get_error_message()]) + var data_v: Variant = JSON.parse_string(text) + if data_v == null or not data_v is Dictionary: + push_error("LootTable: JSON parse error or not a Dictionary in " + path) return null - if not json.data is Dictionary: - push_error("LootTable: Expected Dictionary at root of " + path) - return null - - var data: Dictionary = json.data + var data: Dictionary = data_v as Dictionary var table := LootTable.new() table.min_rolls = int(data.get("min_rolls", 1)) table.max_rolls = int(data.get("max_rolls", 1)) diff --git a/scripts/core/room_generator.gd b/scripts/core/room_generator.gd index 4dbfb146..ccbd02bb 100644 --- a/scripts/core/room_generator.gd +++ b/scripts/core/room_generator.gd @@ -83,12 +83,14 @@ static func _getBiomeParams(biomeId: String) -> Dictionary: var f := FileAccess.open(BIOMES_CONFIG_PATH, FileAccess.READ) if not f: return {} - var json := JSON.new() - var err := json.parse(f.get_as_text()) + var text := f.get_as_text() f.close() - if err != OK: + + var data_v: Variant = JSON.parse_string(text) + if not data_v is Dictionary: return {} - var data: Dictionary = json.data as Dictionary + + var data: Dictionary = data_v as Dictionary var biomes_data: Dictionary = data.get("biomes", {}) as Dictionary return biomes_data.get(biomeId, {}) as Dictionary diff --git a/scripts/entities/apparition_renderer.gd b/scripts/entities/apparition_renderer.gd index 66212533..1c728918 100644 --- a/scripts/entities/apparition_renderer.gd +++ b/scripts/entities/apparition_renderer.gd @@ -129,18 +129,12 @@ func _load_rig_config() -> void: var json_text: String = file.get_as_text() file.close() - var json: JSON = JSON.new() - var error: Error = json.parse(json_text) - if error == OK and json.data is Dictionary: - _rig_config = json.data as Dictionary + var data: Variant = JSON.parse_string(json_text) + if data is Dictionary: + _rig_config = data as Dictionary _apply_rig_config() else: - push_error( - ( - "ApparitionRenderer: failed to parse JSON config or config is not a Dictionary: %s" - % json.get_error_message() - ) - ) + push_error("ApparitionRenderer: failed to parse JSON config or config is not a Dictionary") func _apply_rig_config() -> void: