From: Andrea Zagli Date: Sat, 2 May 2020 08:36:04 +0000 (+0200) Subject: Adjustments to ::new_from_file and added ::new_from_data. X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=78f7cee38b7cafef31a0815cdebd796df43b7a5c;p=zakform%2Fjson Adjustments to ::new_from_file and added ::new_from_data. --- diff --git a/src/form.c b/src/form.c index ec92d95..fceff83 100644 --- a/src/form.c +++ b/src/form.c @@ -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; } diff --git a/src/form.h b/src/form.h index 48fbdaa..c65f347 100644 --- a/src/form.h +++ b/src/form.h @@ -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