helics  3.3.0
JsonProcessingFunctions.hpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2017-2022,
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 namespace helics::fileops {
20 
22 bool hasJsonExtension(const std::string& jsonString);
23 
27 Json::Value loadJson(const std::string& jsonString);
28 
31 Json::Value loadJsonStr(std::string_view jsonString);
32 
34 helics::Time loadJsonTime(const Json::Value& timeElement,
35  time_units defaultUnits = time_units::sec);
36 
38 std::string getName(const Json::Value& element);
39 
41 std::string generateJsonString(const Json::Value& block);
42 
43 inline std::string JsonAsString(const Json::Value& element)
44 {
45  return (element.isString()) ? element.asString() : generateJsonString(element);
46 }
47 
48 inline std::string
49  getOrDefault(const Json::Value& element, const std::string& key, std::string_view defVal)
50 {
51  return (element.isMember(key)) ? JsonAsString(element[key]) : std::string(defVal);
52 }
53 
54 inline double getOrDefault(const Json::Value& element, const std::string& key, double defVal)
55 {
56  return (element.isMember(key)) ? element[key].asDouble() : defVal;
57 }
58 
59 inline bool getOrDefault(const Json::Value& element, const std::string& key, bool defVal)
60 {
61  return (element.isMember(key)) ? element[key].asBool() : defVal;
62 }
63 
64 inline int64_t getOrDefault(const Json::Value& element, const std::string& key, int64_t defVal)
65 {
66  return (element.isMember(key)) ? element[key].asInt64() : defVal;
67 }
68 
69 inline bool callIfMember(const Json::Value& element,
70  const std::string& key,
71  const std::function<void(const std::string&, helics::Time)>& call)
72 {
73  if (element.isMember(key)) {
74  call(key, loadJsonTime(element[key]));
75  return true;
76  }
77  return false;
78 }
79 
80 inline bool callIfMember(const Json::Value& element,
81  const std::string& key,
82  const std::function<void(const std::string&, bool)>& call)
83 {
84  if (element.isMember(key)) {
85  call(key, element[key].asBool());
86  return true;
87  }
88  return false;
89 }
90 
91 inline bool callIfMember(const Json::Value& element,
92  const std::string& key,
93  const std::function<void(const std::string&, int)>& call)
94 {
95  if (element.isMember(key)) {
96  call(key, element[key].asInt());
97  return true;
98  }
99  return false;
100 }
101 
102 inline bool callIfMember(const Json::Value& element,
103  const std::string& key,
104  const std::function<void(const std::string&)>& call)
105 {
106  if (element.isMember(key)) {
107  call(element[key].asString());
108  return true;
109  }
110  return false;
111 }
112 
113 inline void
114  replaceIfMember(const Json::Value& element, const std::string& key, helics::Time& timeVal)
115 {
116  if (element.isMember(key)) {
117  timeVal = loadJsonTime(element[key]);
118  }
119 }
120 
121 inline void replaceIfMember(const Json::Value& element, const std::string& key, std::string& sval)
122 {
123  if (element.isMember(key)) {
124  sval = element[key].asString();
125  }
126 }
127 
128 inline void replaceIfMember(const Json::Value& element, const std::string& key, bool& bval)
129 {
130  if (element.isMember(key)) {
131  bval = element[key].asBool();
132  }
133 }
134 
135 inline void replaceIfMember(const Json::Value& element, const std::string& key, int& sval)
136 {
137  if (element.isMember(key)) {
138  sval = element[key].asInt();
139  }
140 }
141 
142 inline void replaceIfMember(const Json::Value& element, const std::string& key, double& sval)
143 {
144  if (element.isMember(key)) {
145  sval = element[key].asDouble();
146  }
147 }
148 
149 } // namespace helics::fileops
JsonProcessingFunctions.hpp
helics::Time
TimeRepresentation< count_time< 9 > > Time
Definition: helicsTime.hpp:27