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
139__JAFFAR_COMMON_INLINE__ const std::string popString(object& json, const std::string& key)
140{
141 const std::string value = getString(json, key);
142 json.erase(key);
143 return value;
144}
145
153__JAFFAR_COMMON_INLINE__ const object popObject(object& json, const std::string& key)
154{
155 const object value = getObject(json, key);
156 json.erase(key);
157 return value;
158}
159
167template <typename T>
168__JAFFAR_COMMON_INLINE__ const std::vector<T> popArray(object& json, const std::string& key)
169{
170 const std::vector<T> value = getArray<T>(json, key);
171 json.erase(key);
172 return value;
173}
174
182template <typename T>
183__JAFFAR_COMMON_INLINE__ const T popNumber(object& json, const std::string& key)
184{
185 const T value = getNumber<T>(json, key);
186 json.erase(key);
187 return value;
188}
189
197__JAFFAR_COMMON_INLINE__ const bool popBoolean(object& json, const std::string& key)
198{
199 const bool value = getBoolean(json, key);
200 json.erase(key);
201 return value;
202}
203
212__JAFFAR_COMMON_INLINE__ const void checkEmpty(const object& json, const std::string& context)
213{
214 if (json.is_object() == false) return;
215 if (json.empty()) return;
216 std::string keys;
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());
219}
220
221} // namespace json
222
223} // namespace jaffarCommon
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