#ifndef JANSSON_ADDITIONALS_H #define JANSSON_ADDITIONALS_H #include #include #include #include #include "blitz/array.h" #include "jansson.h" #include "simulation/errors.h" #include "simulation/unit_converter.h" // define the json_path type; contains the keys of the nested json-objects // as the elements in the vector e.g. ["Root", "Group1", "Group2", ..., "GroupN"] typedef std::vector json_path; // return the value specified by "path" as a json_t pointer // (can point to a json-object, json-array, etc.) inline json_t* json_object_by_path(json_t* object, const json_path& path) { json_t* j_obj = object; for (auto str : path) { j_obj = json_object_get(j_obj, str.c_str()); } return j_obj; } // return the value of the json_t pointer "object" as an double; defaults "default value" inline double json_double(json_t* object, double default_value = 0.) { if (object) { return json_real_value(object); } else { return default_value; } } // return the value of the json_t pointer "object" as an int; defaults to "default value" // if the pointer is not a json-real value inline int json_int(json_t* object, int default_value = 0) { if (object) { return std::floor(json_real_value(object)); } else { return default_value; } } // return the value of the json_t pointer "object" as a string; defaults "default value" // if the pointer is not a json-string inline std::string json_string(json_t* object, std::string default_value = "") { if (object) { return std::string(json_string_value(object)); } else { return default_value; } } // return the value of the json_t pointer "object" as a bool; defaults to default_value // if the pointer is not a json-string or the string has a value different from "true" or "false" inline bool json_bool(json_t* object, bool default_value = false) { if (object) { return (json_string(object) == "true"); } else { return default_value; } } // return the json-array "vector" in the given json-object object as a blitz::TinyVector // if the "vector" json-array does not exist or does not contain the same number of elements as // specified in the template parameter s an error is thrown template blitz::TinyVector json_blitz_vector(json_t* object) { blitz::TinyVector vector; size_t index; json_t* value; json_t* array = json_object_by_path(object, {"vector"}); size_t size = json_array_size(array); if (size != s) { std::stringstream error_message; error_message << "The provided vector has size: " << size << ", but is stated as size " << s; ERROR(error_message.str()); } json_array_foreach(array, index, value) { vector(index) = json_double(value); } return vector; } // returns a vector containing the values of a json-array pointed to by // json_t* object; if json_t* object is not a json-array returns an empty vector inline std::vector json_vector(json_t* object) { std::vector vector; size_t index; json_t* value; json_array_foreach(object, index, value) { vector.push_back(json_double(value)); } return vector; } // return the json-array in "object" as a json_path variable inline json_path json_path_get(json_t* object) { json_path path; json_t* value; size_t index; json_array_foreach(object, index, value) { path.push_back(json_string(value)); } return path; } // returns a json_path variable, that consists of the path appended by the // json_path append inline json_path json_path_append(const json_path& path, const json_path& append) { json_path result = path; for (auto a : append) { result.push_back(a); } return result; } // returns the value of a json-object, which should consist of a json-real with the key // "value" and a json-string with the key "unit" transformed to the specified length // "output_unit" (supported units: "mm", "cm", "m"). If the key "unit" does not exist or is not // a json-string an error will be thrown and the program will terminate. If the key "value" // does not exist or not a json-real it will default to 0.0 inline double json_read_length(json_t* object, std::string output_unit = "m") { double value = json_double(json_object_by_path(object, {"value"})); std::string unit = json_string(json_object_by_path(object, {"unit"})); return unitConvertion::length(value, unit, output_unit); } /* returns the value of a json-object, which should consist of a json-real with the key "value" and a json-string with the key "unit" transformed to the specified energy "output_unit" (supported units: "eV", "J"). If the key "unit" does not exist or is not a json-string an error will be thrown and the program will terminate. If the key "value" does not exist or not a json-real it will default to 0.0 */ inline double json_read_energy(json_t* object, std::string output_unit = "eV") { double value = json_double(json_object_by_path(object, {"value"})); std::string unit = json_string(json_object_by_path(object, {"unit"})); return unitConvertion::energy(value, unit, output_unit); } #endif /* JANSSON_ADDITIONALS_H */