title: Get string for entire rapidjson::Document
body: <p>How do I get the entire rapidjson::Document as a string?</p>
  
  <p>I've got the following code:</p>
  
  <pre><code>rapidjson::Document jsonDoc;
  rapidjson::MemoryPoolAllocator&lt;&gt; &amp; allocator = jsonDoc.GetAllocator();
  jsonDoc.SetObject();
  jsonDoc.AddMember("ACTION", "poop", allocator);
  jsonDoc.AddMember("TRANSACTIONID", 2, allocator);
  std::string json = jsonDoc.GetString(); // Assert fails here, not a string
  </code></pre>
  
  <p>and it fails an assert, because it is not a string.</p>
  
  <p>I've seen some examples where people use SAX rather than DOM to get string using:</p>
  
  <pre><code>rapidjson::StringBuffer buffer;
  buffer.Clear();
  rapidjson::Writer&lt;rapidjson::StringBuffer&gt; writer(buffer);
  
  writer.StartObject();
  
  writer.String("ACTION");
  writer.String("poop");
  
  writer.String("Trans");
  writer.Int(19);
  
  writer.EndObject();
  
  std::string json = buffer.GetString();
  </code></pre>
  
  <p>But I'd rather build my DOM the former way and not use SAX.</p>
  
  <hr>
  
  <p>EDIT:</p>
  
  <p>Reading the documentation some more, it would appear they want us to use SAX to convert a DOM to a string? From the following:</p>
  
  <p>"DOM as SAX Event Publisher</p>
  
  <p>In RapidJSON, stringifying a DOM with Writer may be look a little bit weired.
      // ...
      Writer writer(buffer);
      d.Accept(writer);</p>
  
  <p>Actually, Value::Accept() is responsible for publishing SAX events about the value to the handler. With this design, Value and Writer are decoupled. Value can generate SAX events, and Writer can handle those events.</p>
  
  <p>User may create custom handlers for transforming the DOM into other formats. For example, a handler which converts the DOM into XML.</p>
  
  <p>For more about SAX events and handler, please refer to SAX.
  "</p>
  
  <p>So, I've come up with the following code. Can someone verify this is the way to do it?</p>
  
  <pre><code>std::string myString1("poop");
  const std::string myString2("poopy");
  
  rapidjson::Document jsonDoc;
  rapidjson::MemoryPoolAllocator&lt;&gt; &amp; allocator = jsonDoc.GetAllocator();
  
  jsonDoc.SetObject();
  
  // We have to use StringRef because they can't handle an std::string, WTF?
  jsonDoc.AddMember("ACTION", rapidjson::StringRef(myString1.c_str()), allocator);
  jsonDoc.AddMember("CONSTACTION", rapidjson::StringRef(myString2.c_str()), allocator);
  jsonDoc.AddMember("TRANSACTIONID", 2, allocator);
  
  rapidjson::StringBuffer buffer;
  buffer.Clear();
  rapidjson::Writer&lt;rapidjson::StringBuffer&gt; writer(buffer);
  jsonDoc.Accept(writer);
  std::string json = buffer.GetString();
  </code></pre>