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 @@
static bool loadjson(const jsonval& parent, const std::string& key, std::vector<TimeJD>& vec, const bool exist = true, bool* loaded = nullptr)
{
jsonval::ConstMemberIterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
return false;
}
const jsonval& a = itr->value;
try {
jsonval::ConstMemberIterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
return false;
}
if (!a.IsArray()) {
throw Exp() << "error: element is not an array";
}
const jsonval& a = itr->value;
for (jsonval::ConstValueIterator it = a.Begin(); it != a.End(); ++it) {
if (it->IsString()) {
vec.emplace_back(StrToDate(it->GetString()));
if (!a.IsArray()) {
throw Exp() << "element is not an array";
}
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;
}
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) {
throw Exp() << "error: element not found " << key;
}
else if (itr == parent.MemberEnd() && !exist) {
if (loaded) {
*loaded = false;
if (itr == parent.MemberEnd() && exist) {
throw Exp() << "error: element not found " << key;
}
else if (itr == parent.MemberEnd() && !exist) {
if (loaded) {
*loaded = false;
}
return;
}
return;
}
const jsonval& a = itr->value;
const jsonval& a = itr->value;
if (a.IsString()) {
TimeGD gd;
StrToDate(a.GetString(), gd);
value = gd;
if (loaded) {
*loaded = true;
if (a.IsString()) {
TimeGD gd;
StrToDate(a.GetString(), gd);
value = gd;
if (loaded) {
*loaded = true;
}
}
else {
throw Exp() << "unexpected type";
}
}
else {
throw (Exp() << "element '" << key << "'" << " has unexpected format '");
catch (std::exception& e) {
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)
{
const jsonval::ConstMemberIterator itr = parent.FindMember(key.c_str());
try {
const jsonval::ConstMemberIterator itr = parent.FindMember(key.c_str());
if (itr == parent.MemberEnd() && exist) {
throw Exp() << "error: element not found";
}
if (itr == parent.MemberEnd() && exist) {
throw Exp() << "element not found";
}
const jsonval& a = itr->value;
const jsonval& a = itr->value;
if (a.IsString()) {
StrToDate(a.GetString(), value);
if (a.IsString()) {
StrToDate(a.GetString(), value);
}
else {
throw Exp() << "unexpected type";
}
}
else {
throw (Exp() << "element '" << key << "'" << " has unexpected format '");
catch (std::exception& e) {
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
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;
if (!extract_value(parent, key, exist, loaded, itr)) {
return false;
}
bool* loaded = nullptr;
if (!extract_value(parent, key, exist, loaded, itr)) {
return false;
}
const jsonval& a = itr->value;
const jsonval& a = itr->value;
if (a.IsString()) {
value = StrToDate(a.GetString());
if (a.IsString()) {
value = StrToDate(a.GetString());
}
else {
throw Exp() << "unexpected type";
}
}
else {
throw (Exp() << "element '" << key << "'" << " has unexpected format '");
catch (std::exception& e) {
throw Exp() << "failed to load key '" << key << "'" << " - " << trim(e.what()) << "\n";
}
return true;
......@@ -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)
{
std::map<std::string, std::string>::const_iterator itr;
try {
std::map<std::string, std::string>::const_iterator itr;
bool* loaded = nullptr;
if (!extract_value(parent, key, exist, loaded, itr)) {
return false;
}
bool* loaded = nullptr;
if (!extract_value(parent, key, exist, loaded, itr)) {
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;
}
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)) {
return false;
}
if (!extract_value(parent, key, exist, loaded, itr)) {
return false;
}
const std::string& str = itr->second;
const std::string& str = itr->second;
if (str.empty() && !exist) {
if (loaded) {
*loaded = false;
if (str.empty() && !exist) {
if (loaded) {
*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;
}
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