title: rapidjson modifying document creating object and removing member causes assert when writing document
body: <p>I have a bit of code where I parse a json file, edit it, and save it again. The code works fine as long as I don't create an object and then remove a member. Doing those two steps causes an assertion in the object where the member was removed when writing again. I've compiled a small example to show what I mean:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>#define RAPIDJSON_HAS_STDSTRING 1
  
  #include "rapidjson/document.h"
  #include "rapidjson/prettywriter.h"
  #include "rapidjson/istreamwrapper.h"
  
  #include &lt;iostream&gt;
  #include &lt;fstream&gt;
  
  
  int main( int argc, char *argv[] )
  {
      rapidjson::Document document;
  
      {
          std::ifstream file( "test.json" );
          rapidjson::IStreamWrapper file_wrapper( file );
  
  
          // Now load it up
          if( document.ParseStream( file_wrapper ).HasParseError() )
          {
              std::cout &lt;&lt; "Configuration: Error parsing JSON." &lt;&lt; std::endl;
              return( EXIT_FAILURE );
          }
      }
  
      // Now move some things around
      {
          auto &amp;allocator = document.GetAllocator();
  
          // Shorthand
          auto &amp;one = document[ "one" ];
  
          // Move each
          if( one.HasMember( "fruit" ) and one[ "fruit" ].IsString() )
          {
              const std::string &amp;s = one[ "fruit" ].GetString();
  
              document.AddMember( "fruit", s, allocator );
  
              one.RemoveMember( "fruit" );
          }
          if( one.HasMember( "pear" ) and one[ "pear" ].IsString() )
          {
              std::string s = one[ "pear" ].GetString();
  
              rapidjson::Value value( rapidjson::kObjectType );
  
              value.AddMember( "pear", s, allocator );
  
              document.AddMember( "sugar", std::move( value ), allocator );
  
              one.RemoveMember( "pear" );
          }
          if( one.HasMember( "apple" ) and one[ "apple" ].IsString() )
          {
              const std::string &amp;s = one[ "apple" ].GetString();
  
              document.AddMember( "apple", s, allocator );
  
              one.RemoveMember( "apple" );
          }
      }
  
      // Write it out
      rapidjson::StringBuffer sb;
      rapidjson::PrettyWriter&lt;rapidjson::StringBuffer&gt; writer( sb );
  
      // Write from the document
      document.Accept( writer );
  
      // Open the file
      std::ofstream file( "test-out.json" );
  
      // Now write
      file &lt;&lt; sb.GetString();
  }
  </code></pre>
  
  <p>Parsing this test.json file:</p>
  
  <pre><code>{
      "one" : {
          "fruit": "box",
          "apple": "plane",
          "pear" : "quincy"
      },
      "two" : {
          "french" : "pub"
      }
  }
  </code></pre>
  
  <p>Will produce this output</p>
  
  <pre><code>report: rapidjson/document.h:1878: bool rapidjson::GenericValue&lt;Encoding, Allocator&gt;::Accept(Handler&amp;) const [with Handler = rapidjson::PrettyWriter&lt;rapidjson::GenericStringBuffer&lt;rapidjson::UTF8&lt;&gt; &gt; &gt;; Encoding = rapidjson::UTF8&lt;&gt;; Allocator = rapidjson::MemoryPoolAllocator&lt;&gt;]: Assertion `m-&gt;name.IsString()' failed.
  Aborted
  </code></pre>
  
  <p>If I take out all of the one.RemoveMember( ... ) lines the code will work. If I remove the block about adding an object it will work. RemoveMember and adding an object can't seem to co-exist.</p>
  
  <p>How can this be sorted out?</p>
  
  <p>Here is a backtrace from gdb:</p>
  
  <pre><code>__GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:50
  50      ../sysdeps/unix/sysv/linux/raise.c: No such file or directory.
  (gdb) bt
  #0  __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:50
  #1  0x00007ffff7a65535 in __GI_abort () at abort.c:79
  #2  0x00007ffff7a6540f in __assert_fail_base (fmt=0x7ffff7bdeea8 "%s%s%s:%u: %s%sAssertion `%s' failed.\n%n", assertion=0x555555562181 "m-&gt;name.IsString()", file=0x55555556212c "rapidjson/document.h", line=1878, 
      function=&lt;optimized out&gt;) at assert.c:92
  #3  0x00007ffff7a73852 in __GI___assert_fail (assertion=0x555555562181 "m-&gt;name.IsString()", file=0x55555556212c "rapidjson/document.h", line=1878, 
      function=0x555555564ec0 &lt;rapidjson::GenericValue&lt;rapidjson::UTF8&lt;char&gt;, rapidjson::MemoryPoolAllocator&lt;rapidjson::CrtAllocator&gt; &gt;::Accept&lt;rapidjson::PrettyWriter&lt;rapidjson::GenericStringBuffer&lt;rapidjson::UTF8&lt;char&gt;, rapidjson::CrtAllocator&gt;, rapidjson::UTF8&lt;char&gt;, rapidjson::UTF8&lt;char&gt;, rapidjson::CrtAllocator, 0u&gt; &gt;(rapidjson::PrettyWriter&lt;rapidjson::GenericStringBuffer&lt;rapidjson::UTF8&lt;char&gt;, rapidjson::CrtAllocator&gt;, rapidjson::UTF8&lt;char&gt;, rapidjson::UTF8&lt;char&gt;, rapidjson::CrtAllocator, 0u&gt;&amp;) const::__PRETTY_FUNCTION__&gt; "bool rapidjson::GenericValue&lt;Encoding, Allocator&gt;::Accept(Handler&amp;) const [with Handler = rapidjson::PrettyWriter&lt;rapidjson::GenericStringBuffer&lt;rapidjson::UTF8&lt;&gt; &gt; &gt;; Encoding = rapidjson::UTF8&lt;&gt;; Al"...) at assert.c:101
  #4  0x000055555555a22d in bool rapidjson::GenericValue&lt;rapidjson::UTF8&lt;char&gt;, rapidjson::MemoryPoolAllocator&lt;rapidjson::CrtAllocator&gt; &gt;::Accept&lt;rapidjson::PrettyWriter&lt;rapidjson::GenericStringBuffer&lt;rapidjson::UTF8&lt;char&gt;, rapidjson::CrtAllocator&gt;, rapidjson::UTF8&lt;char&gt;, rapidjson::UTF8&lt;char&gt;, rapidjson::CrtAllocator, 0u&gt; &gt;(rapidjson::PrettyWriter&lt;rapidjson::GenericStringBuffer&lt;rapidjson::UTF8&lt;char&gt;, rapidjson::CrtAllocator&gt;, rapidjson::UTF8&lt;char&gt;, rapidjson::UTF8&lt;char&gt;, rapidjson::CrtAllocator, 0u&gt;&amp;) const ()
  #5  0x000055555555a2c5 in bool rapidjson::GenericValue&lt;rapidjson::UTF8&lt;char&gt;, rapidjson::MemoryPoolAllocator&lt;rapidjson::CrtAllocator&gt; &gt;::Accept&lt;rapidjson::PrettyWriter&lt;rapidjson::GenericStringBuffer&lt;rapidjson::UTF8&lt;char&gt;, rapidjson::CrtAllocator&gt;, rapidjson::UTF8&lt;char&gt;, rapidjson::UTF8&lt;char&gt;, rapidjson::CrtAllocator, 0u&gt; &gt;(rapidjson::PrettyWriter&lt;rapidjson::GenericStringBuffer&lt;rapidjson::UTF8&lt;char&gt;, rapidjson::CrtAllocator&gt;, rapidjson::UTF8&lt;char&gt;, rapidjson::UTF8&lt;char&gt;, rapidjson::CrtAllocator, 0u&gt;&amp;) const ()
  #6  0x00005555555568df in main ()
  </code></pre>