CSCI 3081 - Drone Delivery System
All Classes Files Functions Variables Friends Pages
json_helper.h
1 #ifndef JSON_HELPER_H_
2 #define JSON_HELPER_H_
3 
4 #include <iostream>
5 #include <string>
6 #include <picojson.h>
7 #include <vector>
8 
9 using std::vector;
10 
11 namespace csci3081 {
12 
13 class JsonHelper {
14 public:
15 
16  /********************** Querying functions **************************/
46  static const picojson::value& GetValue(const picojson::object& obj, std::string key) {
48  if (!JsonHelper::ContainsKey(obj, key)) {
49  throw std::logic_error("Trying to get the picojson::value from key that does not exist: " + key);
50  }
51  return obj.find(key)->second;
52  }
53 
55  static const picojson::object& GetObject(const picojson::object& obj, std::string key) {
56  if (!JsonHelper::ContainsKey(obj, key)) {
57  throw std::logic_error("Trying to get the picojson::object from key that does not exist: " + key);
58  }
59  return GetValue(obj, key).get<picojson::object>();
60  }
61 
63  static const picojson::array& GetArray(const picojson::object& obj, std::string key) {
64  if (!JsonHelper::ContainsKey(obj, key)) {
65  throw std::logic_error("Trying to get the picojson::array from key that does not exist: " + key);
66  }
67  return GetValue(obj, key).get<picojson::array>();
68  }
69 
71  static std::string GetString(const picojson::object& obj, std::string key) {
72  if (!JsonHelper::ContainsKey(obj, key)) {
73  throw std::logic_error("Trying to get the string from key that does not exist: " + key);
74  }
75  return GetValue(obj, key).get<std::string>();
76  }
77 
79  static double GetDouble(const picojson::object& obj, std::string key) {
80  if (!JsonHelper::ContainsKey(obj, key)) {
81  throw std::logic_error("Trying to get the double from key that does not exist: " + key);
82  }
83  return GetValue(obj, key).get<double>();
84  }
85 
87  static std::vector<float> GetStdFloatVector(const picojson::object& obj, std::string key) {
88  if (!JsonHelper::ContainsKey(obj, key)) {
89  throw std::logic_error("Trying to get the std::vector<float> from key that does not exist: " + key);
90  }
91  std::vector<float> vec;
92  picojson::array arr = GetArray(obj, key);
93  for (int i = 0; i < arr.size(); i++) {
94  vec.push_back(arr[i].get<double>());
95  }
96  return vec;
97  }
98 
100  static bool ContainsKey(const picojson::object& obj, std::string key) {
101  return obj.find(key) != obj.end();
102  }
103 
104  /********************** Creation functions **************************/
105 
107  static picojson::object CreateJsonNotification() {
108  picojson::object obj;
109  AddStringToJsonObject(obj, "type", "notify");
110  return obj;
111  }
112 
114  static picojson::object CreateJsonObject() {
115  return picojson::object();
116  }
117 
119  static picojson::value ConvertPicojsonObjectToValue(picojson::object& obj) {
120  return picojson::value(obj);
121  }
122 
124  static void AddValueToJsonObject(picojson::object& obj, std::string key, picojson::value val) {
125  obj[key] = val;
126  }
127 
129  static void AddObjectToJsonObject(picojson::object& obj, std::string key, picojson::object& val) {
131  }
132 
134  static void AddStringToJsonObject(picojson::object& obj, std::string key, std::string val) {
135  obj[key] = picojson::value(val);
136  }
137 
139  static void AddFloatToJsonObject(picojson::object& obj, std::string key, float num) {
140  obj[key] = picojson::value(num);
141  }
142 
144  static void AddStdFloatVectorToJsonObject(picojson::object& obj, std::string key, std::vector<float> vec) {
145  picojson::array arr = CreateJsonArrayFromVector(vec);
146  obj[key] = picojson::value(arr);
147  }
148 
150  static picojson::array CreateJsonArrayFromVector(std::vector<float> vec) {
151  picojson::array arr;
152  for (auto num : vec) {
153  arr.push_back(picojson::value(num));
154  }
155  return arr;
156  }
157 
159  static void AddStdVectorVectorFloatToJsonObject(picojson::object& obj,
160  std::string key, std::vector<std::vector<float>> array) {
161  obj[key] = EncodeArray(array);
162  }
163 
165  static picojson::value EncodeArray(const vector<vector<float>> arr) { //todo discard some precision by converting to strings?
166  vector<picojson::value> result;
167  for(vector<float> subarr : arr) {
168  vector<picojson::value> subarr_encode;
169  for(float val : subarr) {
170  subarr_encode.push_back(picojson::value(val));
171  }
172  result.push_back(picojson::value(subarr_encode));
173  }
174  return picojson::value(result);
175  }
176 
177 
178  /********************** Printing functions **************************/
179 
181  static void PrintArray(const picojson::array& arr) {
182  std::cout << "Printing Array:" << std::endl << std::endl;
183  int i = 0;
184  for (auto key : arr) {
185  std::cout << "Array item: " << i++ << std::endl;
186  std::cout << " " << key << std::endl;
187  }
188  }
189 
191  static void PrintKeyValues(const picojson::object& obj, std::string prefix = " ") {
192  std::cout << "PrintKeyValues ---------" << std::endl;
193  for (picojson::object::const_iterator it = obj.begin(); it != obj.end(); it++) {
194  std::cout << prefix << it->first << ": " << it->second << std::endl;
195  }
196  std::cout << "End PrintKeyValues ---------" << std::endl;
197  }
198 
200  static void Print(const picojson::object& obj, std::string prefix = " ") {
201  picojson::value val(obj);
202  std::cout << prefix << val.serialize() << std::endl;
203  }
204 
206  static void PrintEntityDetails(const picojson::object& val) {
207  std::cout << "\n------JSON:------" << std::endl;
208  JsonHelper::Print(val);
209 
210  std::cout << "------Key Values:------" << std::endl;
212 
213  std::cout << "------Entity Type:------" << std::endl;
214  std::string type = JsonHelper::GetString(val, "type");
215  std::cout << " " << type << std::endl;
216 
217  std::cout << "------Contains Key:------" << std::endl;
218  std::cout << " Contains type: " << JsonHelper::ContainsKey(val, "type") << std::endl;
219  std::cout << " Contains otherKey: " << JsonHelper::ContainsKey(val, "otherKey") << std::endl;
220  std::cout << " Contains position: " << JsonHelper::ContainsKey(val, "position") << std::endl;
221 
222  std::cout << "------Position array:------" << std::endl;
223  if (JsonHelper::ContainsKey(val, "position")) {
224  const picojson::array& position = JsonHelper::GetArray(val, "position");
225  for (int f = 0; f < position.size(); f++) {
226  std::cout << " position[" << f << "]: " << position[f] << std::endl;
227  }
228  }
229  std::cout << std::endl;
230  }
231 };
232 
233 } // namespace csci3081
234 #endif
static picojson::value ConvertPicojsonObjectToValue(picojson::object &obj)
Converts given obj to an equivalent picojson::value.
Definition: json_helper.h:119
static void PrintEntityDetails(const picojson::object &val)
Prints the details of a json object representation of an entity.
Definition: json_helper.h:206
static double GetDouble(const picojson::object &obj, std::string key)
Returns the key from json object obj as a double, will throw an error if key doesn&#39;t exist...
Definition: json_helper.h:79
static void PrintKeyValues(const picojson::object &obj, std::string prefix=" ")
Helper method for PrintEntityDetails.
Definition: json_helper.h:191
static void AddStdFloatVectorToJsonObject(picojson::object &obj, std::string key, std::vector< float > vec)
Adds a float vector value named key to a json object.
Definition: json_helper.h:144
static void AddObjectToJsonObject(picojson::object &obj, std::string key, picojson::object &val)
Adds the latter picojson object to the former object as a value under the key &#39;key&#39;.
Definition: json_helper.h:129
static picojson::array CreateJsonArrayFromVector(std::vector< float > vec)
Creates a json array from a float vector.
Definition: json_helper.h:150
Definition: asubject.cc:3
static bool ContainsKey(const picojson::object &obj, std::string key)
Returns a boolean value of whether the json object obj contains key or not.
Definition: json_helper.h:100
static void Print(const picojson::object &obj, std::string prefix=" ")
Helper method for PrintEntityDetails.
Definition: json_helper.h:200
static std::vector< float > GetStdFloatVector(const picojson::object &obj, std::string key)
Returns the key from json object obj as a float vector, will throw an error if key doesn&#39;t exist...
Definition: json_helper.h:87
static const picojson::array & GetArray(const picojson::object &obj, std::string key)
Returns the key from json object obj as a json array, will throw an error if key doesn&#39;t exist...
Definition: json_helper.h:63
static void AddStdVectorVectorFloatToJsonObject(picojson::object &obj, std::string key, std::vector< std::vector< float >> array)
Given a picojson object, key, and vector<vector<float>> array, adds the array as the value for key in...
Definition: json_helper.h:159
static const picojson::object & GetObject(const picojson::object &obj, std::string key)
Returns the key from json object obj as a json object, throws error if key doesn&#39;t exist...
Definition: json_helper.h:55
static std::string GetString(const picojson::object &obj, std::string key)
Returns the key from json object obj as a string, will throw an error if key doesn&#39;t exist...
Definition: json_helper.h:71
static picojson::value EncodeArray(const vector< vector< float >> arr)
Given a vector<vector<float>> array, returns the array as a picojson::value.
Definition: json_helper.h:165
static void AddStringToJsonObject(picojson::object &obj, std::string key, std::string val)
Adds a string value named key to a json object.
Definition: json_helper.h:134
static void AddValueToJsonObject(picojson::object &obj, std::string key, picojson::value val)
Adds a picojson value to the specifiied key in a json object.
Definition: json_helper.h:124
static const picojson::value & GetValue(const picojson::object &obj, std::string key)
Returns the key from json object obj as a json value, throws error if key doesn&#39;t exist...
Definition: json_helper.h:47
Definition: json_helper.h:13
static void AddFloatToJsonObject(picojson::object &obj, std::string key, float num)
Adds a float value named key to a json object.
Definition: json_helper.h:139
static picojson::object CreateJsonNotification()
Create a new json object for sending notifications.
Definition: json_helper.h:107
static void PrintArray(const picojson::array &arr)
Helper method for PrintEntityDetails.
Definition: json_helper.h:181
static picojson::object CreateJsonObject()
Returns a new json object.
Definition: json_helper.h:114