From 16f3cb054233b9d5f9eba8ea8b2b0f1e92fbe66f Mon Sep 17 00:00:00 2001 From: technosf <535060+technosf@users.noreply.github.com> Date: Thu, 26 Sep 2024 11:04:55 -0700 Subject: [PATCH 1/2] Added check for null root --- src/Models/StationStore.vala | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/Models/StationStore.vala b/src/Models/StationStore.vala index ae7c27d..7e65ef1 100644 --- a/src/Models/StationStore.vala +++ b/src/Models/StationStore.vala @@ -3,8 +3,13 @@ * SPDX-FileCopyrightText: 2020-2022 Louis Brauer */ -// StationStore can store and retrieve a collection of stations -// in a JSON file + /** + StationStore + + Store and retrieve a collection of stations in a JSON file + + Uses libgee for data structures. + */ using Gee; @@ -63,9 +68,18 @@ public class StationStore : Object { warning (@"store: unable to load data, does it exist? $(e.message)"); } - Json.Node node = parser.get_root (); - Json.Array array = node.get_array (); - array.foreach_element ((a, i, elem) => { + Json.Node? node = parser.get_root (); + + if ( node == null ) + { + debug ("store: unable to load data, root is null"); + return; // FIXME What should be done when root node is null? + } + + debug ("store: root is not null"); + + Json.Array array = node.get_array (); // Json-CRITICAL **: 21:02:51.821: json_node_get_array: assertion 'JSON_NODE_IS_VALID (node)' failed + array.foreach_element ((a, i, elem) => { // json_array_foreach_element: assertion 'array != NULL' failed Station station = Json.gobject_deserialize (typeof (Station), elem) as Station; // TODO This should probably not be here but in // DirectoryController @@ -130,4 +144,4 @@ public class StationStore : Object { } } -} \ No newline at end of file +} From 2e598ac76806bfd6273b25fd22ee31f4c35c81b7 Mon Sep 17 00:00:00 2001 From: technosf <535060+technosf@users.noreply.github.com> Date: Sat, 28 Sep 2024 22:58:16 -0700 Subject: [PATCH 2/2] Added check for null JSON node. Renamed vars. --- src/Models/StationStore.vala | 26 ++++++++++---------------- src/Widgets/Window.vala | 4 ++-- 2 files changed, 12 insertions(+), 18 deletions(-) diff --git a/src/Models/StationStore.vala b/src/Models/StationStore.vala index 7e65ef1..88f1074 100644 --- a/src/Models/StationStore.vala +++ b/src/Models/StationStore.vala @@ -6,7 +6,7 @@ /** StationStore - Store and retrieve a collection of stations in a JSON file + Store and retrieve a collection of stations in a JSON file, i.e. favorites Uses libgee for data structures. */ @@ -17,16 +17,16 @@ namespace Tuner.Model { public class StationStore : Object { private ArrayList _store; - private File _data_file; + private File _favorites_file; - public StationStore (string data_path) { + public StationStore (string favorites_path) { Object (); _store = new ArrayList (); - _data_file = File.new_for_path (data_path); + _favorites_file = File.new_for_path (favorites_path); ensure (); load (); - debug (@"store initialized in path $data_path"); + debug (@"store initialized in path $favorites_path"); } public void add (Station station) { @@ -48,7 +48,7 @@ public class StationStore : Object { // Non-racy approach is to try to create the file first // and ignore errors if it already exists try { - var df = _data_file.create (FileCreateFlags.PRIVATE); + var df = _favorites_file.create (FileCreateFlags.PRIVATE); df.close (); debug (@"store created"); } catch (Error e) { @@ -61,7 +61,7 @@ public class StationStore : Object { Json.Parser parser = new Json.Parser (); try { - var stream = _data_file.read (); + var stream = _favorites_file.read (); parser.load_from_stream (stream); stream.close (); } catch (Error e) { @@ -70,13 +70,7 @@ public class StationStore : Object { Json.Node? node = parser.get_root (); - if ( node == null ) - { - debug ("store: unable to load data, root is null"); - return; // FIXME What should be done when root node is null? - } - - debug ("store: root is not null"); + if ( node == null ) return; // No favorites store Json.Array array = node.get_array (); // Json-CRITICAL **: 21:02:51.821: json_node_get_array: assertion 'JSON_NODE_IS_VALID (node)' failed array.foreach_element ((a, i, elem) => { // json_array_foreach_element: assertion 'array != NULL' failed @@ -102,8 +96,8 @@ public class StationStore : Object { var data = serialize (); try { - _data_file.delete (); - var stream = _data_file.create ( + _favorites_file.delete (); + var stream = _favorites_file.create ( FileCreateFlags.REPLACE_DESTINATION | FileCreateFlags.PRIVATE ); var s = new DataOutputStream (stream); diff --git a/src/Widgets/Window.vala b/src/Widgets/Window.vala index ba21b5d..6c17664 100644 --- a/src/Widgets/Window.vala +++ b/src/Widgets/Window.vala @@ -109,8 +109,8 @@ public class Tuner.Window : Gtk.ApplicationWindow { var stack = new Gtk.Stack (); stack.transition_type = Gtk.StackTransitionType.CROSSFADE; - var data_file = Path.build_filename (Application.instance.data_dir, "favorites.json"); - var store = new Model.StationStore (data_file); + var favorites_file = Path.build_filename (Application.instance.data_dir, "favorites.json"); + var store = new Model.StationStore (favorites_file); _directory = new DirectoryController (store); var primary_box = new Gtk.Paned (Gtk.Orientation.HORIZONTAL);