FhSim  3.1.0
Marine systems simulation
Loading...
Searching...
No Matches
JsonReading.h
1#pragma once
2
8#include <algorithm>
9#include <initializer_list>
10#include <nlohmann/json.hpp>
11#include <numbers>
12#include <stdexcept>
13#include <string>
14
15namespace util
16{
17
23inline double ReadHeadingRad(const nlohmann::json& node)
24{
25 if (node.contains("heading_rad"))
26 return node.at("heading_rad").get<double>();
27 if (node.contains("heading_deg"))
28 return node.at("heading_deg").get<double>() * std::numbers::pi / 180.0;
29 return 0.0;
30}
31
45inline void RequireKnownKeys(const nlohmann::json& node, std::initializer_list<const char*> known, const std::string& what)
46{
47 if (!node.is_object())
48 throw std::runtime_error(what + " must be a JSON object");
49 for (const auto& entry : node.items()) {
50 if (entry.key().starts_with('_'))
51 continue;
52 const bool isKnown = std::any_of(known.begin(), known.end(), [&](const char* key) { return entry.key() == key; });
53 if (isKnown)
54 continue;
55 std::string expected;
56 for (const char* key : known)
57 expected += std::string(expected.empty() ? "" : ", ") + "'" + key + "'";
58 throw std::runtime_error("unknown key '" + entry.key() + "' in " + what + "; expected " + expected);
59 }
60}
61
62} // namespace util
Utility functions for reading Environment parameters from XML.
Definition BathymetryDiagnostics.h:16
void RequireKnownKeys(const nlohmann::json &node, std::initializer_list< const char * > known, const std::string &what)
Definition JsonReading.h:45
double ReadHeadingRad(const nlohmann::json &node)
Definition JsonReading.h:23