8#include <json/single_include/nlohmann/json.hpp>
31__JAFFAR_COMMON__INLINE__
const void checkEntry(
const object& json,
const std::string& key)
33 if (json.is_object() ==
false)
34 JAFFAR_THROW_LOGIC(
"[Error] JSON passed is not a key/value object. Happened when trying to obtain string key '%s'. JSON Dump: %s\n", key.c_str(), json.dump(2).c_str());
35 if (json.contains(key) ==
false)
JAFFAR_THROW_LOGIC(
"[Error] JSON contains no field called '%s'. JSON Dump: %s\n", key.c_str(), json.dump(2).c_str());
49__JAFFAR_COMMON__INLINE__
const std::string
getString(
const object& json,
const std::string& key)
52 if (json[key].is_string() ==
false)
JAFFAR_THROW_LOGIC(
"[Error] Configuration key '%s' is not a string. JSON Dump: %s\n", key.c_str(), json.dump(2).c_str());
53 return json.at(key).get<std::string>();
67__JAFFAR_COMMON__INLINE__
const object&
getObject(
const object& json,
const std::string& key)
70 if (json[key].is_object() ==
false)
JAFFAR_THROW_LOGIC(
"[Error] Configuration key '%s' is not a key/value object. JSON Dump: %s\n", key.c_str(), json.dump(2).c_str());
86__JAFFAR_COMMON__INLINE__
const std::vector<T>
getArray(
const object& json,
const std::string& key)
89 if (json[key].is_array() ==
false)
JAFFAR_THROW_LOGIC(
"[Error] Configuration key '%s' is not an array. JSON Dump: %s\n", key.c_str(), json.dump(2).c_str());
90 return json.at(key).get<std::vector<T>>();
105__JAFFAR_COMMON__INLINE__
const T
getNumber(
const object& json,
const std::string& key)
108 if (json[key].is_number() ==
false)
JAFFAR_THROW_LOGIC(
"[Error] Configuration key '%s' is not a number. JSON Dump: %s\n", key.c_str(), json.dump(2).c_str());
109 return json.at(key).get<T>();
123__JAFFAR_COMMON__INLINE__
const bool getBoolean(
const object& json,
const std::string& key)
126 if (json[key].is_boolean() ==
false)
JAFFAR_THROW_LOGIC(
"[Error] Configuration key '%s' is not a boolean. JSON Dump: %s\n", key.c_str(), json.dump(2).c_str());
127 return json.at(key).get<
bool>();
Contains common functions for exception throwing.
#define JAFFAR_THROW_LOGIC(...)
Definition exceptions.hpp:27
__JAFFAR_COMMON__INLINE__ const void checkEntry(const object &json, const std::string &key)
Definition json.hpp:31
nlohmann::json object
Definition json.hpp:20
__JAFFAR_COMMON__INLINE__ const object & getObject(const object &json, const std::string &key)
Definition json.hpp:67
__JAFFAR_COMMON__INLINE__ const bool getBoolean(const object &json, const std::string &key)
Definition json.hpp:123
__JAFFAR_COMMON__INLINE__ const std::string getString(const object &json, const std::string &key)
Definition json.hpp:49
__JAFFAR_COMMON__INLINE__ const T getNumber(const object &json, const std::string &key)
Definition json.hpp:105
__JAFFAR_COMMON__INLINE__ const std::vector< T > getArray(const object &json, const std::string &key)
Definition json.hpp:86