Commit da459ff0 authored by Alexander Lapshin's avatar Alexander Lapshin

added try catch blocks to json loading functions

parent 831edfb3
......@@ -18,7 +18,7 @@ typedef rapidjson::Document::AllocatorType jsonalloc;
typedef rapidjson::Value jsonval;
typedef rapidjson::Document jsondoc;
inline bool extract_value(const jsonval& parent, const std::string& key, bool exist, bool* loaded, jsonval::ConstMemberIterator& result)
inline bool extract_value(const jsonval& parent, const std::string& key, const bool exist, bool* loaded, jsonval::ConstMemberIterator& result)
{
const jsonval::ConstMemberIterator itr = parent.FindMember(key.c_str());
......@@ -55,7 +55,7 @@ inline bool extract_value(const jsonval& parent, const std::string& key, bool ex
return true;
}
inline bool extract_value(const std::map<std::string, std::string>& parent, const std::string& key, bool exist, bool* loaded, std::map<std::string, std::string>::const_iterator& result)
inline bool extract_value(const std::map<std::string, std::string>& parent, const std::string& key, const bool exist, bool* loaded, std::map<std::string, std::string>::const_iterator& result)
{
const auto itr = parent.find(key);
......@@ -81,6 +81,7 @@ inline bool extract_value(const std::map<std::string, std::string>& parent, cons
static bool loadjson(const jsonval& parent, const std::string& key, std::vector<std::string>& vec, const bool exist = true, bool* loaded = nullptr)
{
try {
jsonval::ConstMemberIterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
return false;
......@@ -89,7 +90,7 @@ static bool loadjson(const jsonval& parent, const std::string& key, std::vector<
const jsonval& a = itr->value;
if (!a.IsArray()) {
throw Exp() << "error: element is not an array";
throw Exp() << "element is not an array";
}
for (jsonval::ConstValueIterator it = a.Begin(); it != a.End(); ++it) {
......@@ -113,15 +114,20 @@ static bool loadjson(const jsonval& parent, const std::string& key, std::vector<
vec.push_back(ss.str());
}
else {
throw Exp() << "error: unsupported type";
throw Exp() << "unsupported type '" << it->GetType() << "'";
}
}
}
catch (std::exception& e) {
throw Exp() << "failed to load key '" << key << "'" << " - " << trim(e.what()) << "\n";
}
return true;
}
static bool loadjson(const jsonval& parent, const std::string& key, std::vector<size_t>& vec, const bool exist = true, bool* loaded = nullptr)
{
try {
jsonval::ConstMemberIterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
return false;
......@@ -130,7 +136,7 @@ static bool loadjson(const jsonval& parent, const std::string& key, std::vector<
const jsonval& a = itr->value;
if (!a.IsArray()) {
throw Exp() << "error: element is not an array";
throw Exp() << "element is not an array";
}
for (jsonval::ConstValueIterator it = a.Begin(); it != a.End(); ++it) {
......@@ -138,8 +144,12 @@ static bool loadjson(const jsonval& parent, const std::string& key, std::vector<
vec.push_back(it->GetInt());
}
else {
throw Exp() << "error: unsupported type";
throw Exp() << "unsupported type '" << it->GetType() << "'";
}
}
}
catch (std::exception& e) {
throw Exp() << "failed to load key '" << key << "'" << " - " << trim(e.what()) << "\n";
}
return true;
......@@ -147,6 +157,7 @@ static bool loadjson(const jsonval& parent, const std::string& key, std::vector<
static bool loadjson(const jsonval& parent, const std::string& key, std::vector<int>& vec, const bool exist = true, bool* loaded = nullptr)
{
try {
jsonval::ConstMemberIterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
return false;
......@@ -155,7 +166,7 @@ static bool loadjson(const jsonval& parent, const std::string& key, std::vector<
const jsonval& a = itr->value;
if (!a.IsArray()) {
throw Exp() << "error: element is not an array";
throw Exp() << "element is not an array";
}
for (jsonval::ConstValueIterator it = a.Begin(); it != a.End(); ++it) {
......@@ -163,15 +174,20 @@ static bool loadjson(const jsonval& parent, const std::string& key, std::vector<
vec.push_back(it->GetInt());
}
else {
throw Exp() << "error: unsupported type";
throw Exp() << "unsupported type '" << it->GetType() << "'";
}
}
}
catch (std::exception& e) {
throw Exp() << "failed to load key '" << key << "'" << " - " << trim(e.what()) << "\n";
}
return true;
}
static bool loadjson(const jsonval& parent, const std::string& key, std::vector<double>& vec, const bool exist = true, bool* loaded = nullptr)
{
try {
jsonval::ConstMemberIterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
return false;
......@@ -180,7 +196,7 @@ static bool loadjson(const jsonval& parent, const std::string& key, std::vector<
const jsonval& a = itr->value;
if (!a.IsArray()) {
throw Exp() << "error: element is not an array";
throw Exp() << "element is not an array";
}
for (jsonval::ConstValueIterator it = a.Begin(); it != a.End(); ++it) {
......@@ -191,15 +207,20 @@ static bool loadjson(const jsonval& parent, const std::string& key, std::vector<
vec.push_back(it->GetInt());
}
else {
throw Exp() << "error: unsupported type";
throw Exp() << "unsupported type '" << it->GetType() << "'";
}
}
}
catch (std::exception& e) {
throw Exp() << "failed to load key '" << key << "'" << " - " << trim(e.what()) << "\n";
}
return true;
}
template <typename T> bool loadjson(const jsonval& parent, std::string key, std::vector<T>& vec, const bool exist = true, bool* loaded = nullptr)
template <typename T> bool loadjson(const jsonval& parent, const std::string& key, std::vector<T>& vec, const bool exist = true, bool* loaded = nullptr)
{
try {
jsonval::ConstMemberIterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
return false;
......@@ -208,7 +229,7 @@ template <typename T> bool loadjson(const jsonval& parent, std::string key, std:
const jsonval& a = itr->value;
if (!a.IsArray()) {
throw Exp() << "error: element is not an array";
throw Exp() << "element is not an array";
}
for (rapidjson::SizeType i = 0; i < a.Size(); i++) {
......@@ -217,11 +238,17 @@ template <typename T> bool loadjson(const jsonval& parent, std::string key, std:
vec.emplace_back(item);
}
}
catch (std::exception& e) {
throw Exp() << "failed to load key '" << key << "'" << " - " << trim(e.what()) << "\n";
}
return true;
}
template <typename T, typename Tid> bool loadjson(const jsonval& parent, const std::string key, std::map<Tid, T>& map, const bool exist = true, bool* loaded = nullptr)
{
try {
jsonval::ConstMemberIterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
return false;
......@@ -230,7 +257,7 @@ template <typename T, typename Tid> bool loadjson(const jsonval& parent, const s
const jsonval& a = itr->value;
if (!a.IsArray()) {
throw Exp() << "error: element is not an array";
throw Exp() << "element is not an array";
}
for (rapidjson::SizeType i = 0; i < a.Size(); i++) {
......@@ -238,12 +265,17 @@ template <typename T, typename Tid> bool loadjson(const jsonval& parent, const s
item.load(a[i]);
map[item.getId()] = item;
}
}
catch (std::exception& e) {
throw Exp() << "failed to load key '" << key << "'" << " - " << trim(e.what()) << "\n";
}
return true;
}
template <typename T> bool loadjson(const jsonval& parent, std::string key, T& item, void (*f)(const jsonval&, T&), const bool exist = true, bool* loaded = nullptr)
template <typename T> bool loadjson(const jsonval& parent, const std::string& key, T& item, void (*f)(const jsonval&, T&), const bool exist = true, bool* loaded = nullptr)
{
try {
jsonval::ConstMemberIterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
return false;
......@@ -252,16 +284,21 @@ template <typename T> bool loadjson(const jsonval& parent, std::string key, T& i
const jsonval& a = itr->value;
if (!a.IsObject()) {
throw Exp() << "error: element is not an object";
throw Exp() << "element is not an object";
}
f(a, item);
}
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, svalue<std::string>& value, const bool exist = true)
{
try {
std::map<std::string, std::string>::const_iterator itr;
bool* loaded = nullptr;
......@@ -270,12 +307,17 @@ static bool loadjson(const std::map<std::string, std::string>& parent, const std
}
value.set(itr->second);
}
catch (std::exception& e) {
throw Exp() << "failed to load key '" << key << "'" << " - " << trim(e.what()) << "\n";
}
return true;
}
static bool loadjson(const jsonval& parent, const std::string& key, svalue<std::string>& value, const bool exist = true)
{
try {
jsonval::ConstMemberIterator itr;
bool* loaded = nullptr;
......@@ -284,16 +326,21 @@ static bool loadjson(const jsonval& parent, const std::string& key, svalue<std::
}
if (!itr->value.IsString()) {
throw (Exp() << "element '" << key << "'" << " has unexpected format '");
throw Exp() << "unexpected type";
}
value.set(itr->value.GetString());
}
catch (std::exception& e) {
throw Exp() << "failed to load key '" << key << "'" << " - " << trim(e.what()) << "\n";
}
return true;
}
template <typename T> static bool loadjson(const jsonval& parent, const std::string& key, svalue<T>& value, const bool exist = true)
{
try {
jsonval::ConstMemberIterator itr;
bool* loaded = nullptr;
......@@ -304,18 +351,23 @@ template <typename T> static bool loadjson(const jsonval& parent, const std::str
const jsonval& a = itr->value;
if (!a.IsObject()) {
throw Exp() << "error: element is not an object";
throw Exp() << "element is not an object";
}
T val;
val.load(itr->value);
value = val;
}
catch (std::exception& e) {
throw Exp() << "failed to load key '" << key << "'" << " - " << trim(e.what()) << "\n";
}
return true;
}
template <typename T> bool loadjson(const jsonval& parent, const std::string& key, T& item, const bool exist = true, bool* loaded = nullptr)
{
try {
jsonval::ConstMemberIterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
return false;
......@@ -324,10 +376,14 @@ template <typename T> bool loadjson(const jsonval& parent, const std::string& ke
const jsonval& a = itr->value;
if (!a.IsObject()) {
throw Exp() << "error: element is not an object";
throw Exp() << "element is not an object";
}
item.load(a);
}
catch (std::exception& e) {
throw Exp() << "failed to load key '" << key << "'" << " - " << trim(e.what()) << "\n";
}
return true;
}
......@@ -349,7 +405,7 @@ template <typename T> void loadjson(const jsonval& parent, std::vector<T>& vec)
}
}
else {
throw Exp() << "error: element is not an array";
throw Exp() << "element is not an array";
}
}
......@@ -370,12 +426,13 @@ template <typename T, typename T2> void loadjson(const jsonval& parent, std::map
}
}
else {
throw Exp() << "error: element is not an array";
throw Exp() << "element is not an array";
}
}
static bool loadjson(const jsonval& parent, const std::string& key, double& value, const bool exist = true, bool* loaded = nullptr)
{
try {
jsonval::ConstMemberIterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
return false;
......@@ -397,11 +454,15 @@ static bool loadjson(const jsonval& parent, const std::string& key, double& valu
}
if (!Converter::ToNumber(str, value)) {
throw (Exp() << "bad value format '" << str << "'" << " in container '");
throw Exp() << "bad value format '" << str << "'";
}
}
else {
throw (Exp() << "element '" << key << "'" << " has unexpected format '");
throw Exp() << "unexpected type";
}
}
catch (std::exception& e) {
throw Exp() << "failed to load key '" << key << "'" << " - " << trim(e.what()) << "\n";
}
return true;
......@@ -409,6 +470,7 @@ static bool loadjson(const jsonval& parent, const std::string& key, double& valu
static bool loadjson(const jsonval& parent, const std::string& key, float& value, const bool exist = true, bool* loaded = nullptr)
{
try {
jsonval::ConstMemberIterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
return false;
......@@ -430,11 +492,15 @@ static bool loadjson(const jsonval& parent, const std::string& key, float& value
}
if (!Converter::ToNumber(str, value)) {
throw (Exp() << "bad value format '" << str << "'" << " in container '");
throw Exp() << "bad value format '" << str << "'";
}
}
else {
throw (Exp() << "element '" << key << "'" << " has unexpected format '");
throw Exp() << "unexpected type";
}
}
catch (std::exception& e) {
throw Exp() << "failed to load key '" << key << "'" << " - " << trim(e.what()) << "\n";
}
return true;
......@@ -442,6 +508,7 @@ static bool loadjson(const jsonval& parent, const std::string& key, float& value
static bool loadjson(const jsonval& parent, const std::string& key, int& value, const bool exist = true, bool* loaded = nullptr)
{
try {
jsonval::ConstMemberIterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
return false;
......@@ -454,7 +521,7 @@ static bool loadjson(const jsonval& parent, const std::string& key, int& value,
}
else if (a.IsString()) {
std::string str = a.GetString();
const std::string str = a.GetString();
if (str.empty() && !exist) {
if (loaded) {
*loaded = false;
......@@ -463,11 +530,15 @@ static bool loadjson(const jsonval& parent, const std::string& key, int& value,
}
if (!Converter::ToNumber(str, value)) {
throw (Exp() << "bad value format '" << str << "'" << " in container '");
throw Exp() << "bad value format '" << str << "'";
}
}
else {
throw (Exp() << "element '" << key << "'" << " has unexpected format '");
throw Exp() << "unexpected type";
}
}
catch (std::exception& e) {
throw Exp() << "failed to load key '" << key << "'" << " - " << trim(e.what()) << "\n";
}
return true;
......@@ -475,6 +546,7 @@ static bool loadjson(const jsonval& parent, const std::string& key, int& value,
static bool loadjson(const jsonval& parent, const std::string& key, short& value, const bool exist = true, bool* loaded = nullptr)
{
try {
jsonval::ConstMemberIterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
return false;
......@@ -487,7 +559,7 @@ static bool loadjson(const jsonval& parent, const std::string& key, short& value
}
else if (a.IsString()) {
std::string str = a.GetString();
const std::string str = a.GetString();
if (str.empty() && !exist) {
if (loaded) {
*loaded = false;
......@@ -496,11 +568,15 @@ static bool loadjson(const jsonval& parent, const std::string& key, short& value
}
if (!Converter::ToNumber(str, value)) {
throw (Exp() << "bad value format '" << str << "'" << " in container '");
throw Exp() << "bad value format '" << str << "'";
}
}
else {
throw (Exp() << "element '" << key << "'" << " has unexpected format '");
throw Exp() << "unexpected type";
}
}
catch (std::exception& e) {
throw Exp() << "failed to load key '" << key << "'" << " - " << trim(e.what()) << "\n";
}
return true;
......@@ -508,6 +584,7 @@ static bool loadjson(const jsonval& parent, const std::string& key, short& value
static bool loadjson(const jsonval& parent, const std::string& key, size_t& value, const bool exist = true, bool* loaded = nullptr)
{
try {
jsonval::ConstMemberIterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
return false;
......@@ -520,7 +597,7 @@ static bool loadjson(const jsonval& parent, const std::string& key, size_t& valu
}
else if (a.IsString()) {
std::string str = a.GetString();
const std::string str = a.GetString();
if (str.empty() && !exist) {
if (loaded) {
*loaded = false;
......@@ -529,11 +606,15 @@ static bool loadjson(const jsonval& parent, const std::string& key, size_t& valu
}
if (!Converter::ToNumber(str, value)) {
throw (Exp() << "bad value format '" << str << "'" << " in container '");
throw Exp() << "bad value format '" << str << "'";
}
}
else {
throw (Exp() << "element '" << key << "'" << " has unexpected format '");
throw Exp() << "unexpected type";
}
}
catch (std::exception& e) {
throw Exp() << "failed to load key '" << key << "'" << " - " << trim(e.what()) << "\n";
}
return true;
......@@ -541,6 +622,7 @@ static bool loadjson(const jsonval& parent, const std::string& key, size_t& valu
static bool loadjson(const jsonval& parent, const std::string& key, bool& value, const bool exist = true, bool* loaded = nullptr)
{
try {
jsonval::ConstMemberIterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
return false;
......@@ -565,11 +647,15 @@ static bool loadjson(const jsonval& parent, const std::string& key, bool& value,
}
if (!Converter::ToNumber(str, value)) {
throw (Exp() << "bad value format '" << str << "'" << " in container '");
throw Exp() << "bad value format '" << str << "'";
}
}
else {
throw (Exp() << "element '" << key << "'" << " has unexpected format '");
throw Exp() << "unexpected type";
}
}
catch (std::exception& e) {
throw Exp() << "failed to load key '" << key << "'" << " - " << trim(e.what()) << "\n";
}
return true;
......@@ -577,6 +663,7 @@ static bool loadjson(const jsonval& parent, const std::string& key, bool& value,
static bool loadjson(const jsonval& parent, const std::string& key, std::string& value, const bool exist = true, bool* loaded = nullptr)
{
try {
jsonval::ConstMemberIterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
return false;
......@@ -593,7 +680,11 @@ static bool loadjson(const jsonval& parent, const std::string& key, std::string&
value = stream.str();
}
else {
throw (Exp() << "element '" << key << "'" << " has unexpected format '");
throw Exp() << "unexpected type";
}
}
catch (std::exception& e) {
throw Exp() << "failed to load key '" << key << "'" << " - " << trim(e.what()) << "\n";
}
return true;
......@@ -655,10 +746,10 @@ template <typename T, typename TK> void PutValue(jsonalloc& alc, jsonval& ss, co
ss.AddMember(jname, jsvec, alc);
}
template <typename T, typename TK> void PutMapAsVec(jsonalloc& alc, jsonval& ss, const std::string conName, const std::map<TK, T>& vec, bool (*f)(rapidjson::Document::AllocatorType&, jsonval&, const T&))
template <typename T, typename TK> void PutMapAsVec(jsonalloc& alc, jsonval& ss, const std::string& conName, const std::map<TK, T>& vec, bool (*f)(rapidjson::Document::AllocatorType&, jsonval&, const T&))
{
jsonval jsvec(rapidjson::kArrayType);
for (typename std::map<TK, T>::const_iterator it = vec.begin(); it != vec.end(); ++it) {
for (auto& it = vec.begin(); it != vec.end(); ++it) {
jsonval child(rapidjson::kObjectType);
if (f(alc, child, it->second)) {
jsvec.PushBack(child, alc);
......@@ -705,7 +796,7 @@ inline void PutValue(jsonalloc& alc, jsonval& ss, const std::string& conName, co
ss.AddMember(jname, jsvec, alc);
}
template <typename T> void PutValue(jsonalloc& alc, jsonval& ss, const std::string conName, const std::vector<T>& vec)
template <typename T> void PutValue(jsonalloc& alc, jsonval& ss, const std::string& conName, const std::vector<T>& vec)
{
jsonval jsvec(rapidjson::kArrayType);
for (typename std::vector<T>::const_iterator it = vec.begin(); it != vec.end(); ++it) {
......@@ -718,7 +809,7 @@ template <typename T> void PutValue(jsonalloc& alc, jsonval& ss, const std::stri
ss.AddMember(jname, jsvec, alc);
}
template <typename T> void PutValue(jsonalloc& alc, jsonval& ss, const std::string conName, const T& object)
template <typename T> void PutValue(jsonalloc& alc, jsonval& ss, const std::string& conName, const T& object)
{
jsonval obj(rapidjson::kObjectType);
PutValue(alc, obj, object);
......@@ -856,6 +947,7 @@ static void loadStrJson(const std::string& data, rapidjson::Document& document)
static bool loadjson(const jsonval& parent, const std::string& key, svalue<double>& value, const bool exist = true)
{
try {
jsonval::ConstMemberIterator itr;
bool* loaded = nullptr;
......@@ -883,13 +975,17 @@ static bool loadjson(const jsonval& parent, const std::string& key, svalue<doubl
double val;
if (!Converter::ToNumber(str, val)) {
throw (Exp() << "bad value format '" << str << "'" << " in container '");
throw Exp() << "bad value format '" << str << "'";
}
value = val;
}
else {
throw (Exp() << "element '" << key << "'" << " has unexpected format '");
throw Exp() << "unexpected type";
}
}
catch (std::exception& e) {
throw Exp() << "failed to load key '" << key << "'" << " - " << trim(e.what()) << "\n";
}
return true;
......@@ -897,6 +993,7 @@ static bool loadjson(const jsonval& parent, const std::string& key, svalue<doubl
static bool loadjson(const jsonval& parent, const std::string& key, svalue<int>& value, const bool exist = true)
{
try {
jsonval::ConstMemberIterator itr;
bool* loaded = nullptr;
......@@ -921,13 +1018,17 @@ static bool loadjson(const jsonval& parent, const std::string& key, svalue<int>&
int val;
if (!Converter::ToNumber(str, val)) {
throw (Exp() << "bad value format '" << str << "'" << " in container '");
throw Exp() << "bad value format '" << str << "'";
}
value = val;
}
else {
throw (Exp() << "element '" << key << "'" << " has unexpected format '");
throw Exp() << "unexpected type";
}
}
catch (std::exception& e) {
throw Exp() << "failed to load key '" << key << "'" << " - " << trim(e.what()) << "\n";
}
return true;
......@@ -935,6 +1036,7 @@ static bool loadjson(const jsonval& parent, const std::string& key, svalue<int>&
static bool loadjson(const std::map<std::string, std::string>& parent, const std::string& key, double& value, const bool exist = true, bool* loaded = nullptr)
{
try {
std::map<std::string, std::string>::const_iterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
......@@ -951,7 +1053,11 @@ static bool loadjson(const std::map<std::string, std::string>& parent, const std
}
if (!Converter::ToNumber(str, value)) {
throw (Exp() << "bad value format '" << str << "'" << " in container '");
throw Exp() << "bad value format '" << str << "'";
}
}
catch (std::exception& e) {
throw Exp() << "failed to load key '" << key << "'" << " - " << trim(e.what()) << "\n";
}
return true;
......@@ -959,6 +1065,7 @@ static bool loadjson(const std::map<std::string, std::string>& parent, const std
static bool loadjson(const std::map<std::string, std::string>& parent, const std::string& key, int& value, const bool exist = true, bool* loaded = nullptr)
{
try {
std::map<std::string, std::string>::const_iterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
......@@ -975,7 +1082,11 @@ static bool loadjson(const std::map<std::string, std::string>& parent, const std
}
if (!Converter::ToNumber(str, value)) {
throw (Exp() << "bad value format '" << str << "'" << " in container '");
throw Exp() << "bad value format '" << str << "'";
}
}
catch (std::exception& e) {
throw Exp() << "failed to load key '" << key << "'" << " - " << trim(e.what()) << "\n";
}
return true;
......@@ -983,6 +1094,7 @@ static bool loadjson(const std::map<std::string, std::string>& parent, const std
static bool loadjson(const std::map<std::string, std::string>& parent, const std::string& key, long long& value, const bool exist = true, bool* loaded = nullptr)
{
try {
std::map<std::string, std::string>::const_iterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
......@@ -999,7 +1111,11 @@ static bool loadjson(const std::map<std::string, std::string>& parent, const std
}
if (!Converter::ToNumber(str, value)) {
throw (Exp() << "bad value format '" << str << "'" << " in container '");
throw Exp() << "bad value format '" << str << "'";
}
}
catch (std::exception& e) {
throw Exp() << "failed to load key '" << key << "'" << " - " << trim(e.what()) << "\n";
}
return true;
......@@ -1007,6 +1123,7 @@ static bool loadjson(const std::map<std::string, std::string>& parent, const std
static bool loadjson(const std::map<std::string, std::string>& parent, const std::string& key, bool& value, const bool exist = true, bool* loaded = nullptr)
{
try {
std::map<std::string, std::string>::const_iterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
......@@ -1023,7 +1140,11 @@ static bool loadjson(const std::map<std::string, std::string>& parent, const std
}
if (!Converter::ToNumber(str, value)) {
throw (Exp() << "bad value format '" << str << "'" << " in container '");
throw Exp() << "bad value format '" << str << "'";
}
}
catch (std::exception& e) {
throw Exp() << "failed to load key '" << key << "'" << " - " << trim(e.what()) << "\n";
}
return true;
......@@ -1031,6 +1152,7 @@ static bool loadjson(const std::map<std::string, std::string>& parent, const std
static bool loadjson(const std::map<std::string, std::string>& parent, const std::string& key, std::string& value, const bool exist = true, bool* loaded = nullptr)
{
try {
std::map<std::string, std::string>::const_iterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
......@@ -1038,12 +1160,17 @@ static bool loadjson(const std::map<std::string, std::string>& parent, const std
}
value = 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, svalue<double>& value, const bool exist = true)
{
try {
std::map<std::string, std::string>::const_iterator itr;
bool* loaded = nullptr;
......@@ -1062,16 +1189,21 @@ static bool loadjson(const std::map<std::string, std::string>& parent, const std
double val;
if (!Converter::ToNumber(str, val)) {
throw (Exp() << "bad value format '" << str << "'" << " in container '");
throw Exp() << "bad value format '" << str << "'";
}
value = val;
}
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, svalue<int>& value, const bool exist = true)
{
try {
std::map<std::string, std::string>::const_iterator itr;
bool* loaded = nullptr;
......@@ -1090,10 +1222,14 @@ static bool loadjson(const std::map<std::string, std::string>& parent, const std
int val;
if (!Converter::ToNumber(str, val)) {
throw (Exp() << "bad value format '" << str << "'" << " in container '");
throw Exp() << "bad value format '" << str << "'";
}
value = val;
}
catch (std::exception& e) {
throw Exp() << "failed to load key '" << key << "'" << " - " << trim(e.what()) << "\n";
}
return true;
}
......@@ -6,6 +6,7 @@
static bool loadjson(const jsonval& parent, const std::string& key, std::vector<TimeJD>& vec, const bool exist = true, bool* loaded = nullptr)
{
try {
jsonval::ConstMemberIterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
return false;
......@@ -14,7 +15,7 @@ static bool loadjson(const jsonval& parent, const std::string& key, std::vector<
const jsonval& a = itr->value;
if (!a.IsArray()) {
throw Exp() << "error: element is not an array";
throw Exp() << "element is not an array";
}
for (jsonval::ConstValueIterator it = a.Begin(); it != a.End(); ++it) {
......@@ -22,15 +23,20 @@ static bool loadjson(const jsonval& parent, const std::string& key, std::vector<
vec.emplace_back(StrToDate(it->GetString()));
}
else {
throw Exp() << "error: unsupported type";
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)
{
try {
const jsonval::ConstMemberIterator itr = parent.FindMember(key.c_str());
if (itr == parent.MemberEnd() && exist) {
......@@ -54,16 +60,21 @@ static void loadjson(const jsonval& parent, const std::string& key, TimeJD& valu
}
}
else {
throw (Exp() << "element '" << key << "'" << " has unexpected format '");
throw Exp() << "unexpected type";
}
}
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)
{
try {
const jsonval::ConstMemberIterator itr = parent.FindMember(key.c_str());
if (itr == parent.MemberEnd() && exist) {
throw Exp() << "error: element not found";
throw Exp() << "element not found";
}
const jsonval& a = itr->value;
......@@ -72,7 +83,11 @@ static void loadjson(const jsonval& parent, const std::string& key, TimeGD& valu
StrToDate(a.GetString(), value);
}
else {
throw (Exp() << "element '" << key << "'" << " has unexpected format '");
throw Exp() << "unexpected type";
}
}
catch (std::exception& e) {
throw Exp() << "failed to load key '" << key << "'" << " - " << trim(e.what()) << "\n";
}
}
......@@ -93,6 +108,7 @@ 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)
{
try {
jsonval::ConstMemberIterator itr;
bool* loaded = nullptr;
......@@ -106,7 +122,11 @@ static bool loadjson(const jsonval& parent, const std::string& key, svalue<TimeJ
value = StrToDate(a.GetString());
}
else {
throw (Exp() << "element '" << key << "'" << " has unexpected format '");
throw Exp() << "unexpected type";
}
}
catch (std::exception& e) {
throw Exp() << "failed to load key '" << key << "'" << " - " << trim(e.what()) << "\n";
}
return true;
......@@ -114,6 +134,7 @@ 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)
{
try {
std::map<std::string, std::string>::const_iterator itr;
bool* loaded = nullptr;
......@@ -122,12 +143,17 @@ static bool loadjson(const std::map<std::string, std::string>& parent, const std
}
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)
{
try {
std::map<std::string, std::string>::const_iterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
......@@ -144,6 +170,10 @@ static bool loadjson(const std::map<std::string, std::string>& parent, const std
}
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