27 #ifndef JSON_HELPERS_H_
28 #define JSON_HELPERS_H_
32 #include <nlohmann/json.hpp>
36 using namespace nlohmann;
43 inline void require_value(
const json& json,
const std::string& msg =
"Missing JSON value") {
52 inline void require_key(
const json& json,
const std::string& key) {
53 std::string msg{
"Missing JSON key: " + key };
54 if (!json.contains(key)) {
64 template <
typename T_>
66 if (j[key].is_null()) {
69 return j[key].get<T_>();
80 inline std::string
optional_value_or(
const json& j,
const std::string& key,
const std::string& or_val) {
81 if (j[key].is_null() || j[key].get<std::string>().empty()) {
84 return j[key].get<std::string>();
90 template <
typename T_>
91 inline std::optional<T_>
optional_value(
const json& j,
const std::string& key) {
92 if (j[key].is_null()) {
95 return std::optional<T_>{ j[key].get<T_>() };
104 inline std::optional<std::string>
optional_value(
const json& j,
const std::string& key) {
105 if (j[key].is_null() || j[key].get<std::string>().empty()) {
108 return std::optional<std::string>{ j[key].get<std::string>() };
114 template <
typename T_>
115 inline T_
require_value(
const json& json,
const std::string& key) {
118 return static_cast<T_
>(json[key].get<T_>());
122 std::string s{ in_stream.str() };
128 return json::parse(
"[" + s +
"]");
Definition: common-types.h:33
json wrap_and_parse(std::stringstream &in_stream)
Definition: json-helpers.hpp:121
std::optional< std::string > optional_value(const json &j, const std::string &key)
Parses the (potentialy null or empty) string from the given key in JSON structure.
Definition: json-helpers.hpp:104
std::string optional_value_or(const json &j, const std::string &key, const std::string &or_val)
Parses the (potentialy null) value from the given key in JSON structure and if not defined or null us...
Definition: json-helpers.hpp:80
void require_key(const json &json, const std::string &key)
Makes sure that the given json has the given key and the key is not null.
Definition: json-helpers.hpp:52
void require_value(const json &json, const std::string &msg="Missing JSON value")
Makes sure that the given json is defined (not null and not undefined).
Definition: json-helpers.hpp:43
#define SHLOG_E_THROW(x)
Definition: static-logger.hpp:158