14 #include "../core/helicsTime.hpp"
16 #include "json/json.h"
19 #include <string_view>
21 namespace helics::fileops {
23 bool hasJsonExtension(std::string_view jsonString);
25 bool looksLikeConfigJson(std::string_view jsonString);
29 Json::Value loadJson(
const std::string& jsonString);
33 Json::Value loadJsonStr(std::string_view jsonString);
36 helics::Time loadJsonTime(
const Json::Value& timeElement,
37 time_units defaultUnits = time_units::sec);
40 std::string getName(
const Json::Value& element);
43 std::string generateJsonString(
const Json::Value& block);
45 inline std::string JsonAsString(
const Json::Value& element)
47 return (element.isString()) ? element.asString() : generateJsonString(element);
51 getOrDefault(
const Json::Value& element,
const std::string& key, std::string_view defVal)
53 return (element.isMember(key)) ? JsonAsString(element[key]) : std::string(defVal);
56 inline double getOrDefault(
const Json::Value& element,
const std::string& key,
double defVal)
58 return (element.isMember(key)) ? element[key].asDouble() : defVal;
61 inline bool getOrDefault(
const Json::Value& element,
const std::string& key,
bool defVal)
63 return (element.isMember(key)) ? element[key].asBool() : defVal;
66 inline int64_t getOrDefault(
const Json::Value& element,
const std::string& key, int64_t defVal)
68 return (element.isMember(key)) ? element[key].asInt64() : defVal;
71 inline bool callIfMember(
const Json::Value& element,
72 const std::string& key,
73 const std::function<
void(
const std::string&,
helics::Time)>& call)
75 if (element.isMember(key)) {
76 call(key, loadJsonTime(element[key]));
82 inline bool callIfMember(
const Json::Value& element,
83 const std::string& key,
84 const std::function<
void(
const std::string&,
bool)>& call)
86 if (element.isMember(key)) {
87 call(key, element[key].asBool());
93 inline bool callIfMember(
const Json::Value& element,
94 const std::string& key,
95 const std::function<
void(
const std::string&,
int)>& call)
97 if (element.isMember(key)) {
98 call(key, element[key].asInt());
104 inline bool callIfMember(
const Json::Value& element,
105 const std::string& key,
106 const std::function<
void(
const std::string&)>& call)
108 if (element.isMember(key)) {
109 call(element[key].asString());
116 replaceIfMember(
const Json::Value& element,
const std::string& key,
helics::Time& timeVal)
118 if (element.isMember(key)) {
119 timeVal = loadJsonTime(element[key]);
123 inline void replaceIfMember(
const Json::Value& element,
const std::string& key, std::string& sval)
125 if (element.isMember(key)) {
126 sval = element[key].asString();
130 inline void replaceIfMember(
const Json::Value& element,
const std::string& key,
bool& bval)
132 if (element.isMember(key)) {
133 bval = element[key].asBool();
137 inline void replaceIfMember(
const Json::Value& element,
const std::string& key,
int& sval)
139 if (element.isMember(key)) {
140 sval = element[key].asInt();
144 inline void replaceIfMember(
const Json::Value& element,
const std::string& key,
double& sval)
146 if (element.isMember(key)) {
147 sval = element[key].asDouble();
TimeRepresentation< count_time< 9 > > Time
Definition: helicsTime.hpp:27