diff options
author | Peteris Krisjanis <pecisk@gmail.com> | 2013-07-08 18:29:17 +0300 |
---|---|---|
committer | Philip Withnall <philip@tecnocode.co.uk> | 2013-08-29 23:12:28 -0600 |
commit | 9771af32dac2a78f67b2ff76f9cea35677e8b99f (patch) | |
tree | 3e6ee1e2338b54dd0d08c370a8e9388564bd65d0 /gdata/gdata-service.c | |
parent | ce7bc546da6176ac4bec698f27792fe834c4a45d (diff) | |
download | libgdata-9771af32dac2a78f67b2ff76f9cea35677e8b99f.tar.gz |
core: Add support for JSON to GDataParsable, GDataEntry and GDataFeed
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 <pecisk@gmail.com>, with
additions by Philip Withnall <philip@tecnocode.co.uk>.
Helps: https://bugzilla.gnome.org/show_bug.cgi?id=657539
Diffstat (limited to 'gdata/gdata-service.c')
-rw-r--r-- | gdata/gdata-service.c | 23 |
1 files changed, 20 insertions, 3 deletions
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) |