| helics 3.6.1

JsonProcessingFunctions.hpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2017-2025,
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 <functional>
17 #include <nlohmann/json.hpp>
18 #include <string>
19 #include <string_view>
20
21 namespace helics::fileops {
22
25 class JsonBuffer {
26 public:
27 JsonBuffer(const nlohmann::json& jref): reference(jref) {}
28
29 const nlohmann::json& json() const { return reference; }
30
31 private:
32 const nlohmann::json& reference;
33 };
34
37 class JsonStorage {
38 public:
39 JsonStorage(const nlohmann::json& jref): jStorage(jref) {}
40 nlohmann::json& json() { return jStorage; }
41 const nlohmann::json& json() const { return jStorage; }
42
43 private:
44 nlohmann::json jStorage{};
45 };
46
48 bool hasJsonExtension(std::string_view jsonString);
50 bool looksLikeConfigJson(std::string_view jsonString);
54 nlohmann::json loadJson(const std::string& jsonString);
55
58 nlohmann::json loadJsonStr(std::string_view jsonString);
59
61 helics::Time loadJsonTime(const nlohmann::json& timeElement,
62 time_units defaultUnits = time_units::sec);
63
65 std::string getName(const nlohmann::json& element);
66
68 std::string generateJsonString(const nlohmann::json& block, bool hexConvert = true);
69
70 inline std::string JsonAsString(const nlohmann::json& element)
71 {
72 return (element.is_string()) ? element.get<std::string>() : generateJsonString(element);
73 }
74
75 inline std::string
76 getOrDefault(const nlohmann::json& element, const std::string& key, std::string_view defVal)
77 {
78 return (element.contains(key)) ? JsonAsString(element[key]) : std::string(defVal);
79 }
80
81 inline double getOrDefault(const nlohmann::json& element, const std::string& key, double defVal)
82 {
83 return (element.contains(key)) ? element[key].get<double>() : defVal;
84 }
85
86 inline bool getOrDefault(const nlohmann::json& element, const std::string& key, bool defVal)
87 {
88 return (element.contains(key)) ? element[key].get<bool>() : defVal;
89 }
90
91 inline int64_t getOrDefault(const nlohmann::json& element, const std::string& key, int64_t defVal)
92 {
93 return (element.contains(key)) ? element[key].get<int64_t>() : defVal;
94 }
95
96 inline bool callIfMember(const nlohmann::json& element,
97 const std::string& key,
98 const std::function<void(const std::string&, helics::Time)>& call)
99 {
100 bool isMember = element.contains(key);
101 if (isMember) {
102 call(key, loadJsonTime(element[key]));
103 }
104 return isMember;
105 }
106
107 inline bool callIfMember(const nlohmann::json& element,
108 const std::string& key,
109 const std::function<void(const std::string&, bool)>& call)
110 {
111 if (element.contains(key)) {
112 call(key, element[key].get<bool>());
113 return true;
114 }
115 return false;
116 }
117
118 inline bool callIfMember(const nlohmann::json& element,
119 const std::string& key,
120 const std::function<void(const std::string&, int)>& call)
121 {
122 if (element.contains(key)) {
123 call(key, element[key].get<int>());
124 return true;
125 }
126 return false;
127 }
128
129 inline bool callIfMember(const nlohmann::json& element,
130 const std::string& key,
131 const std::function<void(const std::string&)>& call)
132 {
133 if (element.contains(key)) {
134 call(element[key].get<std::string>());
135 return true;
136 }
137 return false;
138 }
139
140 inline void
141 replaceIfMember(const nlohmann::json& element, const std::string& key, helics::Time& timeVal)
142 {
143 if (element.contains(key)) {
144 timeVal = loadJsonTime(element[key]);
145 }
146 }
147
148 inline void
149 replaceIfMember(const nlohmann::json& element, const std::string& key, std::string& sval)
150 {
151 if (element.contains(key)) {
152 sval = element[key].get<std::string>();
153 }
154 }
155
156 inline void replaceIfMember(const nlohmann::json& element, const std::string& key, bool& bval)
157 {
158 if (element.contains(key)) {
159 bval = element[key].get<bool>();
160 }
161 }
162
163 inline void replaceIfMember(const nlohmann::json& element, const std::string& key, int& sval)
164 {
165 if (element.contains(key)) {
166 sval = element[key].get<int>();
167 }
168 }
169
170 inline void replaceIfMember(const nlohmann::json& element, const std::string& key, double& sval)
171 {
172 if (element.contains(key)) {
173 sval = element[key].get<double>();
174 }
175 }
176
177 } // namespace helics::fileops
helics::fileops::isMember
bool isMember(const toml::value &element, const std::string &key)
Definition: TomlProcessingFunctions.hpp:157
helics::fileops::JsonBuffer
Definition: JsonProcessingFunctions.hpp:25
helics::fileops::JsonStorage
Definition: JsonProcessingFunctions.hpp:37
helics::Time
TimeRepresentation< count_time< 9 > > Time
Definition: helicsTime.hpp:27