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