]> saetta.ns0.it Git - zakform/json/commitdiff
First build.
authorAndrea Zagli <azagli@libero.it>
Sat, 2 May 2020 09:15:40 +0000 (11:15 +0200)
committerAndrea Zagli <azagli@libero.it>
Sat, 2 May 2020 09:15:40 +0000 (11:15 +0200)
Implemented ::load and ::get_object.

src/form.c

index fceff838dff3148b1e928f02339e03946af25107..5662d1e8dc5208fe2eaf31c2fce6fd92383ec3fb 100644 (file)
@@ -172,7 +172,7 @@ ZakFormJsonProvider
        parser = json_parser_new ();
 
        error = NULL;
-       if (!json_parser_load_from_data (parser, data, size, &error))
+       if (!json_parser_load_from_data (parser, data, length, &error))
                {
                        g_warning ("Unable to read data\n. %s",
                                   error != NULL && error->message != NULL ? error->message : "No details.");
@@ -244,8 +244,8 @@ zak_form_json_provider_finalize (GObject *gobject)
        parent_class->finalize (gobject);
 }
 
-static gchar
-*zak_form_json_provider_get_group (GPtrArray *elements)
+static JsonObject
+*zak_form_json_provider_get_object (ZakFormIProvider *provider, GPtrArray *elements)
 {
        gchar *ret;
 
@@ -255,6 +255,13 @@ static gchar
 
        guint i;
 
+       JsonNode *node;
+       JsonPath *path;
+       JsonArray *ar;
+       JsonObject *obj;
+
+       ZakFormJsonProviderPrivate *priv = zak_form_json_provider_get_instance_private (ZAK_FORM_JSON_PROVIDER (provider));
+
        key = g_string_new ("");
        for (i = 0; i < elements->len; i++)
                {
@@ -270,16 +277,24 @@ static gchar
 
        if (key->len > 0)
                {
-                       ret = g_strdup (key->str + 1);
+                       ret = g_strdup_printf ("KEY:%s", key->str + 1);
                }
        else
                {
-                       ret = g_strdup ("THE_KEY");
+                       ret = g_strdup ("KEY:");
                }
 
        g_string_free (key, TRUE);
 
-       return ret;
+       path = json_path_new ();
+       json_path_compile (path, g_strdup_printf ("$[*].%s", ret), NULL);
+
+       node = json_path_match (path, priv->root);
+
+       ar = json_node_get_array (node);;
+       obj = json_array_get_object_element (ar, 0);
+
+       return obj;
 }
 
 static gboolean
@@ -289,29 +304,22 @@ zak_form_json_provider_load (ZakFormIProvider *provider, GPtrArray *elements)
 
        guint i;
 
-       gchar *group;
-
-       GError *error;
-
-       ZakFormJsonProviderPrivate *priv = zak_form_json_provider_get_instance_private (ZAK_FORM_JSON_PROVIDER (provider));
+       JsonObject *obj;
 
        ret = TRUE;
 
-       group = zak_form_json_provider_get_group (elements);
+       obj = zak_form_json_provider_get_object (provider, elements);
 
        for (i = 0; i < elements->len; i++)
                {
                        ZakFormElement *element = (ZakFormElement *)g_ptr_array_index (elements, i);
                        if (zak_form_element_get_to_load (element))
                                {
-                                       error = NULL;
-                                       zak_form_element_set_value (element, g_key_file_get_string (priv->kfile, group, zak_form_element_get_name (element), &error));
+                                       zak_form_element_set_value (element, json_object_get_string_member (obj, zak_form_element_get_name (element)));
                                        zak_form_element_set_as_original_value (element);
                                }
                }
 
-       g_free (group);
-
        return ret;
 }
 
@@ -323,7 +331,7 @@ zak_form_json_provider_insert (ZakFormIProvider *provider, GPtrArray *elements)
        guint i;
 
        GValue *value;
-       gchar *group;
+       JsonObject *obj;
 
        GError *error;
 
@@ -331,7 +339,7 @@ zak_form_json_provider_insert (ZakFormIProvider *provider, GPtrArray *elements)
 
        ret = TRUE;
 
-       group = zak_form_json_provider_get_group (elements);
+       obj = zak_form_json_provider_get_object (provider, elements);
 
        for (i = 0; i < elements->len; i++)
                {
@@ -340,25 +348,24 @@ zak_form_json_provider_insert (ZakFormIProvider *provider, GPtrArray *elements)
                                {
                                        value = zak_form_element_unformat_gvalue (element, zak_form_element_get_value_gvalue (element));
 
-                                       g_key_file_set_string (priv->kfile, group,
-                                                              zak_form_element_get_name (element),
-                                                              g_value_get_string (value));
+                                       /* 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");
-                                               }
+                                       /* 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"); */
+                                       /*      } */
+
 
                                        g_value_unset (value);
                                }
                }
 
-       g_free (group);
-
        return ret;
 }
 
@@ -373,7 +380,7 @@ zak_form_json_provider_delete (ZakFormIProvider *provider, GPtrArray *elements)
 {
        gboolean ret;
 
-       gchar *group;
+       JsonObject *obj;
 
        guint i;
 
@@ -383,7 +390,7 @@ zak_form_json_provider_delete (ZakFormIProvider *provider, GPtrArray *elements)
 
        ret = TRUE;
 
-       group = zak_form_json_provider_get_group (elements);
+       obj = zak_form_json_provider_get_object (provider, elements);
 
        for (i = 0; i < elements->len; i++)
                {
@@ -391,22 +398,22 @@ zak_form_json_provider_delete (ZakFormIProvider *provider, GPtrArray *elements)
                        if (zak_form_element_get_to_save (element))
                                {
                                        error = NULL;
-                                       g_key_file_remove_key (priv->kfile, group,
-                                                              zak_form_element_get_name (element),
-                                                              &error);
+                                       /* g_key_file_remove_key (priv->kfile, group, */
+                                       /*                        zak_form_element_get_name (element), */
+                                       /*                        &error); */
+
 
                                        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");
-                                               }
+                                       /* 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"); */
+                                       /*      } */
+
                                }
                }
 
-       g_free (group);
-
        return ret;
 }