Browse Source

Optimize util::get

master
Jørgen Lien Sellæg 9 years ago
parent
commit
6f6b14c318
  1. 16
      toREST/include/util.hpp

16
toREST/include/util.hpp

@ -74,20 +74,10 @@ public:
}
return nlohmann::json(nullptr);
}
static bool has_property(const std::string &key, const nlohmann::json &object) {
auto it = object.find(key);
return it != object.end();
}
template <class T>
static auto get(const std::string &key, const nlohmann::json &object, const T &default_value) {
if (has_property(key, object)) {
T r;
try {
r = object[key]; //TODO fix use json parser where a non-throwable exist
} catch (const std::exception &) {
return default_value;
}
return r;
static T get(const std::string &key, const nlohmann::json &object, const T &default_value) {
if (object.find(key) != object.end()) {
return object[key];
}
return default_value;
}

Loading…
Cancel
Save