14 #include "../core/helicsTime.hpp"
17 #include <nlohmann/json.hpp>
19 #include <string_view>
21 namespace helics::fileops {
27 JsonBuffer(
const nlohmann::json& jref): reference(jref) {}
29 const nlohmann::json& json()
const {
return reference; }
32 const nlohmann::json& reference;
39 JsonStorage(
const nlohmann::json& jref): jStorage(jref) {}
40 nlohmann::json& json() {
return jStorage; }
41 const nlohmann::json& json()
const {
return jStorage; }
44 nlohmann::json jStorage{};
48 bool hasJsonExtension(std::string_view jsonString);
50 bool looksLikeConfigJson(std::string_view jsonString);
54 nlohmann::json loadJson(
const std::string& jsonString);
58 nlohmann::json loadJsonStr(std::string_view jsonString);
61 helics::Time loadJsonTime(
const nlohmann::json& timeElement,
62 time_units defaultUnits = time_units::sec);
65 std::string getName(
const nlohmann::json& element);
68 std::string generateJsonString(
const nlohmann::json& block,
bool hexConvert =
true);
70 inline std::string JsonAsString(
const nlohmann::json& element)
72 return (element.is_string()) ? element.get<std::string>() : generateJsonString(element);
76 getOrDefault(
const nlohmann::json& element,
const std::string& key, std::string_view defVal)
78 return (element.contains(key)) ? JsonAsString(element[key]) : std::string(defVal);
81 inline double getOrDefault(
const nlohmann::json& element,
const std::string& key,
double defVal)
83 return (element.contains(key)) ? element[key].get<
double>() : defVal;
86 inline bool getOrDefault(
const nlohmann::json& element,
const std::string& key,
bool defVal)
88 return (element.contains(key)) ? element[key].get<
bool>() : defVal;
91 inline int64_t getOrDefault(
const nlohmann::json& element,
const std::string& key, int64_t defVal)
93 return (element.contains(key)) ? element[key].get<int64_t>() : defVal;
96 inline bool callIfMember(
const nlohmann::json& element,
97 const std::string& key,
98 const std::function<
void(
const std::string&,
helics::Time)>& call)
100 bool isMember = element.contains(key);
102 call(key, loadJsonTime(element[key]));
107 inline bool callIfMember(
const nlohmann::json& element,
108 const std::string& key,
109 const std::function<
void(
const std::string&,
bool)>& call)
111 if (element.contains(key)) {
112 call(key, element[key].get<bool>());
118 inline bool callIfMember(
const nlohmann::json& element,
119 const std::string& key,
120 const std::function<
void(
const std::string&,
int)>& call)
122 if (element.contains(key)) {
123 call(key, element[key].get<int>());
129 inline bool callIfMember(
const nlohmann::json& element,
130 const std::string& key,
131 const std::function<
void(
const std::string&)>& call)
133 if (element.contains(key)) {
134 call(element[key].get<std::string>());
141 replaceIfMember(
const nlohmann::json& element,
const std::string& key,
helics::Time& timeVal)
143 if (element.contains(key)) {
144 timeVal = loadJsonTime(element[key]);
149 replaceIfMember(
const nlohmann::json& element,
const std::string& key, std::string& sval)
151 if (element.contains(key)) {
152 sval = element[key].get<std::string>();
156 inline void replaceIfMember(
const nlohmann::json& element,
const std::string& key,
bool& bval)
158 if (element.contains(key)) {
159 bval = element[key].get<
bool>();
163 inline void replaceIfMember(
const nlohmann::json& element,
const std::string& key,
int& sval)
165 if (element.contains(key)) {
166 sval = element[key].get<
int>();
170 inline void replaceIfMember(
const nlohmann::json& element,
const std::string& key,
double& sval)
172 if (element.contains(key)) {
173 sval = element[key].get<
double>();
bool isMember(const toml::value &element, const std::string &key)
Definition: TomlProcessingFunctions.hpp:157
Definition: JsonProcessingFunctions.hpp:25
Definition: JsonProcessingFunctions.hpp:37
TimeRepresentation< count_time< 9 > > Time
Definition: helicsTime.hpp:27