title: Libcurl + rapidjson for Streaming Deserialization?
body: <p>I have been tearing my hair out (not much to begin with, though), trying to figure out a way to hit a web service and then parse and deserialize the json in chunks while parsing at the same time into into my objects without storing the entire document (500mb+).    Effectively, I am trying to connect libcurl and rapidjson using SAX-style parsing right into my classes.</p>
  
  <p>Maybe I am thinking about this wrong, but my current line of thought is that during the curl_writeback add the received data to the json text stream.  What I am struggling with is how to initiate and then make the rapidjson parsing engine wait for the next block of data to come in from the curl writeback.  Halting before the uncompleted token is processed until the new stream buffer has been received.   I don't want to append buffers, because I want to free up the previous buffer.</p>
  
  <p>Has anyone done this before?</p>
  
  <p>Here is some code to illustrate the concept...but obviously not complete (and probably all wrong!)</p>
  
  <p>Here is the structure for passing information to curl_write so that it knows how to do a callback to the parser.</p>
  
  <pre><code>struct CurlHandler {
      stringstream *data;
      Reader *reader;
      void *parsehandler;
  };
  </code></pre>
  
  <p>Then before initiating the request, I populate the structure as follows:</p>
  
  <pre><code>rapidjson::Reader reader;
  CurlHandler handler;
  ParseHandler parser;
  handler.reader = &amp;reader;
  handler.parsehandler = &amp;parser;
  SetOption(CURLOPT_WRITEDATA, (void *) &amp;handler);
  </code></pre>
  
  <p>ParseHandler is a class that is needed by rapidjson to do SAX parsing.  The idea was to call the rapidjson parser (somehow) during the curl_write:</p>
  
  <pre><code>static size_t curl_write(void *ptr, size_t size, size_t nmemb, void *CurlHandlerPtr) {
      CurlHandler *handler = (CurlHandler *) CurlHandlerPtr;
  
  //  handler-&gt;reader.Parse(handler-&gt;data, handler-&gt;parsehandler);   ????????
  
      return 1;
  }
  </code></pre>
  
  <p>I sure hope there is an easier answer than this.</p>
  
  <p>Here is the full code for context:</p>
  
  <pre><code>#pragma once
  #include &lt;string&gt;
  #include &lt;iostream&gt;
  #include "curl\curl.h"
  #include "rapidjson\rapidjson.h"
  #include "rapidjson\reader.h"
  
  using namespace std;
  using namespace rapidjson;
  
  struct CurlHandler {
      stringstream *data;
      Reader *reader;
      void *parsehandler;
  };
  
  static size_t curl_write(void *ptr, size_t size, size_t nmemb, void *CurlHandlerPtr) {
      CurlHandler *handler = (CurlHandler *) CurlHandlerPtr;
  
  //  handler-&gt;reader.Parse(handler-&gt;data, handler-&gt;parsehandler);   ????????
  
      return 1;
  }
  
  
  class ParseHandler {
      bool Null() { cout &lt;&lt; "Null()" &lt;&lt; endl; return true; }
      bool Bool(bool b) { cout &lt;&lt; "Bool(" &lt;&lt; boolalpha &lt;&lt; b &lt;&lt; ")" &lt;&lt; endl; return true; }
      bool Int(int i) { cout &lt;&lt; "Int(" &lt;&lt; i &lt;&lt; ")" &lt;&lt; endl; return true; }
      bool Uint(unsigned u) { cout &lt;&lt; "Uint(" &lt;&lt; u &lt;&lt; ")" &lt;&lt; endl; return true; }
      bool Int64(int64_t i) { cout &lt;&lt; "Int64(" &lt;&lt; i &lt;&lt; ")" &lt;&lt; endl; return true; }
      bool Uint64(uint64_t u) { cout &lt;&lt; "Uint64(" &lt;&lt; u &lt;&lt; ")" &lt;&lt; endl; return true; }
      bool Double(double d) { cout &lt;&lt; "Double(" &lt;&lt; d &lt;&lt; ")" &lt;&lt; endl; return true; }
      bool String(const char* str, SizeType length, bool copy) {
          cout &lt;&lt; "String(" &lt;&lt; str &lt;&lt; ", " &lt;&lt; length &lt;&lt; ", " &lt;&lt; boolalpha &lt;&lt; copy &lt;&lt; ")" &lt;&lt; endl;
          return true;
      }
      bool StartObject() { cout &lt;&lt; "StartObject()" &lt;&lt; endl; return true; }
      bool Key(const char* str, SizeType length, bool copy) {
          cout &lt;&lt; "Key(" &lt;&lt; str &lt;&lt; ", " &lt;&lt; length &lt;&lt; ", " &lt;&lt; boolalpha &lt;&lt; copy &lt;&lt; ")" &lt;&lt; endl;
          return true;
      }
      bool EndObject(SizeType memberCount) { cout &lt;&lt; "EndObject(" &lt;&lt; memberCount &lt;&lt; ")" &lt;&lt; endl; return true; }
      bool StartArray() { cout &lt;&lt; "StartArray()" &lt;&lt; endl; return true; }
      bool EndArray(SizeType elementCount) { cout &lt;&lt; "EndArray(" &lt;&lt; elementCount &lt;&lt; ")" &lt;&lt; endl; return true; }
  };
  
  class StreamTest {
  private:
      string _username;
      string _password;
  
      CURL *_curl;
      CURLcode _error;
  
  public:
      StreamTest() {
          curl_global_init(CURL_GLOBAL_ALL);
          _curl = curl_easy_init();
      };
      ~StreamTest() {
          curl_global_cleanup();
      };
  
      int Initialize(string username, string password) {
          _username = username;
          _password = password;
          return 0;
      }
  
      int Get(std::string url) {
          CURLcode result;
  
          rapidjson::Reader reader;
          CurlHandler handler;
          ParseHandler parser;
          handler.reader = &amp;reader;
          handler.parsehandler = &amp;parser;
  
          std::string s = _username; s += ":"; s += _password;
  
          SetOption(CURLOPT_USERPWD, s.c_str());
          SetOption(CURLOPT_URL, url.c_str());
          SetOption(CURLOPT_WRITEFUNCTION, curl_write);
          SetOption(CURLOPT_WRITEDATA, (void *) &amp;handler);
          result = curl_easy_perform(_curl);
  
          return 0;
      }
  
      template &lt;typename ValueType&gt;
      StreamTest&amp; SetOption(CURLoption opt, ValueType val) {
          if(_curl &amp;&amp; _error == CURLE_OK) {
              _error = curl_easy_setopt(_curl, opt, val);
          }
          return *this;
      }
  };
  
  int _tmain(int argc, _TCHAR* argv[]) {
      StreamTest http;
  
      http.Initialize("guest", "passwd");
  
      http.Get("http://localhost/GetData");
  
      cin.get();
  }
  </code></pre>
  
  <p>EDIT:  </p>
  
  <p>Ideally the http.Get function in _tmain would pass in a reference to an object that is to be deserialized into.   The object would receive all the SAX events from rapidjson in a callback of sorts.</p>