From: Andrea Zagli Date: Sun, 9 Aug 2020 08:46:34 +0000 (+0200) Subject: First saving to file. X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=2586662f87caf834229820b9c61390e14ecd6b85;p=zakform%2Fjson First saving to file. --- diff --git a/libzakformjson.pc.in b/libzakformjson.pc.in index 93bdcc4..b491991 100644 --- a/libzakformjson.pc.in +++ b/libzakformjson.pc.in @@ -5,7 +5,7 @@ includedir=@includedir@ modulesdir=@libdir@/@PACKAGE@/modules Name: @PACKAGE_NAME@ -Description: Class for ZakFormIProvider based on json file. +Description: Class for ZakFormIProvider based on json format. Version: @PACKAGE_VERSION@ Requires: libzakform libzakutils json-glib-1.0 Libs: -L${libdir} -lzakformjson diff --git a/src/form.c b/src/form.c index 5662d1e..a26fa0b 100644 --- a/src/form.c +++ b/src/form.c @@ -44,13 +44,15 @@ static void zak_form_json_provider_get_property (GObject *object, static void zak_form_json_provider_dispose (GObject *gobject); static void zak_form_json_provider_finalize (GObject *gobject); -static gchar *zak_form_json_provider_get_group (GPtrArray *elements); +static JsonObject *zak_form_json_provider_get_object (ZakFormIProvider *provider, GPtrArray *elements); static gboolean zak_form_json_provider_load (ZakFormIProvider *provider, GPtrArray *elements); static gboolean zak_form_json_provider_insert (ZakFormIProvider *provider, GPtrArray *elements); static gboolean zak_form_json_provider_update (ZakFormIProvider *provider, GPtrArray *elements); static gboolean zak_form_json_provider_delete (ZakFormIProvider *provider, GPtrArray *elements); +static void zak_form_json_provider_write_to_file (ZakFormJsonProvider *provider); + typedef struct { gchar *filename; @@ -123,11 +125,18 @@ ZakFormJsonProvider error = NULL; gf = g_file_new_for_path (filename); ostr = g_file_replace (gf, NULL, FALSE, G_FILE_CREATE_PRIVATE, NULL, &error); - if (ostr != NULL) + if (ostr != NULL + && error == NULL) { g_output_stream_close (G_OUTPUT_STREAM (ostr), NULL, NULL); g_object_unref (ostr); } + else + { + g_warning ("Unable to create file «%s»: %s", + filename, + error != NULL && error->message != NULL ? error->message : "no details."); + } g_object_unref (gf); } @@ -136,17 +145,30 @@ ZakFormJsonProvider error = NULL; if (!json_parser_load_from_file (parser, filename, &error)) { - g_warning ("Unable to open json file: «%s»\n. %s", + g_warning ("Unable to open json file: «%s»: %s", filename, - error != NULL && error->message != NULL ? error->message : "No details."); - return NULL; + error != NULL && error->message != NULL ? error->message : "no details."); } zak_form_json_provider = ZAK_FORM_JSON_PROVIDER (g_object_new (zak_form_json_provider_get_type (), NULL)); priv = zak_form_json_provider_get_instance_private (zak_form_json_provider); + priv->filename = g_strdup (filename); priv->root = json_parser_get_root (parser); + if (priv->root == NULL) + { + JsonArray *jar; + + g_warning ("Unable to get json root element."); + + jar = json_array_new (); + priv->root = json_node_new (JSON_NODE_ARRAY); + json_node_init_array (priv->root, jar); + + zak_form_json_provider_write_to_file (zak_form_json_provider); + } + return zak_form_json_provider; } @@ -184,6 +206,17 @@ ZakFormJsonProvider priv->root = json_parser_get_root (parser); + if (priv->root == NULL) + { + JsonArray *jar; + + g_warning ("Unable to get json root element."); + + jar = json_array_new (); + priv->root = json_node_new (JSON_NODE_ARRAY); + json_node_init_array (priv->root, jar); + } + return zak_form_json_provider; } @@ -291,7 +324,7 @@ static JsonObject node = json_path_match (path, priv->root); - ar = json_node_get_array (node);; + ar = json_node_get_array (node); obj = json_array_get_object_element (ar, 0); return obj; @@ -333,39 +366,46 @@ zak_form_json_provider_insert (ZakFormIProvider *provider, GPtrArray *elements) GValue *value; JsonObject *obj; - GError *error; - ZakFormJsonProviderPrivate *priv = zak_form_json_provider_get_instance_private (ZAK_FORM_JSON_PROVIDER (provider)); ret = TRUE; - obj = zak_form_json_provider_get_object (provider, elements); + //obj = zak_form_json_provider_get_object (provider, elements); + obj = NULL; + + if (obj == NULL) + { + JsonNode *jnode; + + obj = json_object_new (); + + jnode = json_node_new (JSON_NODE_OBJECT); + json_node_init_object (jnode, obj); + json_array_add_element (json_node_get_array (priv->root), jnode); + } for (i = 0; i < elements->len; i++) { ZakFormElement *element = (ZakFormElement *)g_ptr_array_index (elements, i); + if (zak_form_element_get_to_save (element)) { - value = zak_form_element_unformat_gvalue (element, zak_form_element_get_value_gvalue (element)); + JsonNode *jnode; - /* g_key_file_set_string (priv->kfile, group, */ - /* zak_form_element_get_name (element), */ - /* g_value_get_string (value)); */ - - error = NULL; - /* if (!g_key_file_save_to_file (priv->kfile, priv->filename, &error) */ - /* || error != NULL) */ - /* { */ - /* g_warning ("Unable to write to file «%s»: %s.", */ - /* priv->filename, */ - /* error != NULL && error->message != NULL ? error->message : "no details"); */ - /* } */ + value = zak_form_element_unformat_gvalue (element, zak_form_element_get_value_gvalue (element)); + jnode = json_node_new (JSON_NODE_VALUE); + json_object_set_member (obj, zak_form_element_get_name (element), json_node_init_string (jnode, g_value_get_string (value))); g_value_unset (value); } } + if (priv->filename != NULL) + { + zak_form_json_provider_write_to_file (ZAK_FORM_JSON_PROVIDER (provider)); + } + return ret; } @@ -417,3 +457,63 @@ zak_form_json_provider_delete (ZakFormIProvider *provider, GPtrArray *elements) return ret; } + +static void +zak_form_json_provider_write_to_file (ZakFormJsonProvider *provider) +{ + GFile *gf; + GFileOutputStream *ostr; + + GError *error; + + JsonGenerator *generator; + + ZakFormJsonProviderPrivate *priv = zak_form_json_provider_get_instance_private (ZAK_FORM_JSON_PROVIDER (provider)); + + if (priv->filename != NULL) + { + /* jsonglib gives error if json is an empty array */ + if (json_array_get_length (json_node_get_array (priv->root)) == 0) + { + /* create the empty file */ + error = NULL; + gf = g_file_new_for_path (priv->filename); + ostr = g_file_replace (gf, NULL, FALSE, G_FILE_CREATE_PRIVATE, NULL, &error); + if (ostr != NULL + && error == NULL) + { + error = NULL; + g_output_stream_write_all (G_OUTPUT_STREAM (ostr), + "[]", + 3, + NULL, NULL, + &error); + + g_output_stream_close (G_OUTPUT_STREAM (ostr), NULL, NULL); + g_object_unref (ostr); + } + else + { + g_warning ("Unable to create file «%s»: %s", + priv->filename, + error != NULL && error->message != NULL ? error->message : "no details."); + } + } + else + { + error = NULL; + generator = json_generator_new (); + json_generator_set_root (generator, priv->root); + + if (!json_generator_to_file (generator, priv->filename, &error) + || error != NULL) + { + g_warning ("Unable to write to file «%s»: %s", + priv->filename, + error != NULL && error->message != NULL ? error->message : "no details."); + } + + g_object_unref (generator); + } + } +}