14 #include "../core/helicsTime.hpp"
16 #include "json/json.h"
19 namespace helics::fileops {
22 bool hasJsonExtension(
const std::string& jsonString);
27 Json::Value loadJson(
const std::string& jsonString);
31 Json::Value loadJsonStr(std::string_view jsonString);
34 helics::Time loadJsonTime(
const Json::Value& timeElement,
35 time_units defaultUnits = time_units::sec);
38 std::string getName(
const Json::Value& element);
41 std::string generateJsonString(
const Json::Value& block);
43 inline std::string JsonAsString(
const Json::Value& element)
45 return (element.isString()) ? element.asString() : generateJsonString(element);
49 getOrDefault(
const Json::Value& element,
const std::string& key, std::string_view defVal)
51 return (element.isMember(key)) ? JsonAsString(element[key]) : std::string(defVal);
54 inline double getOrDefault(
const Json::Value& element,
const std::string& key,
double defVal)
56 return (element.isMember(key)) ? element[key].asDouble() : defVal;
59 inline bool getOrDefault(
const Json::Value& element,
const std::string& key,
bool defVal)
61 return (element.isMember(key)) ? element[key].asBool() : defVal;
64 inline int64_t getOrDefault(
const Json::Value& element,
const std::string& key, int64_t defVal)
66 return (element.isMember(key)) ? element[key].asInt64() : defVal;
69 inline bool callIfMember(
const Json::Value& element,
70 const std::string& key,
71 const std::function<
void(
const std::string&,
helics::Time)>& call)
73 if (element.isMember(key)) {
74 call(key, loadJsonTime(element[key]));
80 inline bool callIfMember(
const Json::Value& element,
81 const std::string& key,
82 const std::function<
void(
const std::string&,
bool)>& call)
84 if (element.isMember(key)) {
85 call(key, element[key].asBool());
91 inline bool callIfMember(
const Json::Value& element,
92 const std::string& key,
93 const std::function<
void(
const std::string&,
int)>& call)
95 if (element.isMember(key)) {
96 call(key, element[key].asInt());
102 inline bool callIfMember(
const Json::Value& element,
103 const std::string& key,
104 const std::function<
void(
const std::string&)>& call)
106 if (element.isMember(key)) {
107 call(element[key].asString());
114 replaceIfMember(
const Json::Value& element,
const std::string& key,
helics::Time& timeVal)
116 if (element.isMember(key)) {
117 timeVal = loadJsonTime(element[key]);
121 inline void replaceIfMember(
const Json::Value& element,
const std::string& key, std::string& sval)
123 if (element.isMember(key)) {
124 sval = element[key].asString();
128 inline void replaceIfMember(
const Json::Value& element,
const std::string& key,
bool& bval)
130 if (element.isMember(key)) {
131 bval = element[key].asBool();
135 inline void replaceIfMember(
const Json::Value& element,
const std::string& key,
int& sval)
137 if (element.isMember(key)) {
138 sval = element[key].asInt();
142 inline void replaceIfMember(
const Json::Value& element,
const std::string& key,
double& sval)
144 if (element.isMember(key)) {
145 sval = element[key].asDouble();