-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathConfiguration_json.cpp
More file actions
50 lines (42 loc) · 1.61 KB
/
Copy pathConfiguration_json.cpp
File metadata and controls
50 lines (42 loc) · 1.61 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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Copyright 2021 (c) Christian von Arnim, ISW University of Stuttgart (for umati and VDW e.V.)
* Copyright 2022 (c) Alen Galinec
*/
#include "Configuration_json.hpp"
#include <fstream>
#include <nlohmann/json.hpp>
#include <sstream>
namespace Configuration {
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Encryption_t, ServerCert, ServerKey, TrustedClients, IssuerCerts, Revocation);
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(UserPassAuthentication_t, Username, Password);
void from_json(const nlohmann::json& j, Configuration& p) {
// if we also allow "null" values, then we need to add an "is_string()"
// check
if (j.count("Hostname") != 0) {
p.Hostname = j.at("Hostname").get<decltype(p.Hostname)::value_type>();
}
if (j.count("Port") != 0) {
p.Port = j.at("Port").get<decltype(p.Port)::value_type>();
}
if (j.count("Encryption") != 0) {
p.Encryption = j.at("Encryption").get<decltype(p.Encryption)::value_type>();
}
if (j.count("UserPassAuthentication") != 0) {
p.UserPassAuthentication = j.at("UserPassAuthentication").get<decltype(p.UserPassAuthentication)::value_type>();
}
}
Configuration FromJsonFile(std::string filename) {
std::ifstream ifs(filename);
if (!ifs.good()) {
std::stringstream ss;
ss << "File not found: '" << filename << "'";
throw std::runtime_error("File not found.");
}
nlohmann::json j;
ifs >> j;
return j.get<Configuration>();
}
} // namespace Configuration