Commit da459ff0 authored by Alexander Lapshin's avatar Alexander Lapshin

added try catch blocks to json loading functions

parent 831edfb3
This diff is collapsed.
...@@ -6,73 +6,88 @@ ...@@ -6,73 +6,88 @@
static bool loadjson(const jsonval& parent, const std::string& key, std::vector<TimeJD>& vec, const bool exist = true, bool* loaded = nullptr) static bool loadjson(const jsonval& parent, const std::string& key, std::vector<TimeJD>& vec, const bool exist = true, bool* loaded = nullptr)
{ {
jsonval::ConstMemberIterator itr; try {
if (!extract_value(parent, key, exist, loaded, itr)) { jsonval::ConstMemberIterator itr;
return false; if (!extract_value(parent, key, exist, loaded, itr)) {
} return false;
}
const jsonval& a = itr->value;
if (!a.IsArray()) { const jsonval& a = itr->value;
throw Exp() << "error: element is not an array";
}
for (jsonval::ConstValueIterator it = a.Begin(); it != a.End(); ++it) { if (!a.IsArray()) {
if (it->IsString()) { throw Exp() << "element is not an array";
vec.emplace_back(StrToDate(it->GetString()));
} }
else {
throw Exp() << "error: unsupported type"; for (jsonval::ConstValueIterator it = a.Begin(); it != a.End(); ++it) {
if (it->IsString()) {
vec.emplace_back(StrToDate(it->GetString()));
}
else {
throw Exp() << "unsupported type";
}
} }
} }
catch (std::exception& e) {
throw Exp() << "failed to load key '" << key << "'" << " - " << trim(e.what()) << "\n";
}
return true; return true;
} }
static void loadjson(const jsonval& parent, const std::string& key, TimeJD& value, bool exist = true, bool* loaded = nullptr) static void loadjson(const jsonval& parent, const std::string& key, TimeJD& value, bool exist = true, bool* loaded = nullptr)
{ {
const jsonval::ConstMemberIterator itr = parent.FindMember(key.c_str()); try {
const jsonval::ConstMemberIterator itr = parent.FindMember(key.c_str());
if (itr == parent.MemberEnd() && exist) { if (itr == parent.MemberEnd() && exist) {
throw Exp() << "error: element not found " << key; throw Exp() << "error: element not found " << key;
} }
else if (itr == parent.MemberEnd() && !exist) { else if (itr == parent.MemberEnd() && !exist) {
if (loaded) { if (loaded) {
*loaded = false; *loaded = false;
}
return;
} }
return;
}
const jsonval& a = itr->value; const jsonval& a = itr->value;
if (a.IsString()) { if (a.IsString()) {
TimeGD gd; TimeGD gd;
StrToDate(a.GetString(), gd); StrToDate(a.GetString(), gd);
value = gd; value = gd;
if (loaded) { if (loaded) {
*loaded = true; *loaded = true;
}
}
else {
throw Exp() << "unexpected type";
} }
} }
else { catch (std::exception& e) {
throw (Exp() << "element '" << key << "'" << " has unexpected format '"); throw Exp() << "failed to load key '" << key << "'" << " - " << trim(e.what()) << "\n";
} }
} }
static void loadjson(const jsonval& parent, const std::string& key, TimeGD& value, bool exist = true) static void loadjson(const jsonval& parent, const std::string& key, TimeGD& value, bool exist = true)
{ {
const jsonval::ConstMemberIterator itr = parent.FindMember(key.c_str()); try {
const jsonval::ConstMemberIterator itr = parent.FindMember(key.c_str());
if (itr == parent.MemberEnd() && exist) { if (itr == parent.MemberEnd() && exist) {
throw Exp() << "error: element not found"; throw Exp() << "element not found";
} }
const jsonval& a = itr->value; const jsonval& a = itr->value;
if (a.IsString()) { if (a.IsString()) {
StrToDate(a.GetString(), value); StrToDate(a.GetString(), value);
}
else {
throw Exp() << "unexpected type";
}
} }
else { catch (std::exception& e) {
throw (Exp() << "element '" << key << "'" << " has unexpected format '"); throw Exp() << "failed to load key '" << key << "'" << " - " << trim(e.what()) << "\n";
} }
} }
...@@ -93,20 +108,25 @@ static void PutValue(jsonalloc& alc, jsonval& ss, const std::string& conName, co ...@@ -93,20 +108,25 @@ static void PutValue(jsonalloc& alc, jsonval& ss, const std::string& conName, co
static bool loadjson(const jsonval& parent, const std::string& key, svalue<TimeJD>& value, const bool exist = true) static bool loadjson(const jsonval& parent, const std::string& key, svalue<TimeJD>& value, const bool exist = true)
{ {
jsonval::ConstMemberIterator itr; try {
jsonval::ConstMemberIterator itr;
bool* loaded = nullptr; bool* loaded = nullptr;
if (!extract_value(parent, key, exist, loaded, itr)) { if (!extract_value(parent, key, exist, loaded, itr)) {
return false; return false;
} }
const jsonval& a = itr->value; const jsonval& a = itr->value;
if (a.IsString()) { if (a.IsString()) {
value = StrToDate(a.GetString()); value = StrToDate(a.GetString());
}
else {
throw Exp() << "unexpected type";
}
} }
else { catch (std::exception& e) {
throw (Exp() << "element '" << key << "'" << " has unexpected format '"); throw Exp() << "failed to load key '" << key << "'" << " - " << trim(e.what()) << "\n";
} }
return true; return true;
...@@ -114,36 +134,46 @@ static bool loadjson(const jsonval& parent, const std::string& key, svalue<TimeJ ...@@ -114,36 +134,46 @@ static bool loadjson(const jsonval& parent, const std::string& key, svalue<TimeJ
static bool loadjson(const std::map<std::string, std::string>& parent, const std::string& key, svalue<TimeJD>& value, const bool exist = true) static bool loadjson(const std::map<std::string, std::string>& parent, const std::string& key, svalue<TimeJD>& value, const bool exist = true)
{ {
std::map<std::string, std::string>::const_iterator itr; try {
std::map<std::string, std::string>::const_iterator itr;
bool* loaded = nullptr; bool* loaded = nullptr;
if (!extract_value(parent, key, exist, loaded, itr)) { if (!extract_value(parent, key, exist, loaded, itr)) {
return false; return false;
} }
value = StrToDate(itr->second); value = StrToDate(itr->second);
}
catch (std::exception& e) {
throw Exp() << "failed to load key '" << key << "'" << " - " << trim(e.what()) << "\n";
}
return true; return true;
} }
static bool loadjson(const std::map<std::string, std::string>& parent, const std::string& key, TimeJD& value, const bool exist = true, bool* loaded = nullptr) static bool loadjson(const std::map<std::string, std::string>& parent, const std::string& key, TimeJD& value, const bool exist = true, bool* loaded = nullptr)
{ {
std::map<std::string, std::string>::const_iterator itr; try {
std::map<std::string, std::string>::const_iterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) { if (!extract_value(parent, key, exist, loaded, itr)) {
return false; return false;
} }
const std::string& str = itr->second; const std::string& str = itr->second;
if (str.empty() && !exist) { if (str.empty() && !exist) {
if (loaded) { if (loaded) {
*loaded = false; *loaded = false;
}
return false;
} }
return false;
}
value = StrToDate(str); value = StrToDate(str);
}
catch (std::exception& e) {
throw Exp() << "failed to load key '" << key << "'" << " - " << trim(e.what()) << "\n";
}
return true; return true;
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment