Search code examples
c++stdstringrapidjson

RapidJson kArrayType with String


I have following code but its cannot compiled. I cannot think about a reason, please hlep.

rapidjson::Document jsonDoc;
jsonDoc.SetObject();
rapidjson::Document::AllocatorType& allocator = jsonDoc.GetAllocator();

rapidjson::Value messageArr(rapidjson::kArrayType);

std::string test = std::string("TEST");
messageArr.PushBack(test.c_str(), allocator);

Giving me following error;

error: no matching function for call to ‘rapidjson::GenericValue >::PushBack(const char*, rapidjson::GenericDocument >::AllocatorType&)’
messageArr.PushBack(test.c_str(), allocator);


Solution

  • [Edited] - Solution:

      std::string test = std::string("TEST");
      rapidjson::Value strVal;
      strVal.SetString(test.c_str(), test.length(), allocator);
      messageArr.PushBack(strVal, allocator);
    

    See RapidJson tutorial - Create String

    Fluent-style:

     messageArr.PushBack(
          rapidjson::Value{}.SetString(test.c_str(), test.length(), allocator),
          allocator
      );