nilan
nilan

Reputation: 61

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);

Upvotes: 3

Views: 7759

Answers (2)

Devolus
Devolus

Reputation: 22074

using namespace rapidjson;
using namespace std;

Value array(kArrayType);
string test = "TEST";
Value cat(test.c_str(), allocator);
array.PushBack(cat, allocator);

Upvotes: 1

Adrian Colomitchi
Adrian Colomitchi

Reputation: 3992

[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
  );

Upvotes: 6

Related Questions