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,140 +81,166 @@ 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)
{
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()) {
std::string str = it->GetString();
vec.push_back(str);
}
else if (it->IsInt()) {
std::ostringstream ss;
ss << it->GetInt();
vec.push_back(ss.str());
}
else if (it->IsBool()) {
std::ostringstream ss;
ss << it->GetBool();
vec.push_back(ss.str());
if (!a.IsArray()) {
throw Exp() << "element is not an array";
}
else if (it->IsDouble()) {
std::ostringstream ss;
ss << it->GetDouble();
vec.push_back(ss.str());
}
else {
throw Exp() << "error: unsupported type";
for (jsonval::ConstValueIterator it = a.Begin(); it != a.End(); ++it) {
if (it->IsString()) {
std::string str = it->GetString();
vec.push_back(str);
}
else if (it->IsInt()) {
std::ostringstream ss;
ss << it->GetInt();
vec.push_back(ss.str());
}
else if (it->IsBool()) {
std::ostringstream ss;
ss << it->GetBool();
vec.push_back(ss.str());
}
else if (it->IsDouble()) {
std::ostringstream ss;
ss << it->GetDouble();
vec.push_back(ss.str());
}
else {
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)
{
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->IsInt()) {
vec.push_back(it->GetInt());
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->IsInt()) {
vec.push_back(it->GetInt());
}
else {
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<int>& 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->IsInt()) {
vec.push_back(it->GetInt());
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->IsInt()) {
vec.push_back(it->GetInt());
}
else {
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)
{
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->IsDouble()) {
vec.push_back(it->GetDouble());
}
else if (it->IsInt()) {
vec.push_back(it->GetInt());
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->IsDouble()) {
vec.push_back(it->GetDouble());
}
else if (it->IsInt()) {
vec.push_back(it->GetInt());
}
else {
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)
{
jsonval::ConstMemberIterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
return false;
}
try {
jsonval::ConstMemberIterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
return false;
}
const jsonval& a = itr->value;
const jsonval& a = itr->value;
if (!a.IsArray()) {
throw Exp() << "error: element is not an array";
}
if (!a.IsArray()) {
throw Exp() << "element is not an array";
}
for (rapidjson::SizeType i = 0; i < a.Size(); i++) {
T item;
item.load(a[i]);
vec.emplace_back(item);
}
for (rapidjson::SizeType i = 0; i < a.Size(); i++) {
T item;
item.load(a[i]);
vec.emplace_back(item);
}
catch (std::exception& e) {
throw Exp() << "failed to load key '" << key << "'" << " - " << trim(e.what()) << "\n";
}
return true;
......@@ -222,112 +248,142 @@ template <typename T> bool loadjson(const jsonval& parent, std::string key, std:
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)
{
jsonval::ConstMemberIterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
return false;
}
try {
jsonval::ConstMemberIterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
return false;
}
const jsonval& a = itr->value;
const jsonval& a = itr->value;
if (!a.IsArray()) {
throw Exp() << "error: element is not an array";
}
if (!a.IsArray()) {
throw Exp() << "element is not an array";
}
for (rapidjson::SizeType i = 0; i < a.Size(); i++) {
T item;
item.load(a[i]);
map[item.getId()] = item;
for (rapidjson::SizeType i = 0; i < a.Size(); i++) {
T item;
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)
{
jsonval::ConstMemberIterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
return false;
}
try {
jsonval::ConstMemberIterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
return false;
}
const jsonval& a = itr->value;
const jsonval& a = itr->value;
if (!a.IsObject()) {
throw Exp() << "error: element is not an object";
}
if (!a.IsObject()) {
throw Exp() << "element is not an object";
}
f(a, item);
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)
{
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.set(itr->second);
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)
{
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;
}
if (!itr->value.IsString()) {
throw (Exp() << "element '" << key << "'" << " has unexpected format '");
}
if (!itr->value.IsString()) {
throw Exp() << "unexpected type";
}
value.set(itr->value.GetString());
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)
{
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.IsObject()) {
throw Exp() << "error: element is not an object";
}
if (!a.IsObject()) {
throw Exp() << "element is not an object";
}
T val;
val.load(itr->value);
value = val;
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)
{
jsonval::ConstMemberIterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
return false;
}
try {
jsonval::ConstMemberIterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
return false;
}
const jsonval& a = itr->value;
const jsonval& a = itr->value;
if (!a.IsObject()) {
throw Exp() << "error: element is not an object";
}
if (!a.IsObject()) {
throw Exp() << "element is not an object";
}
item.load(a);
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,38 +426,43 @@ 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)
{
jsonval::ConstMemberIterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
return false;
}
try {
jsonval::ConstMemberIterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
return false;
}
const jsonval& a = itr->value;
const jsonval& a = itr->value;
if (a.IsNumber()) {
value = a.GetDouble();
}
else if (a.IsString()) {
if (a.IsNumber()) {
value = a.GetDouble();
}
else if (a.IsString()) {
const std::string str = a.GetString();
if (str.empty() && !exist) {
if (loaded) {
*loaded = false;
}
return false;
}
const std::string str = a.GetString();
if (str.empty() && !exist) {
if (loaded) {
*loaded = false;
if (!Converter::ToNumber(str, value)) {
throw Exp() << "bad value format '" << str << "'";
}
return false;
}
if (!Converter::ToNumber(str, value)) {
throw (Exp() << "bad value format '" << str << "'" << " in container '");
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;
......@@ -409,32 +470,37 @@ 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)
{
jsonval::ConstMemberIterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
return false;
}
try {
jsonval::ConstMemberIterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
return false;
}
const jsonval& a = itr->value;
const jsonval& a = itr->value;
if (a.IsNumber()) {
value = a.GetDouble();
}
else if (a.IsString()) {
if (a.IsNumber()) {
value = a.GetDouble();
}
else if (a.IsString()) {
const std::string str = a.GetString();
if (str.empty() && !exist) {
if (loaded) {
*loaded = false;
}
return false;
}
const std::string str = a.GetString();
if (str.empty() && !exist) {
if (loaded) {
*loaded = false;
if (!Converter::ToNumber(str, value)) {
throw Exp() << "bad value format '" << str << "'";
}
return false;
}
if (!Converter::ToNumber(str, value)) {
throw (Exp() << "bad value format '" << str << "'" << " in container '");
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;
......@@ -442,32 +508,37 @@ 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)
{
jsonval::ConstMemberIterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
return false;
}
try {
jsonval::ConstMemberIterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
return false;
}
const jsonval& a = itr->value;
const jsonval& a = itr->value;
if (a.IsNumber()) {
value = a.GetInt();
}
else if (a.IsString()) {
if (a.IsNumber()) {
value = a.GetInt();
}
else if (a.IsString()) {
const std::string str = a.GetString();
if (str.empty() && !exist) {
if (loaded) {
*loaded = false;
}
return false;
}
std::string str = a.GetString();
if (str.empty() && !exist) {
if (loaded) {
*loaded = false;
if (!Converter::ToNumber(str, value)) {
throw Exp() << "bad value format '" << str << "'";
}
return false;
}
if (!Converter::ToNumber(str, value)) {
throw (Exp() << "bad value format '" << str << "'" << " in container '");
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;
......@@ -475,32 +546,37 @@ 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)
{
jsonval::ConstMemberIterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
return false;
}
try {
jsonval::ConstMemberIterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
return false;
}
const jsonval& a = itr->value;
const jsonval& a = itr->value;
if (a.IsNumber()) {
value = a.GetInt();
}
else if (a.IsString()) {
if (a.IsNumber()) {
value = a.GetInt();
}
else if (a.IsString()) {
const std::string str = a.GetString();
if (str.empty() && !exist) {
if (loaded) {
*loaded = false;
}
return false;
}
std::string str = a.GetString();
if (str.empty() && !exist) {
if (loaded) {
*loaded = false;
if (!Converter::ToNumber(str, value)) {
throw Exp() << "bad value format '" << str << "'";
}
return false;
}
if (!Converter::ToNumber(str, value)) {
throw (Exp() << "bad value format '" << str << "'" << " in container '");
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;
......@@ -508,32 +584,37 @@ 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)
{
jsonval::ConstMemberIterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
return false;
}
try {
jsonval::ConstMemberIterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
return false;
}
const jsonval& a = itr->value;
const jsonval& a = itr->value;
if (a.IsNumber()) {
value = a.GetInt();
}
else if (a.IsString()) {
if (a.IsNumber()) {
value = a.GetInt();
}
else if (a.IsString()) {
const std::string str = a.GetString();
if (str.empty() && !exist) {
if (loaded) {
*loaded = false;
}
return false;
}
std::string str = a.GetString();
if (str.empty() && !exist) {
if (loaded) {
*loaded = false;
if (!Converter::ToNumber(str, value)) {
throw Exp() << "bad value format '" << str << "'";
}
return false;
}
if (!Converter::ToNumber(str, value)) {
throw (Exp() << "bad value format '" << str << "'" << " in container '");
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;
......@@ -541,35 +622,40 @@ 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)
{
jsonval::ConstMemberIterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
return false;
}
try {
jsonval::ConstMemberIterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
return false;
}
const jsonval& a = itr->value;
const jsonval& a = itr->value;
if (a.IsInt()) {
value = a.GetInt();
}
else if (a.IsBool()) {
value = a.GetBool();
}
else if (a.IsString()) {
if (a.IsInt()) {
value = a.GetInt();
}
else if (a.IsBool()) {
value = a.GetBool();
}
else if (a.IsString()) {
std::string str = a.GetString();
if (str.empty() && !exist) {
if (loaded) {
*loaded = false;
}
return false;
}
std::string str = a.GetString();
if (str.empty() && !exist) {
if (loaded) {
*loaded = false;
if (!Converter::ToNumber(str, value)) {
throw Exp() << "bad value format '" << str << "'";
}
return false;
}
if (!Converter::ToNumber(str, value)) {
throw (Exp() << "bad value format '" << str << "'" << " in container '");
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;
......@@ -577,23 +663,28 @@ 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)
{
jsonval::ConstMemberIterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
return false;
}
try {
jsonval::ConstMemberIterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
return false;
}
const jsonval& a = itr->value;
const jsonval& a = itr->value;
if (a.IsString()) {
value = a.GetString();
}
else if (a.IsInt()) {
std::stringstream stream;
stream << a.GetInt();
value = stream.str();
if (a.IsString()) {
value = a.GetString();
}
else if (a.IsInt()) {
std::stringstream stream;
stream << a.GetInt();
value = stream.str();
}
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;
......@@ -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,40 +947,45 @@ 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)
{
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.IsDouble()) {
value = a.GetDouble();
}
else if (a.IsInt()) {
value = static_cast<double>(a.GetInt());
}
else if (a.IsString()) {
if (a.IsDouble()) {
value = a.GetDouble();
}
else if (a.IsInt()) {
value = static_cast<double>(a.GetInt());
}
else if (a.IsString()) {
const std::string str = a.GetString();
if (str.empty() && !exist) {
if (loaded) {
*loaded = false;
}
return false;
}
const std::string str = a.GetString();
if (str.empty() && !exist) {
if (loaded) {
*loaded = false;
double val;
if (!Converter::ToNumber(str, val)) {
throw Exp() << "bad value format '" << str << "'";
}
return false;
}
double val;
if (!Converter::ToNumber(str, val)) {
throw (Exp() << "bad value format '" << str << "'" << " in container '");
value = val;
}
else {
throw Exp() << "unexpected type";
}
value = val;
}
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;
......@@ -897,37 +993,42 @@ 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)
{
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.IsInt()) {
value = a.GetInt();
}
else if (a.IsString()) {
if (a.IsInt()) {
value = a.GetInt();
}
else if (a.IsString()) {
const std::string str = a.GetString();
if (str.empty() && !exist) {
if (loaded) {
*loaded = false;
}
return false;
}
const std::string str = a.GetString();
if (str.empty() && !exist) {
if (loaded) {
*loaded = false;
int val;
if (!Converter::ToNumber(str, val)) {
throw Exp() << "bad value format '" << str << "'";
}
return false;
}
int val;
if (!Converter::ToNumber(str, val)) {
throw (Exp() << "bad value format '" << str << "'" << " in container '");
value = val;
}
else {
throw Exp() << "unexpected type";
}
value = val;
}
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;
......@@ -935,23 +1036,28 @@ 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)
{
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;
}
if (!Converter::ToNumber(str, value)) {
throw (Exp() << "bad value format '" << str << "'" << " in container '");
if (!Converter::ToNumber(str, value)) {
throw Exp() << "bad value format '" << str << "'";
}
}
catch (std::exception& e) {
throw Exp() << "failed to load key '" << key << "'" << " - " << trim(e.what()) << "\n";
}
return true;
......@@ -959,23 +1065,28 @@ 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)
{
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;
}
if (!Converter::ToNumber(str, value)) {
throw (Exp() << "bad value format '" << str << "'" << " in container '");
if (!Converter::ToNumber(str, value)) {
throw Exp() << "bad value format '" << str << "'";
}
}
catch (std::exception& e) {
throw Exp() << "failed to load key '" << key << "'" << " - " << trim(e.what()) << "\n";
}
return true;
......@@ -983,23 +1094,28 @@ 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)
{
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;
}
if (!Converter::ToNumber(str, value)) {
throw (Exp() << "bad value format '" << str << "'" << " in container '");
if (!Converter::ToNumber(str, value)) {
throw Exp() << "bad value format '" << str << "'";
}
}
catch (std::exception& e) {
throw Exp() << "failed to load key '" << key << "'" << " - " << trim(e.what()) << "\n";
}
return true;
......@@ -1007,23 +1123,28 @@ 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)
{
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;
}
if (!Converter::ToNumber(str, value)) {
throw (Exp() << "bad value format '" << str << "'" << " in container '");
if (!Converter::ToNumber(str, value)) {
throw Exp() << "bad value format '" << str << "'";
}
}
catch (std::exception& e) {
throw Exp() << "failed to load key '" << key << "'" << " - " << trim(e.what()) << "\n";
}
return true;
......@@ -1031,69 +1152,84 @@ 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)
{
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;
}
value = itr->second;
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)
{
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;
}
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;
}
double val;
if (!Converter::ToNumber(str, val)) {
throw (Exp() << "bad value format '" << str << "'" << " in container '");
}
double val;
if (!Converter::ToNumber(str, val)) {
throw Exp() << "bad value format '" << str << "'";
}
value = val;
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)
{
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;
}
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;
}
int val;
if (!Converter::ToNumber(str, val)) {
throw (Exp() << "bad value format '" << str << "'" << " in container '");
}
int val;
if (!Converter::ToNumber(str, val)) {
throw Exp() << "bad value format '" << str << "'";
}
value = val;
value = val;
}
catch (std::exception& e) {
throw Exp() << "failed to load key '" << key << "'" << " - " << trim(e.what()) << "\n";
}
return true;
}
......@@ -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