2

I am trying to use an std::string with RapidJson

using namespace std;
using namespace rapidjson;
const char* json = "{\n"
                   "    \"id\": null\n"
                   "    \"code\": null\n"
                   "}";
Document d;
string a = "myString";
d["myValue"].SetString(a); //error: no matching member function for call to 'SetString' in the compiler

I just want to be able to edit my json with rapidjson using std::string, but it is not working. New to c++ btw, so sorry if it is a stupid question.

Edit: I tried the solution from Jorge Perez, but I am still getting this error:

/include/rapidjson/document.h:1139: rapidjson::GenericValue<Encoding, Allocator>& rapidjson::GenericValue<Encoding, Allocator>::operator[](const rapidjson::GenericValue<Encoding, SourceAllocator>&) [with SourceAllocator = rapidjson::MemoryPoolAllocator<>; Encoding = rapidjson::UTF8<>; Allocator = rapidjson::MemoryPoolAllocator<>]: Assertion `false' failed.

Any ideas?

Sandeep
  • 18,356
  • 16
  • 68
  • 108
Ivan Solis
  • 23
  • 1
  • 5

1 Answers1

0

If you have a string:

std::string s = "myString"; 

You can set it in RapidJson by using the data and size:

document["myValue"].SetString(s.data(), s.size(), document.GetAllocator());
Alecto Irene Perez
  • 10,321
  • 23
  • 46
  • I did that, now it is giving me ScrabbleClient: ../Downloads/rapidjson-master/include/rapidjson/document.h:1139: rapidjson::GenericValue& rapidjson::GenericValue::operator[](const rapidjson::GenericValue&) [with SourceAllocator = rapidjson::MemoryPoolAllocator<>; Encoding = rapidjson::UTF8<>; Allocator = rapidjson::MemoryPoolAllocator<>]: Assertion `false' failed. Any ideas? – Ivan Solis Apr 09 '19 at 06:22
  • So @IvanSolis did you read what near the 1139 line inside ./Downloads/rapidjson-master/include/rapidjson/document.h ? Because there is a really nice [comment](http://rapidjson.org/document_8h_source.html) there on line 1105. – KamilCuk Apr 09 '19 at 07:26