-
-
Save rodolfobandeira/2bb3d5a55dc69141adb2e7903370adc2 to your computer and use it in GitHub Desktop.
Json Parser in C++ with Libcurl and Jsoncpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef PARSER_H | |
#define PARSER_H | |
#include <iostream> | |
#include <string> | |
#include <curl/curl.h> | |
#include <jsoncpp/json/json.h> | |
#include <jsoncpp/json/reader.h> | |
#include <jsoncpp/json/writer.h> | |
#include <jsoncpp/json/value.h> | |
class Parser{ | |
public: | |
Parser(const std::string &s): url(s){} | |
Parser(){} | |
void request(); | |
std::string get_json_string(); | |
Json::Value get_json(){ return root; } | |
private: | |
std::string url; | |
Json::Value root; | |
static std::size_t write_callback(char* in, std::size_t size, std::size_t nmemb, std::string* out); | |
}; | |
std::string Parser::get_json_string(){ | |
Json::FastWriter fastWriter; | |
return fastWriter.write(root); | |
} | |
std::size_t Parser::write_callback(char *in, size_t size, size_t nmemb, std::string *out){ | |
std::size_t total_size = size * nmemb; | |
if(total_size){ | |
out->append(in, total_size); | |
return total_size; | |
} | |
return 0; | |
} | |
void Parser::request(){ | |
// Parse raw Json string | |
std::string str_buffer; | |
CURL *curl = curl_easy_init(); | |
if(curl){ | |
CURLcode res; | |
curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); | |
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback); | |
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &str_buffer); | |
res = curl_easy_perform(curl); | |
if(res != CURLE_OK){ | |
std::cerr << "curl_easy_perform() failed:" << curl_easy_strerror(res) << std::endl; | |
return; | |
} | |
curl_easy_cleanup(curl); | |
}else{ | |
std::cout << "Could not initialize curl" << std::endl; | |
} | |
// Convert string to Json::Value | |
Json::Reader reader; | |
bool parse_status = reader.parse(str_buffer.c_str(), root); | |
if (!parse_status){ | |
std::cerr << "parse() failed: " << reader.getFormattedErrorMessages() << std::endl; | |
return; | |
} | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment