]> saetta.ns0.it Git - zakform/json/commitdiff
Adjustments to ::new_from_file and added ::new_from_data.
authorAndrea Zagli <azagli@libero.it>
Sat, 2 May 2020 08:36:04 +0000 (10:36 +0200)
committerAndrea Zagli <azagli@libero.it>
Sat, 2 May 2020 08:36:04 +0000 (10:36 +0200)
src/form.c
src/form.h

index ec92d95fe8701b365a88b2c9db73e7824721294b..fceff838dff3148b1e928f02339e03946af25107 100644 (file)
@@ -54,6 +54,8 @@ static gboolean zak_form_json_provider_delete (ZakFormIProvider *provider, GPtrA
 typedef struct
        {
                gchar *filename;
+
+               JsonNode *root;
        } ZakFormJsonProviderPrivate;
 
 struct _ZakFormJsonProvider
@@ -103,7 +105,7 @@ zak_form_iprovider_interface_init (ZakFormIProviderInterface *iface)
 ZakFormJsonProvider
 *zak_form_json_provider_new_from_file (const gchar *filename)
 {
-       GKeyFile *kfile;
+       JsonParser *parser;;
 
        GFile *gf;
        GFileOutputStream *ostr;
@@ -111,6 +113,7 @@ ZakFormJsonProvider
        GError *error;
 
        ZakFormJsonProvider *zak_form_json_provider;
+       ZakFormJsonProviderPrivate *priv;
 
        g_return_val_if_fail (filename != NULL && g_strcmp0 (filename, "") != 0, NULL);
 
@@ -128,17 +131,58 @@ ZakFormJsonProvider
                        g_object_unref (gf);
                }
 
-       kfile = g_key_file_new ();
+       parser = json_parser_new ();
 
        error = NULL;
-       if (!g_key_file_load_from_file (kfile, filename, G_KEY_FILE_NONE, &error))
+       if (!json_parser_load_from_file (parser, filename, &error))
                {
-                       g_warning ("Unable to open ini file: «%s»\n. %s",
+                       g_warning ("Unable to open json file: «%s»\n. %s",
                                   filename,
                                   error != NULL && error->message != NULL ? error->message : "No details.");
                        return NULL;
                }
-       zak_form_json_provider = zak_form_json_provider_new_from_gkeyfile (kfile, filename);
+
+       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->root = json_parser_get_root (parser);
+
+       return zak_form_json_provider;
+}
+
+/**
+ * zak_form_json_provider_new_from_data:
+ * @data:
+ * @length:
+ *
+ * Returns: the newly created #ZakFormJsonProvider object.
+ */
+ZakFormJsonProvider
+*zak_form_json_provider_new_from_data (const gchar *data, gssize length)
+{
+       JsonParser *parser;;
+
+       GError *error;
+
+       ZakFormJsonProvider *zak_form_json_provider;
+       ZakFormJsonProviderPrivate *priv;
+
+       g_return_val_if_fail (data != NULL && g_strcmp0 (data, "") != 0, NULL);
+
+       parser = json_parser_new ();
+
+       error = NULL;
+       if (!json_parser_load_from_data (parser, data, size, &error))
+               {
+                       g_warning ("Unable to read data\n. %s",
+                                  error != NULL && error->message != NULL ? error->message : "No details.");
+                       return NULL;
+               }
+
+       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->root = json_parser_get_root (parser);
 
        return zak_form_json_provider;
 }
index 48fbdaa519ef60fac8e6ac01c62a4b10d178e87f..c65f347c57d785134a3697e1378dc2e0673a356f 100644 (file)
@@ -33,6 +33,7 @@ G_DECLARE_FINAL_TYPE (ZakFormJsonProvider, zak_form_json_provider, ZAK_FORM_JSON
 
 
 ZakFormJsonProvider *zak_form_json_provider_new_from_file (const gchar *filename);
+ZakFormJsonProvider *zak_form_json_provider_new_from_data (const gchar *data, gssize length);
 
 
 G_END_DECLS