fixed compilation against newer version of json-c (deprecation of the function json_o...
[ppastats.git] / src / lp_json.c
index 76a97d6..fcc0651 100644 (file)
@@ -23,8 +23,8 @@
 #include <string.h>
 #include <time.h>
 
-#include "lp_json.h"
-#include "lp_ws.h"
+#include <lp_json.h>
+#include <lp_ws.h>
 #include <ptime.h>
 
 static time_t json_to_time(json_object *json)
@@ -37,7 +37,6 @@ static time_t json_to_time(json_object *json)
        if (!str)
                return -1;
 
-
        memset(&tm, 0, sizeof(struct tm));
        tm.tm_isdst = -1;
        ret = strptime(str, "%FT%T", &tm);
@@ -48,16 +47,20 @@ static time_t json_to_time(json_object *json)
                return -1;
 }
 
-static json_object *time_to_json(time_t t)
+json_object *time_to_json(time_t t)
 {
        char *str;
+       json_object *j;
 
        str = time_to_ISO8601_time(&t);
 
-       if (str)
-               return json_object_new_string(str);
-       else
+       if (str) {
+               j = json_object_new_string(str);
+               free(str);
+               return j;
+       } else {
                return NULL;
+       }
 }
 
 static struct bpph *json_to_bpph(json_object *o)
@@ -70,25 +73,28 @@ static struct bpph *json_to_bpph(json_object *o)
        struct bpph *bpph;
        const char *status;
        time_t date_created;
+       json_object *j;
 
-       binary_package_name = json_object_get_string
-               (json_object_object_get(o, "binary_package_name"));
+       json_object_object_get_ex(o, "binary_package_name", &j);
+       binary_package_name = json_object_get_string(j);
 
-       binary_package_version = json_object_get_string
-               (json_object_object_get(o, "binary_package_version"));
+       json_object_object_get_ex(o, "binary_package_version", &j);
+       binary_package_version = json_object_get_string(j);
 
-       distro_arch_series_link = json_object_get_string
-               (json_object_object_get(o, "distro_arch_series_link"));
+       json_object_object_get_ex(o, "distro_arch_series_link", &j);
+       distro_arch_series_link = json_object_get_string(j);
 
-       self_link = json_object_get_string
-               (json_object_object_get(o, "self_link"));
+       json_object_object_get_ex(o, "self_link", &j);
+       self_link = json_object_get_string(j);
 
-       arch_specific = json_object_get_boolean
-               (json_object_object_get(o, "architecture_specific"));
+       json_object_object_get_ex(o, "architecture_specific", &j);
+       arch_specific = json_object_get_boolean(j);
 
-       date_created = json_to_time(json_object_object_get(o, "date_created"));
+       json_object_object_get_ex(o, "date_created", &j);
+       date_created = json_to_time(j);
 
-       status = json_object_get_string(json_object_object_get(o, "status"));
+       json_object_object_get_ex(o, "status", &j);
+       status = json_object_get_string(j);
 
        bpph =  bpph_new(binary_package_name,
                         binary_package_version,
@@ -103,7 +109,7 @@ static struct bpph *json_to_bpph(json_object *o)
 
 static json_object *bpph_to_json(struct bpph *bpph)
 {
-       json_object *json;
+       json_object *json, *time;
 
        json = json_object_new_object();
 
@@ -133,8 +139,9 @@ static json_object *bpph_to_json(struct bpph *bpph)
        json_object_object_add
                (json, "status", json_object_new_string(bpph->status));
 
+       time = time_to_json(bpph->date_created);
        json_object_object_add
-               (json, "date_created", time_to_json(bpph->date_created));
+               (json, "date_created", time);
 
        return json;
 }
@@ -146,21 +153,23 @@ struct distro_arch_series *json_object_to_distro_arch_series(json_object *o)
        const char *architecture_tag;
        json_bool is_nominated_arch_indep;
        const char *distroseries_link;
+       json_object *j;
 
-       display_name = json_object_get_string
-               (json_object_object_get(o, "display_name"));
+       json_object_object_get_ex(o, "display_name", &j);
+       display_name = json_object_get_string(j);
 
-       title = json_object_get_string
-               (json_object_object_get(o, "title"));
+       json_object_object_get_ex(o, "title", &j);
+       title = json_object_get_string(j);
 
-       architecture_tag = json_object_get_string
-               (json_object_object_get(o, "architecture_tag"));
+       json_object_object_get_ex(o, "architecture_tag", &j);
+       architecture_tag = json_object_get_string(j);
 
-       distroseries_link = json_object_get_string
-               (json_object_object_get(o, "distroseries_link"));
+       json_object_object_get_ex(o, "distroseries_link", &j);
+       distroseries_link = json_object_get_string              (j);
 
+       json_object_object_get_ex(o, "is_nominated_arch_indep", &j);
        is_nominated_arch_indep = json_object_get_boolean
-               (json_object_object_get(o, "is_nominated_arch_indep"));
+               (j);
 
        return distro_arch_series_new(display_name,
                                      title,
@@ -175,15 +184,19 @@ struct distro_series *json_object_to_distro_series(json_object *o)
        const char *title;
        const char *name;
        const char *version;
+       json_object *j;
 
-       displayname = json_object_get_string
-               (json_object_object_get(o, "displayname"));
+       json_object_object_get_ex(o, "displayname", &j);
+       displayname = json_object_get_string(j);
 
-       title = json_object_get_string(json_object_object_get(o, "title"));
+       json_object_object_get_ex(o, "title", &j);
+       title = json_object_get_string(j);
 
-       version = json_object_get_string(json_object_object_get(o, "version"));
+       json_object_object_get_ex(o, "version", &j);
+       version = json_object_get_string(j);
 
-       name = json_object_get_string(json_object_object_get(o, "name"));
+       json_object_object_get_ex(o, "name", &j);
+       name = json_object_get_string(j);
 
        return distro_series_new(name,
                                 version,
@@ -198,7 +211,7 @@ struct bpph **json_object_to_bpph_list(json_object *o)
        struct bpph **entries, *h;
        const struct distro_arch_series *distro;
 
-       o_entries = json_object_object_get(o, "entries");
+       json_object_object_get_ex(o, "entries", &o_entries);
 
        if (!o_entries)
                return NULL;