[libgdata] app: Add support for JSON to GDataAPPCategories
- From: Philip Withnall <pwithnall src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [libgdata] app: Add support for JSON to GDataAPPCategories
- Date: Sun, 19 Apr 2015 23:15:10 +0000 (UTC)
commit f89b123bf15dc1f4eee107b6a65a2a6bd1273253
Author: Philip Withnall <philip tecnocode co uk>
Date: Sun Apr 19 18:02:03 2015 +0100
app: Add support for JSON to GDataAPPCategories
This allows downloading the YouTube category list with v3 of the YouTube
API.
https://bugzilla.gnome.org/show_bug.cgi?id=687597
gdata/app/gdata-app-categories.c | 76 ++++++++++++++++++++++++++++++++++++++
1 files changed, 76 insertions(+), 0 deletions(-)
---
diff --git a/gdata/app/gdata-app-categories.c b/gdata/app/gdata-app-categories.c
index 375a1e2..aaf7bfa 100644
--- a/gdata/app/gdata-app-categories.c
+++ b/gdata/app/gdata-app-categories.c
@@ -44,6 +44,11 @@ static void gdata_app_categories_get_property (GObject *object, guint property_i
static gboolean pre_parse_xml (GDataParsable *parsable, xmlDoc *doc, xmlNode *root_node, gpointer user_data,
GError **error);
static gboolean parse_xml (GDataParsable *parsable, xmlDoc *doc, xmlNode *node, gpointer user_data, GError
**error);
static gboolean post_parse_xml (GDataParsable *parsable, gpointer user_data, GError **error);
+static gboolean
+parse_json (GDataParsable *parsable, JsonReader *reader, gpointer user_data,
+ GError **error);
+static gboolean
+post_parse_json (GDataParsable *parsable, gpointer user_data, GError **error);
struct _GDataAPPCategoriesPrivate {
GList *categories;
@@ -72,6 +77,10 @@ gdata_app_categories_class_init (GDataAPPCategoriesClass *klass)
parsable_class->pre_parse_xml = pre_parse_xml;
parsable_class->parse_xml = parse_xml;
parsable_class->post_parse_xml = post_parse_xml;
+
+ parsable_class->parse_json = parse_json;
+ parsable_class->post_parse_json = post_parse_json;
+
parsable_class->element_name = "categories";
parsable_class->element_namespace = "app";
@@ -195,6 +204,73 @@ post_parse_xml (GDataParsable *parsable, gpointer user_data, GError **error)
return TRUE;
}
+/* Reference: https://developers.google.com/youtube/v3/docs/videoCategories/list */
+static gboolean
+parse_json (GDataParsable *parsable, JsonReader *reader, gpointer user_data, GError **error)
+{
+ GDataAPPCategories *self = GDATA_APP_CATEGORIES (parsable);
+ GDataAPPCategoriesPrivate *priv = self->priv;
+
+ if (g_strcmp0 (json_reader_get_member_name (reader), "items") == 0) {
+ guint i, elements;
+
+ /* Loop through the elements array. */
+ for (i = 0, elements = (guint) json_reader_count_elements (reader); i < elements; i++) {
+ GDataCategory *category = NULL;
+ const gchar *id, *title;
+ const GError *child_error = NULL;
+
+ json_reader_read_element (reader, i);
+
+ json_reader_read_member (reader, "id");
+ id = json_reader_get_string_value (reader);
+ json_reader_end_member (reader);
+
+ json_reader_read_member (reader, "snippet");
+
+ json_reader_read_member (reader, "title");
+ title = json_reader_get_string_value (reader);
+ json_reader_end_member (reader);
+
+ child_error = json_reader_get_error (reader);
+
+ if (child_error != NULL) {
+ return gdata_parser_error_from_json_error (reader,
+ child_error,
+ error);
+ }
+
+ /* Create the category. */
+ category = gdata_category_new (id, NULL, title);
+ priv->categories = g_list_prepend (priv->categories,
+ category);
+
+ json_reader_end_member (reader); /* snippet */
+ json_reader_end_element (reader); /* category */
+ }
+
+ return TRUE;
+ } else if (g_strcmp0 (json_reader_get_member_name (reader), "kind") == 0 ||
+ g_strcmp0 (json_reader_get_member_name (reader), "etag") == 0 ||
+ g_strcmp0 (json_reader_get_member_name (reader), "id") == 0) {
+ /* Ignore. */
+ return TRUE;
+ } else {
+ return GDATA_PARSABLE_CLASS (gdata_app_categories_parent_class)->parse_json (parsable,
reader, user_data, error);
+ }
+}
+
+static gboolean
+post_parse_json (GDataParsable *parsable, gpointer user_data, GError **error)
+{
+ GDataAPPCategoriesPrivate *priv = GDATA_APP_CATEGORIES (parsable)->priv;
+
+ /* Reverse our lists of stuff. */
+ priv->categories = g_list_reverse (priv->categories);
+
+ return TRUE;
+}
+
/**
* gdata_app_categories_get_categories:
* @self: a #GDataAPPCategories
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]