SOMHunter Core
json-helpers.hpp
Go to the documentation of this file.
1 /* This file is part of SOMHunter.
2  *
3  * Copyright (C) 2021 Frantisek Mejzlik <frankmejzlik@protonmail.com>
4  * Mirek Kratochvil <exa.exa@gmail.com>
5  * Patrik Vesely <prtrikvesely@gmail.com>
6  *
7  * SOMHunter is free software: you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License as published by the Free
9  * Software Foundation, either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * SOMHunter is distributed in the hope that it will be useful, but WITHOUT ANY
13  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15  * details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * SOMHunter. If not, see <https://www.gnu.org/licenses/>.
19  */
20 
27 #ifndef JSON_HELPERS_H_
28 #define JSON_HELPERS_H_
29 
30 #include <sstream>
31 // ---
32 #include <nlohmann/json.hpp>
33 // ---
34 #include "common.h"
35 
36 using namespace nlohmann;
37 
38 namespace sh {
39 
43 inline void require_value(const json& json, const std::string& msg = "Missing JSON value") {
44  if (json.is_null()) {
45  SHLOG_E_THROW(msg);
46  }
47 }
48 
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)) {
55  SHLOG_E_THROW(msg);
56  }
57  require_value(json[key], msg);
58 }
59 
64 template <typename T_>
65 inline T_ optional_value_or(const json& j, const std::string& key, const T_& or_val) {
66  if (j[key].is_null()) {
67  return or_val;
68  } else {
69  return j[key].get<T_>();
70  }
71 }
72 
79 template <>
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()) {
82  return or_val;
83  }
84  return j[key].get<std::string>();
85 }
86 
90 template <typename T_>
91 inline std::optional<T_> optional_value(const json& j, const std::string& key) {
92  if (j[key].is_null()) {
93  return std::nullopt;
94  }
95  return std::optional<T_>{ j[key].get<T_>() };
96 }
97 
103 template <>
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()) {
106  return std::nullopt;
107  }
108  return std::optional<std::string>{ j[key].get<std::string>() };
109 }
110 
114 template <typename T_>
115 inline T_ require_value(const json& json, const std::string& key) {
116  require_key(json, key);
117 
118  return static_cast<T_>(json[key].get<T_>());
119 }
120 
121 inline json wrap_and_parse(std::stringstream& in_stream) {
122  std::string s{ in_stream.str() };
123 
124  if (s[0] == ',') {
125  s[0] = ' ';
126  }
127 
128  return json::parse("[" + s + "]");
129 }
130 
131 } // namespace sh
132 
133 #endif // JSON_HELPERS_H_
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