21 #ifndef SNABSUITE_UTIL_READ_JSON_HPP 22 #define SNABSUITE_UTIL_READ_JSON_HPP 24 #include <cypress/cypress.hpp> 33 using cypress::global_logger;
38 std::map<std::string, T>
json_to_map(
const cypress::Json &obj)
40 std::map<std::string, T> input;
41 for (
auto i = obj.begin();
i != obj.end();
i++) {
42 const cypress::Json value =
i.value();
44 if (value.is_number()) {
45 input.emplace(
i.key(), value);
56 std::vector<T>
read_check(std::map<std::string, T> &input,
57 const std::vector<std::string> &names,
58 const std::vector<T> &defaults)
60 std::vector<T> res(names.size());
61 for (
size_t i = 0;
i < res.size();
i++) {
62 auto it = input.find(names[
i]);
63 if (it != input.end()) {
68 res[
i] = defaults.at(i);
69 global_logger().debug(
"SNABSuite",
70 "For " + names[i] +
" the default value " +
71 std::to_string(defaults[i]) +
" is used");
74 for (
auto elem : input) {
75 throw std::invalid_argument(
"Unknown parameter \"" + elem.first +
"\"");
81 cypress::Json
read_config(
const std::string &name,
const std::string &backend);
84 const cypress::Json &json,
85 const std::string name);
92 const std::string &backend);
100 template <
typename T>
103 if (!json.is_array() || json.size() == 0 || json[0].is_array()) {
104 throw std::invalid_argument(
"Error in conversion from Json to array!");
107 for (
auto i : json) {
109 res.push_back(std::numeric_limits<T>::quiet_NaN());
124 template <
typename T,
int n>
127 if (!json.is_array() || json.size() == 0 || json[0].is_array()) {
128 throw std::invalid_argument(
"Error in conversion from Json to array!");
130 std::array<T, n> res;
132 for (
auto i : json) {
134 res[
index] = std::numeric_limits<T>::quiet_NaN();
149 template <
typename T>
152 if (!json.is_array() || !json[0].is_array()) {
153 throw std::invalid_argument(
"Error in conversion from Json to array!");
155 std::vector<std::vector<T>> res;
156 for (
auto i : json) {
157 res.push_back(json_array_to_vector<T>(
i));
167 template <
typename T,
int n>
170 if (!json.is_array() || !json[0].is_array()) {
171 throw std::invalid_argument(
"Error in conversion from Json to array!");
173 std::vector<std::vector<std::array<T, n>>> res;
174 for (
size_t i = 0;
i< json.size();
i++) {
175 std::vector<std::array<T, n>> res2;
176 for (
size_t j = 0; j< json[
i].size(); j++) {
177 res2.push_back(json_array_to_array<T, n>(json[
i][j]));
192 std::string name =
"",
bool warn =
true);
std::vector< T > read_check(std::map< std::string, T > &input, const std::vector< std::string > &names, const std::vector< T > &defaults)
std::vector< std::vector< std::array< T, n > > > json_3Darray_to_vector(const cypress::Json &json)
std::vector< std::vector< T > > json_2Darray_to_vector(const cypress::Json &json)
std::vector< T > json_array_to_vector(const cypress::Json &json)
cypress::Json read_config(const std::string &name, const std::string &backend)
cypress::Json extract_backend(const cypress::Json &config, const std::string &backend)
std::map< std::string, T > json_to_map(const cypress::Json &obj)
bool replace_arrays_by_value(cypress::Json &json, const size_t &index=0, std::string name="", bool warn=true)
bool check_json_for_parameters(const std::vector< std::string > ¶meters, const cypress::Json &json, const std::string name)
std::array< T, n > json_array_to_array(const cypress::Json &json)