为什么 运算符 = 不为 Json::Value?

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

本文关键字:Value Json 不为 运算符 为什么      更新时间:2023-10-16

example:

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

相反

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;

我认为前者比后者非常方便。

为什么 运算符 = 不为 Json::Value?

你可以使用std::istream& operator>>(std::istream&, Value&)

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

构造函数采用std::string是构建一个包含字符串值的值(因此isString()将返回true(。