helics 3.7.0
Loading...
Searching...
No Matches
JsonProcessingFunctions.hpp
Go to the documentation of this file.
1/*
2Copyright (c) 2017-2026,
3Battelle Memorial Institute; Lawrence Livermore National Security, LLC; Alliance for Energy
4Innovation LLC. See the top-level NOTICE for additional details. All rights reserved.
5SPDX-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
21namespace helics::fileops {
22
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
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
48bool hasJsonExtension(std::string_view jsonString);
50bool looksLikeConfigJson(std::string_view jsonString);
54nlohmann::json loadJson(const std::string& jsonString);
55
58nlohmann::json loadJsonStr(std::string_view jsonString);
59
61helics::Time loadJsonTime(const nlohmann::json& timeElement,
62 time_units defaultUnits = time_units::sec);
63
65std::string getName(const nlohmann::json& element);
66
68std::string generateJsonString(const nlohmann::json& block, bool hexConvert = true);
69
70inline std::string JsonAsString(const nlohmann::json& element)
71{
72 return (element.is_string()) ? element.get<std::string>() : generateJsonString(element);
73}
74
75inline 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
81inline 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
86inline 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
91inline 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
96inline 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
107inline 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
118inline 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
129inline 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
140inline 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
148inline 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
156inline 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
163inline 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
170inline 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
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
STL namespace.