X-Git-Url: http://git.wpitchoune.net/gitweb/?a=blobdiff_plain;f=src%2Flp_json.c;h=10b5927945ec1f4e8d649caba8b9a69eaf4d208d;hb=80c2b1ecf858c7df7a7e447ed4e7e10393be6a17;hp=ddb8f31f836121afa2ab8efff6becda751bff893;hpb=5a64d0eb4e839196874e7dfde19765f90e10598c;p=ppastats.git diff --git a/src/lp_json.c b/src/lp_json.c index ddb8f31..10b5927 100644 --- a/src/lp_json.c +++ b/src/lp_json.c @@ -23,14 +23,9 @@ #include #include -#include "lp_json.h" -#include "lp_ws.h" - -/* Declares json_bool to have consistent code even with - old json lib releases using boolean instead of json_bool.*/ -#ifndef json_bool -typedef boolean json_bool; -#endif +#include +#include +#include static time_t json_to_time(json_object *json) { @@ -42,6 +37,7 @@ 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); @@ -51,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_str(t); + 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) @@ -106,7 +106,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(); @@ -136,8 +136,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; } @@ -258,6 +259,7 @@ json_object_to_daily_download_total(const char *d, json_object *o_c) result = malloc(sizeof(struct daily_download_total)); result->count = json_object_get_int(o_c); + memset(&result->date, 0, sizeof(struct tm)); strptime(d, "%FT%T%z", &result->date); return result; @@ -298,3 +300,41 @@ json_object_to_daily_download_totals(json_object *o) return result; } + +struct json_object *date_to_json(struct tm *tm) +{ + json_object *json; + + json = json_object_new_array(); + json_object_array_add(json, json_object_new_int(tm->tm_year+1900)); + json_object_array_add(json, json_object_new_int(tm->tm_mon+1)); + json_object_array_add(json, json_object_new_int(tm->tm_mday)); + + return json; +} + +json_object *ddts_to_json(struct daily_download_total **ddts) +{ + json_object *json_ddt, *json_ddts; + struct daily_download_total *ddt; + + json_ddts = json_object_new_array(); + + while (ddts && *ddts) { + ddt = *ddts; + + json_ddt = json_object_new_object(); + json_object_object_add(json_ddt, + "value", + json_object_new_int(ddt->count)); + json_object_object_add(json_ddt, + "time", + date_to_json(&ddt->date)); + + json_object_array_add(json_ddts, json_ddt); + + ddts++; + } + + return json_ddts; +}