title: Is there way to use rapidjson with std::string efficiently?
body: <p>I'm trying to work with rapidjson. </p>
  
  <p>I want to generate string and add it to some <code>rapidjson::Value</code> which is object.</p>
  
  <p>I was using std::string when worked with qjson, but in case of rapidjson it seems inappropriate. I don't want to generate string and then copy it, string object lifetime ends before object (<code>rapidjson::Value</code>) lifetime (therefore <code>generated_string.c_str()</code> is not a case).
  There may be \0 in json, so, <code>char*</code> with null-terminated string also not a solution.</p>
  
  <p>So, I must write my own string type? Or use something like </p>
  
  <pre><code>auto create_string_object() -&gt; rapidjson::GenericStringRef&lt;char&gt;
  {
     size_t len;
     char* result;
      // generate char* result with length len
     const char* return_value = result;
     return rapidjson::StringRef(return_value,len);
  }
  auto create_object_object(rapidjson::Document::AllocatorType &amp;allocator) -&gt; rapidjson::Value
  {
     // ...
     rapidjson::Value result(rapidjson::kObjectType);
     rapidjson::Value tmp;  // tmp = create_string_object() will not compile
     tmp = create_string_object();
     result.AddMember("key", tmp, allocator); 
     // ...
  }
  </code></pre>
  
  <p>Or there are some other ways to work with strings? It seems hard to me. We can't move string to <code>rapidjson::Value</code> because of different structures inside that <code>Value</code>, we can't set pointer inside <code>Value</code> to <code>c_str()</code> because string will be destroyed before <code>Value</code>.
  Even with <code>GenericStringRef&lt;char&gt;</code> I must rewrite almost all work with strings.</p>
  
  <p>By the way, why <code>RAPIDJSON_HAS_STDSTRING</code> is 0 by default? Some problems with work? I see that i can copy string into <code>rapidjson::Value</code> and copy pointer if I know that string lifetime will end before value lifetime. </p>
  
  <p><strong>UPD:</strong> Now I see that rapidjson frees only strings that were copied:</p>
  
  <pre><code>  //! Destructor.
    /*! Need to destruct elements of array, members of object, or copy-string.
    */
    ~GenericValue() {
        if (Allocator::kNeedFree) { // Shortcut by Allocator's trait
            switch(flags_) {
            case kArrayFlag:
                for (GenericValue* v = data_.a.elements; v != data_.a.elements + data_.a.size; ++v)
                    v-&gt;~GenericValue();
                Allocator::Free(data_.a.elements);
                break;
  
            case kObjectFlag:
                for (MemberIterator m = MemberBegin(); m != MemberEnd(); ++m)
                  m-&gt;~Member();
                Allocator::Free(data_.o.members);
                break;
  
            case kCopyStringFlag:
                Allocator::Free(const_cast&lt;Ch*&gt;(data_.s.str));
                break;
  
            default:
                break;  // Do nothing for other types.
            }
        }
    }
  </code></pre>
  
  <p>So, as it was said in answer, using <code>GenericStringRef</code> in a way such as in my code here is a bad idea, because in such case I must manage memory by myself. </p>