Search code examples
c++jsonrapidjson

Why is rapidjson giving me problems with std::string?


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?


Solution

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