HOPS
HOPS class reference
MHO_JSONHeaderWrapper.hh
Go to the documentation of this file.
1 #ifndef JSON_WRAPPER_HH__
2 #define JSON_WRAPPER_HH__
3 
4 #include "nlohmann/json.hpp"
5 using mho_json = nlohmann::json;
6 using mho_ordered_json = nlohmann::ordered_json;
7 namespace nl = nlohmann;
8 
9 #include <sstream>
10 #include <string>
11 
12 #include "MHO_Message.hh"
13 #include "MHO_Tokenizer.hh"
14 
15 namespace hops
16 {
17 
26 //constructor is protected
27 //this class is only intended to provide an interface that derived classes may inherit
29 {
30  protected:
32 
33  MHO_JSONWrapper(const MHO_JSONWrapper& copy) { fObject = copy.fObject; };
34 
40  void SetObject(mho_json obj) { fObject = obj; }
41 
42  protected:
44 
45  public:
46  virtual ~MHO_JSONWrapper(){};
47 
54  bool HasKey(const std::string& key) const
55  {
56  auto it = fObject.find(key);
57  if(it != fObject.end())
58  {
59  return true;
60  }
61  return false;
62  }
63 
70  bool HasKey(const char* char_key) const
71  {
72  std::string key(char_key);
73  return HasKey(key);
74  }
75 
77  {
78  if(this != &rhs)
79  {
80  fObject = rhs.fObject;
81  }
82  return *this;
83  }
84 
88  void Clear() { fObject.clear(); }
89 
97  template< typename XValueType > void Insert(const std::string& key, const XValueType& value)
98  {
99  //allow replacement of values
100  fObject[key] = value;
101  }
102 
110  template< typename XValueType > bool Retrieve(const std::string& key, XValueType& value) const
111  {
112  auto iter = fObject.find(key);
113  if(iter == fObject.end())
114  {
115  return false;
116  }
117  mho_json test;
118  test["test"] = value;
119  if(iter.value().type() == test.begin().value().type())
120  {
121  value = iter.value().get< XValueType >();
122  return true;
123  }
124  else
125  {
126  return false;
127  }
128  }
129 
135  std::vector< std::string > DumpKeys() const
136  {
137  std::vector< std::string > keys;
138  for(auto iter = fObject.begin(); iter != fObject.end(); iter++)
139  {
140  keys.push_back(iter.key());
141  }
142  return keys;
143  }
144 
145  //TODO eliminate me
149  void DumpMap() const
150  {
151  for(auto iter = fObject.begin(); iter != fObject.end(); iter++)
152  {
153  std::cout << iter.key() << " : " << iter.value() << std::endl;
154  }
155  }
156 
163  bool ContainsKey(const std::string& key) const
164  {
165  auto iter = fObject.find(key);
166  if(iter == fObject.end())
167  {
168  return false;
169  }
170  else
171  {
172  return true;
173  }
174  }
175 };
176 
177 //specialize for mho_json
185 template<> inline bool MHO_JSONWrapper::Retrieve(const std::string& key, mho_json& value) const
186 {
187  auto iter = fObject.find(key);
188  if(iter == fObject.end())
189  {
190  return false;
191  }
192  value = fObject[key];
193  return true;
194 }
195 
196 } // namespace hops
197 
198 #endif
nlohmann::ordered_json mho_ordered_json
Definition: MHO_JSONHeaderWrapper.hh:6
nlohmann::json mho_json
Definition: MHO_JSONHeaderWrapper.hh:5
Definition: MHO_JSONHeaderWrapper.hh:29
bool ContainsKey(const std::string &key) const
Checks if a map contains a specific key.
Definition: MHO_JSONHeaderWrapper.hh:163
virtual ~MHO_JSONWrapper()
Definition: MHO_JSONHeaderWrapper.hh:46
std::vector< std::string > DumpKeys() const
Dumps all keys from the internal object.
Definition: MHO_JSONHeaderWrapper.hh:135
MHO_JSONWrapper()
Definition: MHO_JSONHeaderWrapper.hh:31
bool Retrieve(const std::string &key, XValueType &value) const
Retrieves a value from an object by key and casts it to the specified type.
Definition: MHO_JSONHeaderWrapper.hh:110
MHO_JSONWrapper(const MHO_JSONWrapper &copy)
Definition: MHO_JSONHeaderWrapper.hh:33
void DumpMap() const
Dumps the contents of the map fObject to standard output.
Definition: MHO_JSONHeaderWrapper.hh:149
void SetObject(mho_json obj)
Setter for object.
Definition: MHO_JSONHeaderWrapper.hh:40
bool HasKey(const char *char_key) const
Checks if a key exists in the internal map.
Definition: MHO_JSONHeaderWrapper.hh:70
void Clear()
Clears the internal object.
Definition: MHO_JSONHeaderWrapper.hh:88
MHO_JSONWrapper & operator=(const MHO_JSONWrapper &rhs)
Definition: MHO_JSONHeaderWrapper.hh:76
void Insert(const std::string &key, const XValueType &value)
Inserts or replaces an object in a map using a key and value.
Definition: MHO_JSONHeaderWrapper.hh:97
bool HasKey(const std::string &key) const
Checks if a key exists in the internal map.
Definition: MHO_JSONHeaderWrapper.hh:54
mho_json fObject
Definition: MHO_JSONHeaderWrapper.hh:43
Definition: MHO_ChannelLabeler.hh:17
Definition: vex.h:175