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>();
139__JAFFAR_COMMON_INLINE__
const std::string
popString(
object& json,
const std::string& key)
141 const std::string value =
getString(json, key);
153__JAFFAR_COMMON_INLINE__
const object popObject(
object& json,
const std::string& key)
155 const object value =
getObject(json, key);
168__JAFFAR_COMMON_INLINE__
const std::vector<T>
popArray(
object& json,
const std::string& key)
170 const std::vector<T> value = getArray<T>(json, key);
183__JAFFAR_COMMON_INLINE__
const T
popNumber(
object& json,
const std::string& key)
185 const T value = getNumber<T>(json, key);
197__JAFFAR_COMMON_INLINE__
const bool popBoolean(
object& json,
const std::string& key)
212__JAFFAR_COMMON_INLINE__
const void checkEmpty(
const object& json,
const std::string& context)
214 if (json.is_object() ==
false)
return;
215 if (json.empty())
return;
217 for (
auto it = json.begin(); it != json.end(); ++it) keys += (keys.empty() ?
"'" :
", '") + it.key() +
"'";
218 JAFFAR_THROW_LOGIC(
"[Error] Unrecognized key(s) in %s: %s. Check for typos or unsupported options. JSON Dump: %s\n", context.c_str(), keys.c_str(), json.dump(2).c_str());
Contains common functions for exception throwing.
#define JAFFAR_THROW_LOGIC(...)
Definition exceptions.hpp:27
__JAFFAR_COMMON_INLINE__ const std::vector< T > popArray(object &json, const std::string &key)
Definition json.hpp:168
nlohmann::json object
Definition json.hpp:20
__JAFFAR_COMMON_INLINE__ const bool getBoolean(const object &json, const std::string &key)
Definition json.hpp:123
__JAFFAR_COMMON_INLINE__ const std::vector< T > getArray(const object &json, const std::string &key)
Definition json.hpp:86
__JAFFAR_COMMON_INLINE__ const bool popBoolean(object &json, const std::string &key)
Definition json.hpp:197
__JAFFAR_COMMON_INLINE__ const object & getObject(const object &json, const std::string &key)
Definition json.hpp:67
__JAFFAR_COMMON_INLINE__ const void checkEmpty(const object &json, const std::string &context)
Definition json.hpp:212
__JAFFAR_COMMON_INLINE__ const std::string popString(object &json, const std::string &key)
Definition json.hpp:139
__JAFFAR_COMMON_INLINE__ const void checkEntry(const object &json, const std::string &key)
Definition json.hpp:31
__JAFFAR_COMMON_INLINE__ const T getNumber(const object &json, const std::string &key)
Definition json.hpp:105
__JAFFAR_COMMON_INLINE__ const std::string getString(const object &json, const std::string &key)
Definition json.hpp:49
__JAFFAR_COMMON_INLINE__ const object popObject(object &json, const std::string &key)
Definition json.hpp:153
__JAFFAR_COMMON_INLINE__ const T popNumber(object &json, const std::string &key)
Definition json.hpp:183