Search code examples
c++jsoncpp

Why don't operator= for Json::Value?


example:

std::string strJson = R"({"foo": "bar"})";
Json::Value root = strJson; // if it implement operator=(std::string&)

instead

std::string strJson = R"({"foo": "bar"})";

Json::CharReaderBuilder builder;
Json::CharReader* reader = builder.newCharReader();

Json::Value json;
std::string errors;

bool parsingSuccessful = reader->parse(
    strJson.c_str(),
    strJson.c_str() + strJson.size(),
    &json,
    &errors
);
delete reader;

My think is the former is very convenient than the latter.

Why don't operator= for Json::Value?


Solution

  • You might use std::istream& operator>>(std::istream&, Value&):

    Json::Value root;
    std::stringstream(R"({"foo": "bar"})") >> root;
    

    Constructor taking std::string is to build a value containing the string value (and so isString() would return true).