helics  3.5.2
JsonProcessingFunctions.hpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2017-2024,
3 Battelle Memorial Institute; Lawrence Livermore National Security, LLC; Alliance for Sustainable
4 Energy, LLC. See the top-level NOTICE for additional details. All rights reserved.
5 SPDX-License-Identifier: BSD-3-Clause
6 */
7 #pragma once
8 
14 #include "../core/helicsTime.hpp"
15 
16 #include "json/json.h"
17 #include <functional>
18 #include <string>
19 #include <string_view>
20 
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);
30 
33 Json::Value loadJsonStr(std::string_view jsonString);
34 
36 helics::Time loadJsonTime(const Json::Value& timeElement,
37  time_units defaultUnits = time_units::sec);
38 
40 std::string getName(const Json::Value& element);
41 
43 std::string generateJsonString(const Json::Value& block);
44 
45 inline std::string JsonAsString(const Json::Value& element)
46 {
47  return (element.isString()) ? element.asString() : generateJsonString(element);
48 }
49 
50 inline std::string
51  getOrDefault(const Json::Value& element, const std::string& key, std::string_view defVal)
52 {
53  return (element.isMember(key)) ? JsonAsString(element[key]) : std::string(defVal);
54 }
55 
56 inline double getOrDefault(const Json::Value& element, const std::string& key, double defVal)
57 {
58  return (element.isMember(key)) ? element[key].asDouble() : defVal;
59 }
60 
61 inline bool getOrDefault(const Json::Value& element, const std::string& key, bool defVal)
62 {
63  return (element.isMember(key)) ? element[key].asBool() : defVal;
64 }
65 
66 inline int64_t getOrDefault(const Json::Value& element, const std::string& key, int64_t defVal)
67 {
68  return (element.isMember(key)) ? element[key].asInt64() : defVal;
69 }
70 
71 inline bool callIfMember(const Json::Value& element,
72  const std::string& key,
73  const std::function<void(const std::string&, helics::Time)>& call)
74 {
75  if (element.isMember(key)) {
76  call(key, loadJsonTime(element[key]));
77  return true;
78  }
79  return false;
80 }
81 
82 inline bool callIfMember(const Json::Value& element,
83  const std::string& key,
84  const std::function<void(const std::string&, bool)>& call)
85 {
86  if (element.isMember(key)) {
87  call(key, element[key].asBool());
88  return true;
89  }
90  return false;
91 }
92 
93 inline bool callIfMember(const Json::Value& element,
94  const std::string& key,
95  const std::function<void(const std::string&, int)>& call)
96 {
97  if (element.isMember(key)) {
98  call(key, element[key].asInt());
99  return true;
100  }
101  return false;
102 }
103 
104 inline bool callIfMember(const Json::Value& element,
105  const std::string& key,
106  const std::function<void(const std::string&)>& call)
107 {
108  if (element.isMember(key)) {
109  call(element[key].asString());
110  return true;
111  }
112  return false;
113 }
114 
115 inline void
116  replaceIfMember(const Json::Value& element, const std::string& key, helics::Time& timeVal)
117 {
118  if (element.isMember(key)) {
119  timeVal = loadJsonTime(element[key]);
120  }
121 }
122 
123 inline void replaceIfMember(const Json::Value& element, const std::string& key, std::string& sval)
124 {
125  if (element.isMember(key)) {
126  sval = element[key].asString();
127  }
128 }
129 
130 inline void replaceIfMember(const Json::Value& element, const std::string& key, bool& bval)
131 {
132  if (element.isMember(key)) {
133  bval = element[key].asBool();
134  }
135 }
136 
137 inline void replaceIfMember(const Json::Value& element, const std::string& key, int& sval)
138 {
139  if (element.isMember(key)) {
140  sval = element[key].asInt();
141  }
142 }
143 
144 inline void replaceIfMember(const Json::Value& element, const std::string& key, double& sval)
145 {
146  if (element.isMember(key)) {
147  sval = element[key].asDouble();
148  }
149 }
150 
151 } // namespace helics::fileops
TimeRepresentation< count_time< 9 > > Time
Definition: helicsTime.hpp:27