#include "json_curl.h" #include #include #include struct json_builder { json_object *obj; json_tokener *tok; }; static size_t parse_cb(char *data, size_t size, size_t nmemb, void *userdata) { size_t rsize = size * nmemb; struct json_builder *jb = userdata; // extra data after the first valid object is ignored. if (!jb->obj) { json_object *tmp = json_tokener_parse_ex(jb->tok, data, rsize); if (tmp) jb->obj = tmp; else if (json_tokener_get_error(jb->tok) != json_tokener_continue) return 0; } return rsize; } json_object *json_curl_perform(CURL *curl, const char *uri) { struct json_builder jb = { NULL, json_tokener_new() }; if (uri) curl_easy_setopt(curl, CURLOPT_URL, uri); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, parse_cb); curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&jb); CURLcode res = curl_easy_perform(curl); json_tokener_free(jb.tok); if (res != CURLE_OK) return NULL; return jb.obj; }