ChatON: json access helper which raises exception if key missing

This commit is contained in:
HanishKVC 2024-05-12 17:08:03 +05:30
parent f94fed92d3
commit 4eae05a6b7

View file

@ -312,6 +312,25 @@ public:
#ifdef CHATON_JSON #ifdef CHATON_JSON
inline std::string json_get_str(json &j, std::vector<std::string> &keys, const std::string &msgTag) {
json curJ = j;
std::stringstream skey;
int i = 0;
for(auto key: keys) {
if (i != 0) skey << "-";
i += 1;
skey << key;
if (curJ.contains(key)) {
curJ = curJ[key];
} else {
std::stringstream ss;
ss << "ERRR:ChatON:" << __func__ << msgTag << ":Key [" << skey.str() << "] is missing";
throw std::runtime_error(ss.str());
}
}
return curJ;
}
inline bool chaton_meta_load(const std::string &fname) { inline bool chaton_meta_load(const std::string &fname) {
std::ifstream f(fname); std::ifstream f(fname);
json conMeta = json::parse(f); json conMeta = json::parse(f);