Search code examples
c++c++17rapidjson

Cannot use std::string variable in RapidJSON function call


I am all new to C++ and am running into an issue. I am using rapidJSON to create JSON documents.

void setKeyValue() {
        Value obj(kObjectType);
        Value key("key");
        Value val(42);;
        obj.AddMember(key, val, d.GetAllocator());
    }

Works as expected. But when I try to replace the call to key to make it use a passed in param, like so:

void setKeyValue(string myKey) {
        Value obj(kObjectType);
        Value key(myKey);
        Value val(42);;
        obj.AddMember(key, val, d.GetAllocator());
    }

The myKey in Value key(myKey) get a red curly underling in Visual Studio saying the following:

enter image description here

What is causing this and how can I solve it?


Solution

  • You don't get support for std::string by default. rapidJSON requires you to specify you want std::string support.

    #define RAPIDJSON_HAS_STDSTRING 1
    

    Only then is this constructor you're using valid:

    GenericValue (const std::basic_string< Ch > &s, Allocator &allocator)