From 9771af32dac2a78f67b2ff76f9cea35677e8b99f Mon Sep 17 00:00:00 2001 From: Peteris Krisjanis Date: Mon, 8 Jul 2013 18:29:17 +0300 Subject: core: Add support for JSON to GDataParsable, GDataEntry and GDataFeed MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This also includes initial code for detection of the Content-Type of received messages, and parsing JSON or XML depending on that. This breaks ABI (but not API), and adds a dependency on json-glib ≥ 0.15. Complete unit tests are included. Further work is expected for integrating JSON support into GDataService, ready for use with the Tasks service. This work is originally by Pēteris Krišjānis , with additions by Philip Withnall . Helps: https://bugzilla.gnome.org/show_bug.cgi?id=657539 --- gdata/gdata-service.c | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) (limited to 'gdata/gdata-service.c') diff --git a/gdata/gdata-service.c b/gdata/gdata-service.c index 9c20aff1..9d446c92 100644 --- a/gdata/gdata-service.c +++ b/gdata/gdata-service.c @@ -914,8 +914,10 @@ __gdata_service_query (GDataService *self, GDataAuthorizationDomain *domain, con gboolean is_async) { GDataServiceClass *klass; - GDataFeed *feed; + GDataFeed *feed = NULL; SoupMessage *message; + SoupMessageHeaders *headers; + const gchar *content_type; message = _gdata_service_query (self, domain, feed_uri, query, cancellable, error); if (message == NULL) @@ -923,8 +925,23 @@ __gdata_service_query (GDataService *self, GDataAuthorizationDomain *domain, con g_assert (message->response_body->data != NULL); klass = GDATA_SERVICE_GET_CLASS (self); - feed = _gdata_feed_new_from_xml (klass->feed_type, message->response_body->data, message->response_body->length, entry_type, - progress_callback, progress_user_data, is_async, error); + + headers = message->response_headers; + content_type = soup_message_headers_get_content_type (headers, NULL); + + if (content_type != NULL && strcmp (content_type, "application/json") == 0) { + /* Definitely JSON. */ + g_debug("JSON content type detected."); + feed = _gdata_feed_new_from_json (klass->feed_type, message->response_body->data, message->response_body->length, entry_type, + progress_callback, progress_user_data, is_async, error); + } else { + /* Potentially XML. Don't bother checking the Content-Type, since the parser + * will fail gracefully if the response body is not valid XML. */ + g_debug("XML content type detected."); + feed = _gdata_feed_new_from_xml (klass->feed_type, message->response_body->data, message->response_body->length, entry_type, + progress_callback, progress_user_data, is_async, error); + } + g_object_unref (message); if (feed == NULL) -- cgit v1.2.1