forked from AMSC-24-25/20-fft-20-fft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json-field-handler.hpp
125 lines (109 loc) · 3.62 KB
/
json-field-handler.hpp
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
#ifndef JSON_FIELD_HANDLER_HPP
#define JSON_FIELD_HANDLER_HPP
#include <string>
#include <unordered_map>
#include <nlohmann/json.hpp>
#include <utility>
/**
* JsonFieldHandler class is a utility class that provides field names for the JSON configuration file.
*
* It also provides methods to retrieve the field values from the JSON configuration file.
*
* It is a kind of wrapper around the JSON object of the nlohmann::json library.
*/
class JsonFieldHandler {
public:
/**
* Enum class for the field names in the JSON configuration file.
*/
enum class Field {
SignalDomain,
SignalLength,
HzFrequency,
Phase,
Noise,
Seed
};
/**
* Returns true if the field is a valid field in the JSON configuration file.
* @param field The field name as a string.
* @return True if the field is valid, false otherwise.
*/
static bool hasField(const std::string &field);
/**
* Get the field name from the field enum.
* @param field The field enum.
* @return The field name.
*/
static std::string getFieldName(Field field);
/**
* Constructor that takes a JSON configuration data.
* @param json The JSON configuration data.
* @throw std::runtime_error If the JSON cannot be parsed.
* @throw std::invalid_argument If the JSON is not valid or the required fields are missing.
*/
explicit JsonFieldHandler(nlohmann::json json) : configurationLoaded(std::move(json)) {
// validate the configuration data, throw an exception if it is not valid
validation();
}
// use std::move to move the json object to the configurationLoaded field
// to avoid copying (or deep copying) the json object
/**
* Get the configuration data that was loaded as a JSON object.
* Not modifiable and not recommended. Use the getter methods instead.
* @return The configuration data as a JSON object.
*/
[[nodiscard]] const nlohmann::json &getConfigurationLoaded() const;
/**
* Get the signal domain fields from the configuration data.
* @return The signal domain.
* @throw std::runtime_error If the signal domain is null.
*/
[[nodiscard]] std::string getSignalDomain() const;
/**
* Get the signal length fields from the configuration data.
* @return The signal length.
* @throw std::runtime_error If the signal length is null.
*/
[[nodiscard]] int getSignalLength() const;
/**
* Get the Hz frequency fields from the configuration data.
* @return The Hz frequency.
*/
[[nodiscard]] double getHzFrequency() const;
/**
* Get the phase fields from the configuration data.
* @return The phase.
*/
[[nodiscard]] double getPhase() const;
/**
* Get the noise fields from the configuration data.
* @return The noise.
*/
[[nodiscard]] double getNoise() const;
/**
* Get the seed fields from the configuration data.
* @return The seed.
* @throw std::runtime_error If the seed is null.
*/
[[nodiscard]] int getSeed() const;
/**
* Check if the configuration data has a seed. Because the seed is optional, it may not be present.
* @return True if the configuration data has a seed; false otherwise.
*/
[[nodiscard]] bool hasSeed() const;
private:
/**
* Unordered map that maps the field enum to the field name.
*/
static const std::unordered_map<Field, std::string> fieldNames;
nlohmann::json configurationLoaded;
/**
* Validate the configuration data.
*
* TODO: The json schema should be used to validate the configuration data.
* @throw std::invalid_argument If the configuration data is not valid or the required fields are missing.
*/
void validation() const;
};
#endif //JSON_FIELD_HANDLER_HPP