jaffarCommon
Loading...
Searching...
No Matches
json.hpp
Go to the documentation of this file.
1#pragma once
2
7#include "exceptions.hpp"
8#include <json/single_include/nlohmann/json.hpp>
9#include <stdlib.h>
10
11namespace jaffarCommon
12{
13
14namespace json
15{
16
20typedef nlohmann::json object;
21
31__JAFFAR_COMMON__INLINE__ const void checkEntry(const object& json, const std::string& key)
32{
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());
36}
37
49__JAFFAR_COMMON__INLINE__ const std::string getString(const object& json, const std::string& key)
50{
51 checkEntry(json, 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>();
54}
55
67__JAFFAR_COMMON__INLINE__ const object& getObject(const object& json, const std::string& key)
68{
69 checkEntry(json, 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());
71 return json.at(key);
72}
73
85template <typename T>
86__JAFFAR_COMMON__INLINE__ const std::vector<T> getArray(const object& json, const std::string& key)
87{
88 checkEntry(json, 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>>();
91}
92
104template <typename T>
105__JAFFAR_COMMON__INLINE__ const T getNumber(const object& json, const std::string& key)
106{
107 checkEntry(json, 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>();
110}
111
123__JAFFAR_COMMON__INLINE__ const bool getBoolean(const object& json, const std::string& key)
124{
125 checkEntry(json, 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>();
128}
129
130} // namespace json
131
132} // namespace jaffarCommon
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