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;
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);
}
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;
}
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;
}
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;
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;
}
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);
+ }
+ }
+}