TCPSPSuite
jsonreader.hpp
1 #ifndef JSONREADER_H
2 #define JSONREADER_H
3 
4 #include "../util/log.hpp" // for Log
5 
6 #include <json.hpp> // for json
7 #include <stdexcept> // for domain_error
8 #include <string> // for streamsize
9 class Instance;
10 
11 class InstanceMalformedException : public std::runtime_error {
12 public:
13  explicit InstanceMalformedException(const char * what);
14 };
15 
16 class JsonReader {
17 public:
18  JsonReader(std::string filename);
19 
20  Instance * parse();
21 
22 private:
23  std::string filename;
24  nlohmann::json js;
25 
26  Instance * instance;
27 
28  void parse_resources();
29  void parse_jobs();
30 
31  template <class T>
32  T
33  get_json(const char * key)
34  {
35  try {
36  return this->js[key];
37  } catch (const std::domain_error & e) {
38  BOOST_LOG(l.e()) << "Got an error trying to access " << key;
39  throw(std::move(e));
40  } catch (nlohmann::json::type_error e) {
41  BOOST_LOG(l.e()) << "JSON type error:";
42  BOOST_LOG(l.e()) << e.what();
43  BOOST_LOG(l.e()) << "Error during access of key '" << key << "'";
44 
45  throw(std::move(e));
46  }
47  }
48 
49  template <class T, class json_T>
50  T
51  get_json(const char * key, json_T & js_in)
52  {
53  try {
54  return js_in[key];
55  } catch (const std::domain_error & e) {
56  BOOST_LOG(l.e()) << "Got an error trying to access " << key;
57  throw(std::move(e));
58  } catch (nlohmann::json::type_error e) {
59  BOOST_LOG(l.e()) << "JSON type error:";
60  BOOST_LOG(l.e()) << e.what();
61  BOOST_LOG(l.e()) << "Error during access of key '" << key << "'";
62 
63  throw(std::move(e));
64  }
65  }
66 
67  Log l;
68 };
69 
70 #endif
Instance
a TCPSP instance
Definition: instance.hpp:24