-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathjson_parser_utils.hpp
More file actions
331 lines (307 loc) · 9.52 KB
/
json_parser_utils.hpp
File metadata and controls
331 lines (307 loc) · 9.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/**
* Copyright © 2025 IBM Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <nlohmann/json.hpp>
#include <cstdint>
#include <map>
#include <stdexcept>
#include <string>
#include <vector>
/**
* @namespace json_parser_utils
*
* Contains utility functions for parsing JSON data.
*
* ## Variables
* The parsing functions support optional usage of variables. JSON string values
* can contain one or more variables. A variable is specified using the format
* `${variable_name}`. A variables map is specified to parsing functions that
* provides the variable values. Any variable in a JSON string value will be
* replaced by the variable value.
*
* Variables can only appear in a JSON string value. The parsing functions for
* other data types, such as integer and double, support a string value if it
* contains a variable. After variable expansion, the string is converted to the
* expected data type.
*/
namespace phosphor::power::json_parser_utils
{
/**
* Empty variables map used as a default value for parsing functions.
*/
extern const std::map<std::string, std::string> NO_VARIABLES;
/**
* Returns the specified property of the specified JSON element.
*
* Throws an invalid_argument exception if the property does not exist.
*
* @param element JSON element
* @param property property name
*/
#pragma GCC diagnostic push
#if __GNUC__ >= 13
#pragma GCC diagnostic ignored "-Wdangling-reference"
#endif
inline const nlohmann::json& getRequiredProperty(const nlohmann::json& element,
const std::string& property)
{
auto it = element.find(property);
if (it == element.end())
{
throw std::invalid_argument{"Required property missing: " + property};
}
return *it;
}
#pragma GCC diagnostic pop
/**
* Parses a JSON element containing a bit position (from 0-7).
*
* Returns the corresponding C++ uint8_t value.
*
* Throws an exception if parsing fails.
*
* @param element JSON element
* @param variables variables map used to expand variables in element value
* @return uint8_t value
*/
uint8_t parseBitPosition(
const nlohmann::json& element,
const std::map<std::string, std::string>& variables = NO_VARIABLES);
/**
* Parses a JSON element containing a bit value (0 or 1).
*
* Returns the corresponding C++ uint8_t value.
*
* Throws an exception if parsing fails.
*
* @param element JSON element
* @param variables variables map used to expand variables in element value
* @return uint8_t value
*/
uint8_t parseBitValue(
const nlohmann::json& element,
const std::map<std::string, std::string>& variables = NO_VARIABLES);
/**
* Parses a JSON element containing a boolean.
*
* Returns the corresponding C++ boolean value.
*
* Throws an exception if parsing fails.
*
* @param element JSON element
* @param variables variables map used to expand variables in element value
* @return boolean value
*/
bool parseBoolean(
const nlohmann::json& element,
const std::map<std::string, std::string>& variables = NO_VARIABLES);
/**
* Parses a JSON element containing a double (floating point number).
*
* Returns the corresponding C++ double value.
*
* Throws an exception if parsing fails.
*
* @param element JSON element
* @param variables variables map used to expand variables in element value
* @return double value
*/
double parseDouble(
const nlohmann::json& element,
const std::map<std::string, std::string>& variables = NO_VARIABLES);
/**
* Parses a JSON element containing a byte value expressed as a hexadecimal
* string.
*
* The JSON number data type does not support the hexadecimal format. For this
* reason, a hexadecimal byte value is stored in a JSON string.
*
* Returns the corresponding C++ uint8_t value.
*
* Throws an exception if parsing fails.
*
* @param element JSON element
* @param variables variables map used to expand variables in element value
* @return uint8_t value
*/
uint8_t parseHexByte(
const nlohmann::json& element,
const std::map<std::string, std::string>& variables = NO_VARIABLES);
/**
* Parses a JSON element containing an array of byte values expressed as a
* hexadecimal strings.
*
* Returns the corresponding C++ uint8_t values.
*
* Throws an exception if parsing fails.
*
* @param element JSON element
* @param variables variables map used to expand variables in element value
* @return vector of uint8_t
*/
std::vector<uint8_t> parseHexByteArray(
const nlohmann::json& element,
const std::map<std::string, std::string>& variables = NO_VARIABLES);
/**
* Parses a JSON element containing an 8-bit signed integer.
*
* Returns the corresponding C++ int8_t value.
*
* Throws an exception if parsing fails.
*
* @param element JSON element
* @param variables variables map used to expand variables in element value
* @return int8_t value
*/
int8_t parseInt8(
const nlohmann::json& element,
const std::map<std::string, std::string>& variables = NO_VARIABLES);
/**
* Parses a JSON element containing an integer.
*
* Returns the corresponding C++ int value.
*
* Throws an exception if parsing fails.
*
* @param element JSON element
* @param variables variables map used to expand variables in element value
* @return integer value
*/
int parseInteger(
const nlohmann::json& element,
const std::map<std::string, std::string>& variables = NO_VARIABLES);
/**
* Parses a JSON element containing a string.
*
* Returns the corresponding C++ string.
*
* Throws an exception if parsing fails.
*
* @param element JSON element
* @param isEmptyValid indicates whether an empty string value is valid
* @param variables variables map used to expand variables in element value
* @return string value
*/
std::string parseString(
const nlohmann::json& element, bool isEmptyValid = false,
const std::map<std::string, std::string>& variables = NO_VARIABLES);
/**
* Parses a JSON element containing an 8-bit unsigned integer.
*
* Returns the corresponding C++ uint8_t value.
*
* Throws an exception if parsing fails.
*
* @param element JSON element
* @param variables variables map used to expand variables in element value
* @return uint8_t value
*/
uint8_t parseUint8(
const nlohmann::json& element,
const std::map<std::string, std::string>& variables = NO_VARIABLES);
/**
* Parses a JSON element containing a 16-bit unsigned integer.
*
* Returns the corresponding C++ uint16_t value.
*
* Throws an exception if parsing fails.
*
* @param element JSON element
* @param variables variables map used to expand variables in element value
* @return uint16_t value
*/
uint16_t parseUint16(
const nlohmann::json& element,
const std::map<std::string, std::string>& variables = NO_VARIABLES);
/**
* Parses a JSON element containing an unsigned integer.
*
* Returns the corresponding C++ unsigned int value.
*
* Throws an exception if parsing fails.
*
* @param element JSON element
* @param variables variables map used to expand variables in element value
* @return unsigned int value
*/
unsigned int parseUnsignedInteger(
const nlohmann::json& element,
const std::map<std::string, std::string>& variables = NO_VARIABLES);
/**
* Verifies that the specified JSON element is a JSON array.
*
* Throws an invalid_argument exception if the element is not an array.
*
* @param element JSON element
*/
inline void verifyIsArray(const nlohmann::json& element)
{
if (!element.is_array())
{
throw std::invalid_argument{"Element is not an array"};
}
}
/**
* Verifies that the specified JSON element is a JSON object.
*
* Throws an invalid_argument exception if the element is not an object.
*
* @param element JSON element
*/
inline void verifyIsObject(const nlohmann::json& element)
{
if (!element.is_object())
{
throw std::invalid_argument{"Element is not an object"};
}
}
/**
* Verifies that the specified JSON element contains the expected number of
* properties.
*
* Throws an invalid_argument exception if the element contains a different
* number of properties. This indicates the element contains an invalid
* property.
*
* @param element JSON element
* @param expectedCount expected number of properties in element
*/
inline void verifyPropertyCount(const nlohmann::json& element,
unsigned int expectedCount)
{
if (element.size() != expectedCount)
{
throw std::invalid_argument{"Element contains an invalid property"};
}
}
namespace internal
{
/**
* Expands any variables that appear in the specified string value.
*
* Does nothing if the variables map is empty or the value contains no
* variables.
*
* Throws an invalid_argument exception if a variable occurs in the value that
* does not exist in the variables map.
*
* @param value string value in which to perform variable expansion
* @param variables variables map containing variable values
*/
void expandVariables(std::string& value,
const std::map<std::string, std::string>& variables);
} // namespace internal
} // namespace phosphor::power::json_parser_utils