Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 632
Ivan Solis Avatar asked Nov 19 '25 02:11

Ivan Solis


1 Answers

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());
like image 52
Alecto Irene Perez Avatar answered Nov 21 '25 14:11

Alecto Irene Perez



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!