Commit 75d39329 authored by Alexander Lapshin's avatar Alexander Lapshin

new json loading helpers

parent da459ff0
......@@ -296,6 +296,44 @@ template <typename T> bool loadjson(const jsonval& parent, const std::string& ke
return true;
}
template <typename T> bool loadjson(const jsonval& parent, const std::string& key, T& item, void (*f)(const std::string&, T&), const bool exist = true, bool* loaded = nullptr)
{
try {
jsonval::ConstMemberIterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
return false;
}
if(!itr->value.IsString()) {
throw Exp() << "element is not an string";
}
f(itr->value.GetString(), item);
}
catch (std::exception& e) {
throw Exp() << "failed to load key '" << key << "'" << " - " << trim(e.what()) << "\n";
}
return true;
}
template <typename T> bool loadjson(const std::map<std::string, std::string>& parent, const std::string& key, T& item, void (*f)(const std::string&, T&), 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)) {
return false;
}
f(itr->second, 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 {
......@@ -749,9 +787,9 @@ template <typename T, typename TK> void PutValue(jsonalloc& alc, jsonval& ss, co
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 (auto& it = vec.begin(); it != vec.end(); ++it) {
for (const auto& it : vec) {
jsonval child(rapidjson::kObjectType);
if (f(alc, child, it->second)) {
if (f(alc, child, it.second)) {
jsvec.PushBack(child, alc);
}
}
......
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