title: Rapidjson returning reference to Document Value
body: <p>I'm having some trouble with the following method and I need some help trying to figure out what I am doing wrong.</p>
  
  <p>I want to return a reference to a Value in a document. I am passing the Document from outside the function so that when I read a json file into it I don't "lose it".</p>
  
  <pre><code>const rapidjson::Value&amp; CTestManager::GetOperations(rapidjson::Document&amp; document)
  {
      const Value Null(kObjectType);
  
      if (m_Tests.empty())
          return Null;
  
      if (m_current &gt; m_Tests.size() - 1)
          return Null;
  
      Test&amp; the_test = m_Tests[m_current];
  
      CMyFile fp(the_test.file.c_str()); // non-Windows use "r"
      if (!fp.is_open())
          return Null;
  
      u32 operations_count = 0;
  
      CFileBuffer json(fp);
      FileReadStream is(fp.native_handle(), json, json.size());
  
      if (document.ParseInsitu&lt;kParseCommentsFlag&gt;(json).HasParseError())
      {
          (...)
      }
      else
      {
          if (!document.IsObject())
          {
              (...)
          }
          else
          {
              auto tests = document.FindMember("td_tests");
              if (tests != document.MemberEnd())
              {
                  for (SizeType i = 0; i &lt; tests-&gt;value.Size(); i++)
                  {
                      const Value&amp; test = tests-&gt;value[i];
  
                      if (test["id"].GetInt() == the_test.id)
                      {
                          auto it = test.FindMember("operations");
                          if (it != test.MemberEnd())
                          {
                              //return it-&gt;value; is this legitimate?
                              return test["operations"];
                          }
  
                          return Null;
                      }
                  }
              }
          }
      }
  
      return Null;
  }
  </code></pre>
  
  <p>Which I am calling like this:</p>
  
  <pre><code>Document document;
  auto operations = TestManager().GetOperations(document);
  </code></pre>
  
  <p>When I inspect the value of <code>test["operations"]</code> inside the function I can see everything I would expect (debug code removed from the abode code).</p>
  
  <p>When I inspect the returned value outside the function I can see that it's an array (which I expect). the member count int the array is correct as well, but when print it out, I only see garbage instead.</p>
  
  <p>When I "print" the Value to a string inside the methods, I get what I expect (i.e. a well formated json), but when I do it outside all keys show up as "IIIIIIII" and values that aren't strings show up correctly.</p>
  
  <pre><code>    rapidjson::StringBuffer strbuf2;
      rapidjson::PrettyWriter&lt;rapidjson::StringBuffer&gt; writer2(strbuf2);
      ops-&gt;Accept(writer2);
  </code></pre>
  
  <p>As this didn't work I decided to change the method to receive a Value as a parameter and do a deep copy into it like this</p>
  
  <pre><code>u32 CTestManager::GetOperationsEx(rapidjson::Document&amp; document, rapidjson::Value&amp; operations)
  {
     (...)
  
      if (document.ParseInsitu&lt;kParseCommentsFlag&gt;(json).HasParseError())
      {
          (...)
      }
      else
      {
          if (!document.IsObject())
          {
              (...)
          }
          else
          {
              auto tests = document.FindMember("tests");
              if (tests != document.MemberEnd())
              {
                  for (SizeType i = 0; i &lt; tests-&gt;value.Size(); i++)
                  {
                      const Value&amp; test = tests-&gt;value[i];
  
                      if (test["id"].GetInt() == the_test.id)
                      {
                          const Value&amp; opv = test["operations"];
  
                          Document::AllocatorType&amp; allocator = document.GetAllocator();
                          operations.CopyFrom(opv, allocator);    //would Swap work?
                          return operations.Size();
                      }
                  }
              }
          }
      }
  
      return 0;
  }
  </code></pre>
  
  <p>Which I'm calling like this:</p>
  
  <pre><code>Document document;
  Value operations(kObjectType);
  u32 count = TestManager().GetOperationsEx(document, operations);
  </code></pre>
  
  <p>But... I get same thing!!!!</p>
  
  <p>I know that it's going to be something silly but I can't put my hands on it!</p>
  
  <p>Any ideas?</p>