bitrl & cuberl Documentation
Simulation engine for reinforcement learning agents
Loading...
Searching...
No Matches
json_file_reader.h
Go to the documentation of this file.
1#ifndef JSON_FILE_READER_H
2#define JSON_FILE_READER_H
3
4#include "bitrl/extern/nlohmann/json/json.hpp"
6
7namespace bitrl
8{
9namespace utils::io
10{
15{
16
17 public:
18
19 using json = nlohmann::json;
20
25 JSONFileReader(const std::string &filename);
26
30 virtual void open() override final;
31
35 template <typename T> T get_value(const std::string &label) const;
36
44 template <typename T> T at(const std::string &label) const;
45
51 const json& get(const std::string &label) const{return data_.at(label);};
52
53 private:
54 json data_;
55};
56
57template <typename T> T JSONFileReader::get_value(const std::string &label) const
58{
59 if (!this->is_open())
60 {
61 throw std::logic_error("JSON file is not open. Have you called open()?");
62 }
63 return data_[label].template get<T>();
64}
65
66template <typename T> T JSONFileReader::at(const std::string &label) const
67{
68 if (!this->is_open())
69 {
70 throw std::logic_error("JSON file is not open. Have you called open()?");
71 }
72
73 auto data = data_.at(label);
74 return T(data);
75}
76
77} // namespace utils::io
78} // namespace bitrl
79#endif // JSON_FILE_READER_H
bool is_open() const noexcept
Return true if and only if the file is open.
Definition file_handler_base.h:55
Definition file_reader_base.h:25
Definition json_file_reader.h:15
virtual void open() override final
Attempts to open the file for reading.
Definition json_file_reader.cpp:17
const json & get(const std::string &label) const
Definition json_file_reader.h:51
nlohmann::json json
Definition json_file_reader.h:19
T get_value(const std::string &label) const
Get the value specified by the label.
Definition json_file_reader.h:57
T at(const std::string &label) const
Definition json_file_reader.h:66
OutT resolve(const std::string &name, const std::map< std::string, std::any > &input)
Definition std_map_utils.h:25
Definition bitrl_consts.h:14