You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've noticed that when iterating through a json object, calling value() wraps the string with quotes while key() doesn't. This should be changed so that either they both be wrapped with quotes or not wrapped at all.
The text was updated successfully, but these errors were encountered:
key() returns a std::string and value() returns a basic_json object. So the former gives you they key as it was stored, whereas the latter gives you a JSON serialization of the string. This includes escaping characters, see RFC 7159.
So if you want an escaped version of the key, you need to create a JSON value out of it first.
Example:
#include<json.hpp>using nlohmann::json;
intmain()
{
json s = "\"quoted\"";
json j = {{s, s}};
std::cout << "output of j (json): " << j << "\n";
std::cout << "output of the first key (std::string): " << j.begin().key() << "\n";
std::cout << "output of the first key (json): " << json(j.begin().key()) << "\n";
std::cout << "output of the first value (json): " << j.begin().value() << "\n";
std::cout << "output of the first value (std::string): " << j.begin()->get<std::string>() << "\n";
}
Output:
output of j (json): {"\"quoted\"":"\"quoted\""}
output of the first key (std::string): "quoted"
output of the first key (json): "\"quoted\""
output of the first value (json): "\"quoted\""
output of the first value (std::string): "quoted"
I've noticed that when iterating through a json object, calling
value()
wraps the string with quotes whilekey()
doesn't. This should be changed so that either they both be wrapped with quotes or not wrapped at all.The text was updated successfully, but these errors were encountered: