[gnome-software] fedora tagger: Use json-glib instead of a handrolled JSON parser



commit b623dddf9218666d27e2ed1f9a78faf55bd5694f
Author: Kalev Lember <klember redhat com>
Date:   Fri Nov 27 16:57:59 2015 +0100

    fedora tagger: Use json-glib instead of a handrolled JSON parser
    
    As we are going to use json-glib for the new fedora-disto-upgrades
    plugin, might use it here as well.

 configure.ac                                  |    1 +
 src/plugins/Makefile.am                       |    4 +-
 src/plugins/gs-plugin-fedora-tagger-ratings.c |   73 +++++++++++--------------
 3 files changed, 34 insertions(+), 44 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 2901fe9..b13344f 100644
--- a/configure.ac
+++ b/configure.ac
@@ -62,6 +62,7 @@ dnl ---------------------------------------------------------------------------
 PKG_CHECK_MODULES(GTK, gtk+-3.0 >= 3.18.2 gio-unix-2.0)
 PKG_CHECK_MODULES(PACKAGEKIT, packagekit-glib2 >= 1.0.11)
 PKG_CHECK_MODULES(APPSTREAM, appstream-glib >= 0.5.5)
+PKG_CHECK_MODULES(JSON_GLIB, json-glib-1.0)
 PKG_CHECK_MODULES(SQLITE, sqlite3)
 PKG_CHECK_MODULES(SOUP, libsoup-2.4 >= 2.51.92)
 PKG_CHECK_MODULES(GSETTINGS_DESKTOP_SCHEMAS, gsettings-desktop-schemas >= 3.11.5)
diff --git a/src/plugins/Makefile.am b/src/plugins/Makefile.am
index 0b7db2b..ea59daa 100644
--- a/src/plugins/Makefile.am
+++ b/src/plugins/Makefile.am
@@ -65,9 +65,9 @@ libgs_plugin_fedora_provenance_la_LDFLAGS = -module -avoid-version
 libgs_plugin_fedora_provenance_la_CFLAGS = $(GS_PLUGIN_CFLAGS) $(WARN_CFLAGS)
 
 libgs_plugin_fedora_tagger_ratings_la_SOURCES = gs-plugin-fedora-tagger-ratings.c
-libgs_plugin_fedora_tagger_ratings_la_LIBADD = $(GS_PLUGIN_LIBS) $(SOUP_LIBS) $(SQLITE_LIBS)
+libgs_plugin_fedora_tagger_ratings_la_LIBADD = $(GS_PLUGIN_LIBS) $(JSON_GLIB_LIBS) $(SOUP_LIBS) 
$(SQLITE_LIBS)
 libgs_plugin_fedora_tagger_ratings_la_LDFLAGS = -module -avoid-version
-libgs_plugin_fedora_tagger_ratings_la_CFLAGS = $(GS_PLUGIN_CFLAGS) $(WARN_CFLAGS)
+libgs_plugin_fedora_tagger_ratings_la_CFLAGS = $(GS_PLUGIN_CFLAGS) $(JSON_GLIB_CFLAGS) $(WARN_CFLAGS)
 
 libgs_plugin_fedora_tagger_usage_la_SOURCES = gs-plugin-fedora-tagger-usage.c
 libgs_plugin_fedora_tagger_usage_la_LIBADD = $(GS_PLUGIN_LIBS) $(SOUP_LIBS) $(SQLITE_LIBS)
diff --git a/src/plugins/gs-plugin-fedora-tagger-ratings.c b/src/plugins/gs-plugin-fedora-tagger-ratings.c
index 0ac0350..6a2cefc 100644
--- a/src/plugins/gs-plugin-fedora-tagger-ratings.c
+++ b/src/plugins/gs-plugin-fedora-tagger-ratings.c
@@ -21,6 +21,7 @@
 
 #include <config.h>
 
+#include <json-glib/json-glib.h>
 #include <libsoup/soup.h>
 #include <string.h>
 #include <sqlite3.h>
@@ -100,50 +101,33 @@ gs_plugin_destroy (GsPlugin *plugin)
 
 /**
  * gs_plugin_parse_json:
- *
- * This is a quick and dirty JSON parser that extracts one line from the
- * JSON formatted data. Sorry JsonGlib, you look awesome, but you're just too
- * heavy for an error message.
  */
 static gchar *
-gs_plugin_parse_json (const gchar *data, gsize data_len, const gchar *key)
+gs_plugin_parse_json (const gchar *data, gsize data_len, const gchar *key, GError **error)
 {
-       gchar *key_full;
+       JsonNode *root;
+       JsonObject *root_object;
+       JsonParser *parser;
+       gboolean ret;
        gchar *value = NULL;
-       guint i;
-       gchar *tmp;
-       guint len;
-       g_autoptr(GString) string = NULL;
-       g_auto(GStrv) split = NULL;
 
-       /* format the key to match what JSON returns */
-       key_full = g_strdup_printf ("\"%s\":", key);
+       parser = json_parser_new ();
 
-       /* replace escaping with something sane */
-       string = g_string_new_len (data, data_len);
-       gs_string_replace (string, "\\\"", "'");
+       ret = json_parser_load_from_data (parser, data, data_len, error);
+       if (!ret)
+               goto out;
 
-       /* find the line that corresponds to our key */
-       split = g_strsplit (string->str, "\n", -1);
-       for (i = 0; split[i] != NULL; i++) {
-               tmp = g_strchug (split[i]);
-               if (g_str_has_prefix (tmp, key_full)) {
-                       tmp += strlen (key_full);
-
-                       /* remove leading chars */
-                       tmp = g_strstrip (tmp);
-                       if (tmp[0] == '\"')
-                               tmp += 1;
-
-                       /* remove trailing chars */
-                       len = strlen (tmp);
-                       if (tmp[len-1] == ',')
-                               len -= 1;
-                       if (tmp[len-1] == '\"')
-                               len -= 1;
-                       value = g_strndup (tmp, len);
-               }
-       }
+       root = json_parser_get_root (parser);
+       if (root == NULL)
+               goto out;
+
+       root_object = json_node_get_object (root);
+       if (root_object == NULL)
+               goto out;
+
+       value = g_strdup (json_object_get_string_member (root_object, key));
+out:
+       g_object_unref (parser);
        return value;
 }
 
@@ -177,10 +161,15 @@ gs_plugin_app_set_rating_pkg (GsPlugin *plugin,
                g_debug ("Failed to set rating on fedora-tagger: %s",
                         soup_status_get_phrase (status_code));
                if (msg->response_body->data != NULL) {
+                       g_autoptr(GError) error_local = NULL;
                        error_msg = gs_plugin_parse_json (msg->response_body->data,
                                                          msg->response_body->length,
-                                                         "error");
-                       g_debug ("the error given was: %s", error_msg);
+                                                         "error",
+                                                         &error_local);
+                       if (error_msg == NULL)
+                               g_debug ("failed to parse fedora-tagger response: %s", error_local->message);
+                       else
+                               g_debug ("the error given was: %s", error_msg);
                }
        } else {
                g_debug ("Got response: %s", msg->response_body->data);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]